如何在 JSF 的单个页面中从两个不同的特定于语言环境的文件加载属性?

How to load properties from two different locales-specific files in a single page in JSF?

资源包配置为-

    <resource-bundle>                        
        <base-name>views.msgs.labels</base-name>
        <var>msg</var>
    </resource-bundle>

faces-config.xml.

resources/views/msgs/ 中有 2 个属性文件 -

labels_en.properties
labels_fr.properties

有 2 个部分:

在上半部分,文本/标签需要显示在 English,而下半部分,文本必须显示在 French

简单来说,

  <f:view locale="en" >            
            #{msg['hello']}
            #{msg['name']}
  </f:view>
  <f:view locale="fr" >            
            #{msg['HELLO']}
            #{msg['name']}
  </f:view>

两个键 hello & name 出现在两个 属性 文件中。

我看到下半部分的 locale="fr" f:view 覆盖了上面的部分。

并且整个输出出现在 French

我该如何解决这个问题?

不支持此结构。 <f:view>代表UIViewRoot。虽然可以有多个 <f:view> 标签,但在组件树中只能有一个 UIViewRoot 实例。后一个 <f:view> 声明的属性将覆盖任何先前声明的属性。

您最好的选择是通过 ResourceBundle API 通常的方式手动加载资源包,并在 EL 中为其分配不同的变量名称。

最简单的方法是为此使用托管 bean。例如

@ManagedBean
@RequestScoped
public class Labels {

    private ResourceBundle french;

    @PostConstruct
    public void init() {
        french = ResourceBundle.getBundle("views.msgs.labels", Locale.FRENCH);
    }

    public ResourceBundle getFrench() {
        return french;
    }

}

(提示:你可以获得<default-locale> and all <supported-locale>s via Application

然后如下使用:

Current locale:
#{msg['hello']}
#{msg['name']}

French locale:
#{labels.french['HELLO']}
#{labels.french['name']}

另请参阅:

  • Read resource bundle properties in a managed bean
  • Getting Message from all resource files in different locale from a key