Ed *_*d . 4 java jsf properties internationalization
我正在将一些JSF文件国际化,因此我正在外化字符串(以及使用占位符连接字符串).我对JSF(今天和昨天)的经验很少,所以如果对我的问题有一个明显的回答,请原谅!
我已经成功地为简单的占位符使用了h:outputFormat标记(和f:param标记),但现在我正在寻找用commandLink组件替换占位符.
即
<h:outputFormat value="#{adminMsgs.scheduleUpdateLink} ">
<h:commandLink value="#{adminMsgs.update}" action="refresh" />
</h:outputFormat>
Run Code Online (Sandbox Code Playgroud)
属性文件:
scheduleUpdateLink = After waiting a few seconds, please {0} the page.
update = update
Run Code Online (Sandbox Code Playgroud)
并按以下方式输出:
After waiting a few seconds, please <a href="#" class="..." onclick="...;return false;">update</a> the page.
Run Code Online (Sandbox Code Playgroud)
这不起作用(更新链接出现在'scheduleUpdateLink'文本之前),有谁知道我怎么做?
提前致谢.
编辑/更新
感谢您的回复McDowell - 非常有用,但仍然没有完全解决我的问题.我还需要对输入框(h:inputText)执行相同的操作,并且可能还存在一个资源字符串中有多个占位符的情况.因此,我不能保证阿拉伯语中的顺序是一样的.
如果我使用Java函数; 你知道我是否可以通过JSF标签传递一个字符串,例如<h:outputFormat value=...并使用faces上下文来获取呈现的HTML,然后我可以将其插入到各自的占位符中并以纯HTML格式返回?或者沿着这些方向的任何其他想法?
干杯.
在H:OUTPUTFORMAT标签将(据我所知)只使用F:PARAM孩子作为参数.
如果消息是链接(而不是h:commandLink),则可以使用HTML文字:
<h:outputFormat escape="false"
value="...seconds, please <a href='etc..." />.
Run Code Online (Sandbox Code Playgroud)
我不建议尝试通过这样做来模拟h:commandLink.有可能在第三方库中找到更丰富的h:commandLink版本,它完全符合您的要求.
将字符串分成两部分是可能的,但是翻译人员的生活很困难,翻译质量可能会受到影响(特别是如果你这么做的话).
我会看一下使用这样的自定义函数:
<f:loadBundle basename="i18n.I18n" var="bundle" />
<h:outputText value="#{i18n:fragment(bundle.scheduleUpdateLink, 0)}" />
<h:commandLink>
<h:outputText value="#{bundle.update}" />
</h:commandLink>
<h:outputText value="#{i18n:fragment(bundle.scheduleUpdateLink, 1)}" />
Run Code Online (Sandbox Code Playgroud)
渲染的输出格式如下:
After waiting a few seconds, please <script type="text/javascript"><!--
/*JavaScript elided*/
//-->
</script><a href="#"
onclick="return oamSubmitForm('someClientId');">update</a> the page.
Run Code Online (Sandbox Code Playgroud)
该函数由静态方法支持,该方法拆分字符串并返回请求的片段:
public static String fragment(String string, int index) {
// TODO: regex probably not complete; ask stackoverflow!
// see http://java.sun.com/javase/6/docs/api/java/text/MessageFormat.html
final String regex = "\\{\\d.*?\\}";
String[] fragments = string.split(regex, index + 2);
return fragments[index];
// TODO: error handling
}
Run Code Online (Sandbox Code Playgroud)
根据您的视图技术(Facelets或JSP),函数声明略有不同:
编辑:基于更多信息
JSF采用声明式方法来构建接口.您想要一个动态UI,其子项按照它们在翻译字符串中出现的顺序排序.这两件事情不一致,但一切都不会丢失.
有几种方法可以解决这个问题.
.
<ed:formatPane string="click {0} after filling in forename {1} and surname {2}">
<h:commandLink value="#{bundle.linkMsg}" />
<h:inputText value="#{bean.prop1}" />
<h:inputText value="#{bean.prop2}" />
</ed:formatPane>
Run Code Online (Sandbox Code Playgroud)
只需替换h:outputFormat的渲染器以支持任意子节点而不是创建完整的自定义控件,可能(而且工作量较少),但我反对它,因为它可能会导致长期维护期间的混乱.
有人可能已经编写了一个自定义控件来完成我的建议(它几乎不是一个原创的想法).