【问题标题】:jsf2 f:selectItem itemLabel complex i18n renderjsf2 f:selectItem itemLabel 复杂 i18n 渲染
【发布时间】:2012-08-13 15:48:09
【问题描述】:

我有一个带有 itemLabel 的 f:selectitem,我想将标签渲染得如此“漂亮”!

问题:

<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 标记中。

我尝试过&lt;f:facet name="itemLabel"&gt;,但没有。

有人可以帮助我吗?

感谢您的建议,

阿格哈塔

【问题讨论】:

    标签: jsf internationalization tags el


    【解决方案1】:

    JSF 不会让您在标记方面这样做。没有办法使用例如&lt;h:outputFormat /&gt;

    相反,您需要简单地创建一个 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
      }
    }
    

    关于可本地化的几点补充:

    1. 请使用有意义的键名。像“message.1”这样的东西没有给翻译者提供上下文。我不知道你在做什么,但也许“some-module.select.message.radio.message.sent.by.pattern”会更好——译者需要知道 a) 它将在哪里显示, b) 该文本的目的是什么(是否是一些描述、消息模式、一般文本、窗口/对话框标题、按钮标题等)。一定要为他们提供这样的背景。

    2. 确保在邮件本身中包含格式标记。这意味着,消息在您的属性文件中应该看起来像message.1=some message by &lt;b&gt;{0}&lt;/b&gt;。您会惊讶于这些标签需要多久被删除(或用其他强调方式替换)。您应该特别小心,因为您似乎在使用 RTL 语言做一些事情,而粗体字体不能很好地使用它们。

    【讨论】:

    • @BalusC:通常情况下,我不会和你争论,但在这种情况下我必须这样做。如果您知道如何可靠地将参数传递给 selectItem 的标签,请不要犹豫,提出您的答案。在一般情况下,我知道如何在 JSF 中格式化消息。但根据我的经验,它不适用于嵌入式案例(例如这个)。至于 getBundle() 的路径,只是为了表明你应该在这里放置一个有效的路径。是的,它是类路径位置,所以您可能期望“。”作为分隔符......好吧,我发现a)斜线工作b)通常效果更好。奇怪,但真实。
    猜你喜欢
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 2011-01-10
    • 2019-03-09
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多