我有一个 f:selectitem 及其 itemLabel,我想将标签渲染得如此“漂亮”!
问题:
<p:selectOneRadio id="selectRadio" value="#{somebean.somevalue}" layout="pageDirection" >
<f:selectItem itemLabel="#{msg['message.1']}" itemValue="1" />
<f:selectItem itemLabel="#{msg['message.2']}" itemValue="2" />
</p:selectOneRadio>
我的属性 i18n 文件:
message.1=some message by {0}
message.2=another message by {0}
我想用 #{somebean.theUser} 内联替换 {0} 结果应该是这样的(包括粗体):
some message by <b>HUSTON</b>
在标记文件中我应该做这样的事情(或以类似的方式)
<f:selectItem itemLabel="#{msg['message.1']{'<b>'+somebean.theUser+'</b>'}}" itemValue="1" />
换句话说,我想将 i18n 字符串参数替换直接添加到 itemLabel 标记中。
我尝试过 <f:facet name="itemLabel">
但什么也没有。
有人可以帮助我吗?
感谢您的建议,
阿加塔
最佳答案
JSF 不会让您从标记方面这样做。没有办法使用例如 <h:outputFormat />
为了它。
相反,您需要简单地创建一个 bean,它将从属性文件中读取内容并相应地设置格式:
<p:selectOneRadio id="selectRadio" value="#{somebean.somevalue}" layout="pageDirection" >
<f:selectItem itemLabel="#{helperbean.someMessage}" itemValue="1" />
<f:selectItem itemLabel="#{helperbean.anotherMessage}" itemValue="2" />
</p:selectOneRadio>
那是 JSF 部分,在您的 bean 中:
public String getSomeMessage() {
// Actually you need some common access helper, it is just simplified example
String messagePattern = null;
try {
ResourceBundle rb = ResourceBundle.getBundle("path/to/properties/file");
String messagePattern = rb.getString("message.1");
return MessageFormat.format(messagePattern, somebean.getTheUser());
} catch (MissingResourceException mre) {
logger.warn("Missing resource file or resource key", mre);
return "!message.1!"; // That will show you where the problem is
}
}
关于可本地化性的几点补充:
请使用有意义的键名称。像“message.1”这样的东西没有给翻译者提供任何上下文。我不知道你在做什么,但也许“some-module.select.message.radio.message.sent.by.pattern”会更好 - 翻译者需要知道a)它将在哪里显示, b) 该文本的目的是什么(是否是一些描述、消息模式、一般文本、窗口/对话框标题、按钮标题等)。请务必向他们提供这样的背景信息。
请务必在邮件本身中包含格式标记。这意味着,该消息应类似于
message.1=some message by <b>{0}</b>
在你的属性文件中。您会惊讶地发现这些类型的标签需要删除(或替换为其他强调手段)的频率。您应该特别小心,因为您似乎正在使用 RTL 语言执行某些操作,而粗体字体不适用于它们。
关于jsf2 f :selectItem itemLabel complex i18n render,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11937797/