【问题标题】:Placeholder in resource bundles资源包中的占位符
【发布时间】:2017-05-17 08:59:50
【问题描述】:

我需要一些占位符来自动填充我的资源包。

我的应用程序的用户有两种可能的配置,例如“1”和“2”。根据此配置,应返回具有正确值的条目。

我知道,条件是可能的:

currentConfig=The configuration is currently the {0,choice,0#first|1#second}

在我的 faces-config.xml 中,资源包配置为在 JSF 中访问。

现在,我想在不指定参数的情况下在 JSF 中获取这个值:

<h:outputText value="#{res.currentConfig}"/>

如果用户配置了“1”,则返回第一个值,否则返回第二个值。

with configuration "1": The configuration is currently the first.
with configuration "2": The configuration is currently the second.

这可以独立于 JSF 页面实现吗?

【问题讨论】:

    标签: jsf jakarta-ee jsf-2 resourcebundle


    【解决方案1】:

    您可以按照自己的方式实现从资源包中获取文本。例如,您可以在一些 managedbean 中创建这样的方法:

    public String getCongiurationText() {
        FacesContext context = FacesContext.getCurrentInstance();
        Locale locale = context.getViewRoot().getLocale();
        ResourceBundle bundle = ResourceBundle.getBundle("messages", locale);
        String msg = bundle.getString("currentConfig");
    
        Integer value = 1;
    
        return MessageFormat.format(msg, getConfiguration());
    }
    

    然后在你的 outputText 中:

    <h:outputText value="#{someUtilBean.getCongiurationText()}" />
    

    通过这种方法,您可以获得独立于 JSF 页面的消息。因此,在每个页面中,您都指定相同的方法,但根据配置,它会显示不同的文本。配置当然可以通过不同的方式获得,但这取决于您希望如何处理它。

    【讨论】:

    • 感谢您的回复,但我不想更改我的 JSF 文件。最好的方法是从 JSF 文件全局自定义资源调用。
    最近更新 更多