占位符在资源包中 [英] Placeholder in resource bundles

查看:163
本文介绍了占位符在资源包中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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}

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

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

现在,我想得到这个值在JSF中没有指定参数:

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

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

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

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.

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

Can this be implemented independently of the JSF pages ?

推荐答案

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

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());
}

然后在你的outputText中:

Then in your outputText :

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

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

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.

这篇关于占位符在资源包中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆