如何本地化 JSF 2 复合组件
我对本地化如何与 JSF 中的复合组件一起工作有一些疑问,我想很好地理解它是如何工作的。
所以我决定用一个小例子来练习复合组件的本地化。
按照建议,我在复合组件所在的完全相同的文件夹中创建了一个 .properties 文件(WebContent/resources 的子文件夹)
labelField1 = FIELD 1
labelField2 = FIELD 2
然后我使用了#{cc.resourceBundleMap。将本地化文本添加到组件实现中:
<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>
我运行应用程序时出现的问题是:
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)
...
我的问题是:
- 我是否需要以某种方式手动加载该消息包,或者这应该自动发生?
- 我的应用程序中复合组件所在文件夹之外的其他消息包是否会干扰这个消息包?(我在应用程序的其他位置也有一个 message_en.properties,用于模板和 UI 的其他部分)
- 我该如何修复它?
I have some doubts on how localization works with composite components in JSF, i want to understand well how it works.
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)
labelField1 = FIELD 1
labelField2 = FIELD 2
Then i used #{cc.resourceBundleMap. to add the localized text to the components 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)
-How can i fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
资源包的文件名必须与组件的文件名完全相同。
因此,如果您有一个表示复合组件的
demoCustomComponent.xhtml
文件,那么您应该有一个包含(默认)本地化消息的demoCustomComponent.properties
文件。您可以使用例如demoCustomComponent_es.properties
将其国际化,但路径中应该始终有一个默认值。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 ademoCustomComponent.properties
file holding the (default) localized messages. You can internationalize it with for exampledemoCustomComponent_es.properties
, but you should always have a default one in the path.