现在很多WEB网站都提供了多语言切换功能,在使用ADF创建的WEB应用中,很容易实现多语言,只需简单的几个步骤。
2013年6月25日更新:通过在URL后面添加参数改变locale,具体参见代码。URL如:http://localhost:7101/locale/faces/test.jspx?language=en
资源文件的文件名结构为basename_language[_country][_variant].extenstion
例如UIResource_zh.properties, UIResources_zh_CN.properties
经过上面的配置之后,在页面上使用资源文件中的项目也变的非常简单,只需要通过EL编辑器选择即可。
这样一来,只需要切换浏览器的语言,应用程序会自动根据浏览器的语言环境选择匹配的语言显示。但是这样还不能达到实时切换语言的要求,那么下面的步骤将介绍如何编写代码来切换语言。
package adf.locale.view; import java.io.IOException; import java.util.Locale; import javax.el.ELContext; import javax.el.ExpressionFactory; import javax.el.ValueExpression; import javax.faces.context.FacesContext; import javax.faces.event.ValueChangeEvent; import javax.servlet.http.HttpServletRequest; import oracle.adf.model.BindingContext; import oracle.adf.share.ADFContext; import oracle.adf.view.rich.context.AdfFacesContext; import oracle.jbo.common.DefLocaleContext; public class LocaleManager { public static final String EL_KEY = "#{localeManager}"; private String currentLanguage; private Locale currentLocale; private static LocaleManager instance; public LocaleManager() { super(); } public static LocaleManager getInstance() { if(instance == null) { Object value = evaluateEL(EL_KEY); if(value instanceof LocaleManager) { instance = (LocaleManager)value; } else { instance = new LocaleManager(); } } return instance; } /** * 用于绑定下拉列表的value change,根据用户选择的语言,然后刷新页面。 * @param event * @throws IOException */ public void changeLocale(ValueChangeEvent event) throws IOException { String newLanguage = (String)event.getNewValue(); if (newLanguage != null) { this.currentLanguage = newLanguage; Locale locale = new Locale(newLanguage); this.currentLocale = locale; FacesContext ctx = FacesContext.getCurrentInstance(); HttpServletRequest request = (HttpServletRequest)ctx.getExternalContext().getRequest(); String uri = request.getRequestURI(); ctx.getExternalContext().redirect(uri); } } private static Object evaluateEL(String el) { FacesContext ctx = FacesContext.getCurrentInstance(); ELContext elCtx = ctx.getELContext(); ExpressionFactory ef = ctx.getApplication().getExpressionFactory(); ValueExpression ve = ef.createValueExpression(elCtx, el, Object.class); return ve.getValue(elCtx); } public void setCurrentLanguage(String currentLanguage) { this.currentLanguage = currentLanguage; } public String getCurrentLanguage() { return currentLanguage; } public void setCurrentLocale(Locale currentLocale) { this.currentLocale = currentLocale; } public Locale getCurrentLocale() { return currentLocale; } }
package adf.locale.view; import java.io.IOException; import java.util.Locale; import javax.el.ELContext; import javax.el.ExpressionFactory; import javax.el.ValueExpression; import javax.faces.FacesException; import javax.faces.application.ViewHandler; import javax.faces.component.UIViewRoot; import javax.faces.context.FacesContext; public class LocaleSettingViewHandler extends ViewHandler { private ViewHandler base; public LocaleSettingViewHandler() { super(); } public LocaleSettingViewHandler(ViewHandler base) { this.base = base; } public Locale calculateLocale(FacesContext facesContext) { LocaleManager localeManager = LocaleManager.getInstance(); //通过添加URL参数language来改变Locale String language = facesContext.getExternalContext().getRequestParameterMap().get("language"); if(language != null) { localeManager.setCurrentLanguage(language); localeManager.setCurrentLocale(new Locale(language)); } Locale locale = localeManager.getCurrentLocale(); if(locale == null) { locale = Locale.getDefault(); } return locale; } public String calculateRenderKitId(FacesContext facesContext) { return base.calculateRenderKitId(facesContext); } public UIViewRoot createView(FacesContext facesContext, String string) { return base.createView(facesContext, string); } public String getActionURL(FacesContext facesContext, String string) { return base.getActionURL(facesContext, string); } public String getResourceURL(FacesContext facesContext, String string) { return base.getResourceURL(facesContext, string); } public void renderView(FacesContext facesContext, UIViewRoot uIViewRoot) throws IOException, FacesException { base.renderView(facesContext, uIViewRoot); } public UIViewRoot restoreView(FacesContext facesContext, String string) { return base.restoreView(facesContext, string); } public void writeState(FacesContext facesContext) throws IOException { base.writeState(facesContext); } public void setBase(ViewHandler base) { this.base = base; } public ViewHandler getBase() { return base; } }
<af:selectOneChoice id="soc1" label="#{res['login.language.label']}" value="#{localeManager.currentLanguage}" valueChangeListener="#{localeManager.changeLocale}" autoSubmit="true"> <af:selectItem label="简体中文" value="zh" id="si1"/> <af:selectItem label="English" value="en" id="si2"/> </af:selectOneChoice>
还有种方法不用编写LocaleSettingViewHandler类的,只需要LocaleManager类即可,需要把LocaleManager类中的currentLocale和jspx中view标签的locale属性绑定。不过这样应该需要在每一个jspx页面中进行设置。可以根据需求灵活选择不同的方法。
<f:view locale="#{localeManager.currentLocale}">
http://docs.oracle.com/cd/E25178_01/web.1111/b31973/af_global.htm#CHDGCAFI
http://download.csdn.net/detail/ygj26/4891805