Facelets如何进行i18n

xiaoxiao2026-09-12  13

如果使用JSP作为JSF的视图,那么在每个也没可以用<f:loadBundle>加载国际化文件,但是使用Faclets如何进行国际化文件加载呢?我的做法如下

1. 在faces-config.xml中增加如下配置:

<application> <view-handler>com.sun.facelets.FaceletViewHandler</view-handler> <locale-config> <default-locale>zh_CN</default-locale> </locale-config> <message-bundle>message</message-bundle> </application>

 2. 在/WEB-INF/classes/下增加message.properties和message_zh_CN.properties两个文件,一个是默认资源文件,一个是中文资源文件。可以根据自己应用的需要添加其他语言支持。

3. 在模版文件,比如/WEB-INF/facelets/template.xhtml中加载这个资源

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <f:loadBundle basename="message" var="msg"/> ...... </html>

 4. 在使用这个模版的客户端文件中不必再进行加载,直接使用即可,例如welcome.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" template="/WEB-INF/facelets/template.xhtml"> <ui:define name="title"> ${msg.app_title} </ui:define> .... </ui:composition>

 因为我使用的是MyFaces JSF 1.1的实现,据说JSF1.2可以完全在faces-config.xml中进行配置,有机会试试~~

转载请注明原文地址: https://www.6miu.com/read-5052566.html

最新回复(0)