Placeholder in resource bundles

喜你入骨 提交于 2019-12-12 05:38:22

问题


I need some placeholders with automatic filling in my resource bundle.

An user of my application has two possible configuration, for example "1" and "2". Depending on this configuration, an entry should returned with the correct value.

I know, that conditions are possible:

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

In my faces-config.xml the resource bundle is configured for access in JSF.

Now, I want to get this value in JSF without specifying parameters:

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

If the user has configured "1", the first value should be returned, otherwise the second value.

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

Can this be implemented independently of the JSF pages ?


回答1:


You can implement get text from resource bundle in your own way. For instance you could create such a method in some 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());
}

Then in your outputText :

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

With that approach you get message independently of JSF pages. So in every page you specify the same method, but depending on configuration it display different text. Configuration can be obtained of course in different way, but it depends how you would like to handle it.



来源:https://stackoverflow.com/questions/41440624/placeholder-in-resource-bundles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!