简体   繁体   English

如何本地化JSF 2复合组件

[英]How to localize JSF 2 composite components

I have some doubts on how localization works with composite components in JSF, i want to understand well how it works. 我对JSF中复合组件的本地化如何工作有一些疑问,我想了解它是如何工作的。

So i decided to practice localization for composite components with a little example. 所以我决定用一个小例子练习复合组件的本地化。

Following the recommendations i created a .properties file in the exactly same folder where the composite component is(A subfolder of WebContent/resources) 根据建议,我在复合组件所在的完全相同的文件夹中创建了一个.properties文件(WebContent / resources的子文件夹)

labelField1 = FIELD 1
labelField2 = FIELD 2

Then i used #{cc.resourceBundleMap. 然后我用#{cc.resourceBundleMap。 to add the localized text to the components implmentation: 将本地化文本添加到组件implmentation:

<html>

<composite:interface>
        ...
</composite:interface>

<composite:implementation>
    <h:form>
            <h:outputText value="#{cc.resourceBundleMap.labelField1}"/>
                                      ...
            <h:outputText value="#{cc.resourceBundleMap.labelField2}"/>
                                              ...
    </h:form>

</composite:implementation> 

</html>

The problem when i run the application is this: 我运行应用程序时的问题是:

SEVERE: Error Rendering View[/index.xhtml]
javax.el.ELException: /resources/custom/demoCustomComponent.xhtml @14,63 value="#{cc.resourceBundleMap.labelField1}": java.lang.NullPointerException
....
Caused by: java.lang.NullPointerException
at javax.faces.component.UIComponent.findComponentResourceBundleLocaleMatch(UIComponent.java:1000)

... ...

My questions are: 我的问题是:

-Do i need to manually load somehow that message bunddle or this should happend automatically? - 我是否需要以某种方式手动加载该消息包或这应该自动发生?

-Can other message bundles in my app outside the folder where the composite component is, disturb this one?(I also have a message_en.properties somewhere else in the app, for the templates and other parts of the UI) - 我的应用程序中的其他消息包在复合组件所在的文件夹之外,打扰这个?(我还在应用程序的其他地方有一个message_en.properties,用于模板和UI的其他部分)

-How can i fix it? - 我怎么修理它?

The filename of the resource bundle has to be exactly the same as the component's filename. 资源包的文件名必须与组件的文件名完全相同

So, if you have a demoCustomComponent.xhtml file representing the composite component, then you should have a demoCustomComponent.properties file holding the (default) localized messages. 因此,如果您有一个表示复合组件的demoCustomComponent.xhtml文件,那么您应该有一个demoCustomComponent.properties (默认)本地化消息的demoCustomComponent.properties文件。 You can internationalize it with for example demoCustomComponent_es.properties , but you should always have a default one in the path. 您可以使用例如demoCustomComponent_es.properties对其进行国际化,但是您应该始终在路径中使用默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM