ADF 11g 界面多语言实现及切换

介绍

现在很多WEB网站都提供了多语言切换功能,在使用ADF创建的WEB应用中,很容易实现多语言,只需简单的几个步骤。

2013年6月25日更新:通过在URL后面添加参数改变locale,具体参见代码。URL如:http://localhost:7101/locale/faces/test.jspx?language=en

效果预览



实现步骤

一、建立并配置使用资源文件

    1.建立资源文件

       资源文件的文件名结构为basename_language[_country][_variant].extenstion

           例如UIResource_zh.properties, UIResources_zh_CN.properties

      

    2.在项目中的faces-config.xml中配置资源文件

     

     3.使用资源文件中的项目

          经过上面的配置之后,在页面上使用资源文件中的项目也变的非常简单,只需要通过EL编辑器选择即可。

          

    这样一来,只需要切换浏览器的语言,应用程序会自动根据浏览器的语言环境选择匹配的语言显示。但是这样还不能达到实时切换语言的要求,那么下面的步骤将介绍如何编写代码来切换语言。

二、编程切换界面语言

    1.编写Manage Bean,并且将Manage Bean配置到adfc-config.xml中,选择合适的scope 
[java]  view plain copy
  1. package adf.locale.view;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import java.util.Locale;  
  6.   
  7. import javax.el.ELContext;  
  8. import javax.el.ExpressionFactory;  
  9. import javax.el.ValueExpression;  
  10.   
  11. import javax.faces.context.FacesContext;  
  12. import javax.faces.event.ValueChangeEvent;  
  13.   
  14. import javax.servlet.http.HttpServletRequest;  
  15.   
  16. import oracle.adf.model.BindingContext;  
  17. import oracle.adf.share.ADFContext;  
  18. import oracle.adf.view.rich.context.AdfFacesContext;  
  19.   
  20. import oracle.jbo.common.DefLocaleContext;  
  21.   
  22. public class LocaleManager {  
  23.   
  24.     public static final String EL_KEY = "#{localeManager}";  
  25.   
  26.     private String currentLanguage;  
  27.     private Locale currentLocale;  
  28.     private static LocaleManager instance;  
  29.   
  30.     public LocaleManager() {  
  31.         super();  
  32.     }  
  33.   
  34.     public static LocaleManager getInstance() {  
  35.         if(instance == null) {  
  36.             Object value = evaluateEL(EL_KEY);  
  37.             if(value instanceof LocaleManager) {  
  38.                 instance = (LocaleManager)value;  
  39.             } else {  
  40.                 instance = new LocaleManager();  
  41.             }  
  42.         }  
  43.         return instance;  
  44.     }  
  45.       
  46.     /** 
  47.      * 用于绑定下拉列表的value change,根据用户选择的语言,然后刷新页面。 
  48.      * @param event 
  49.      * @throws IOException 
  50.      */  
  51.     public void changeLocale(ValueChangeEvent event) throws IOException {  
  52.         String newLanguage = (String)event.getNewValue();  
  53.         if (newLanguage != null) {  
  54.             this.currentLanguage = newLanguage;  
  55.             Locale locale = new Locale(newLanguage);  
  56.             this.currentLocale = locale;  
  57.             FacesContext ctx = FacesContext.getCurrentInstance();  
  58.             HttpServletRequest request =  
  59.                 (HttpServletRequest)ctx.getExternalContext().getRequest();  
  60.             String uri = request.getRequestURI();  
  61.             ctx.getExternalContext().redirect(uri);  
  62.         }  
  63.     }  
  64.   
  65.     private static Object evaluateEL(String el) {  
  66.         FacesContext ctx = FacesContext.getCurrentInstance();  
  67.         ELContext elCtx = ctx.getELContext();  
  68.         ExpressionFactory ef = ctx.getApplication().getExpressionFactory();  
  69.         ValueExpression ve = ef.createValueExpression(elCtx, el, Object.class);  
  70.         return ve.getValue(elCtx);  
  71.     }  
  72.   
  73.     public void setCurrentLanguage(String currentLanguage) {  
  74.         this.currentLanguage = currentLanguage;  
  75.     }  
  76.   
  77.     public String getCurrentLanguage() {  
  78.         return currentLanguage;  
  79.     }  
  80.   
  81.     public void setCurrentLocale(Locale currentLocale) {  
  82.         this.currentLocale = currentLocale;  
  83.     }  
  84.   
  85.     public Locale getCurrentLocale() {  
  86.         return currentLocale;  
  87.     }  
  88. }  
    2.编写并配置ViewHandler类

[java]  view plain copy
  1. package adf.locale.view;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import java.util.Locale;  
  6.   
  7. import javax.el.ELContext;  
  8.   
  9. import javax.el.ExpressionFactory;  
  10.   
  11. import javax.el.ValueExpression;  
  12.   
  13. import javax.faces.FacesException;  
  14. import javax.faces.application.ViewHandler;  
  15. import javax.faces.component.UIViewRoot;  
  16. import javax.faces.context.FacesContext;  
  17.   
  18. public class LocaleSettingViewHandler extends ViewHandler {  
  19.       
  20.     private ViewHandler base;  
  21.       
  22.     public LocaleSettingViewHandler() {  
  23.         super();  
  24.     }  
  25.       
  26.     public LocaleSettingViewHandler(ViewHandler base) {  
  27.         this.base = base;  
  28.     }  
  29.       
  30.     public Locale calculateLocale(FacesContext facesContext) {  
  31.         LocaleManager localeManager = LocaleManager.getInstance();  
  32.         //通过添加URL参数language来改变Locale  
  33.         String language =   
  34.             facesContext.getExternalContext().getRequestParameterMap().get("language");  
  35.         if(language != null) {  
  36.             localeManager.setCurrentLanguage(language);  
  37.             localeManager.setCurrentLocale(new Locale(language));  
  38.         }  
  39.         Locale locale = localeManager.getCurrentLocale();  
  40.         if(locale == null) {  
  41.             locale = Locale.getDefault();  
  42.         }  
  43.         return locale;  
  44.     }  
  45.       
  46.     public String calculateRenderKitId(FacesContext facesContext) {  
  47.         return base.calculateRenderKitId(facesContext);  
  48.     }  
  49.   
  50.     public UIViewRoot createView(FacesContext facesContext, String string) {  
  51.         return base.createView(facesContext, string);  
  52.     }  
  53.   
  54.     public String getActionURL(FacesContext facesContext, String string) {  
  55.         return base.getActionURL(facesContext, string);  
  56.     }  
  57.   
  58.     public String getResourceURL(FacesContext facesContext, String string) {  
  59.         return base.getResourceURL(facesContext, string);  
  60.     }  
  61.   
  62.     public void renderView(FacesContext facesContext,  
  63.                            UIViewRoot uIViewRoot) throws IOException,  
  64.                                                          FacesException {  
  65.         base.renderView(facesContext, uIViewRoot);  
  66.     }  
  67.   
  68.     public UIViewRoot restoreView(FacesContext facesContext, String string) {  
  69.         return base.restoreView(facesContext, string);  
  70.     }  
  71.   
  72.     public void writeState(FacesContext facesContext) throws IOException {  
  73.         base.writeState(facesContext);  
  74.     }  
  75.   
  76.     public void setBase(ViewHandler base) {  
  77.         this.base = base;  
  78.     }  
  79.   
  80.     public ViewHandler getBase() {  
  81.         return base;  
  82.     }  
  83. }  


    将LocaleSettingViewHandler配置到faces-config.xml中

    

    3.ADF界面设置
[html]  view plain copy
  1. <af:selectOneChoice id="soc1"  
  2.                                         label="#{res['login.language.label']}"  
  3.                                         value="#{localeManager.currentLanguage}"  
  4.                                         valueChangeListener="#{localeManager.changeLocale}"  
  5.                                         autoSubmit="true">  
  6.                       <af:selectItem label="简体中文" value="zh" id="si1"/>  
  7.                       <af:selectItem label="English" value="en" id="si2"/>  
  8.                     </af:selectOneChoice>  

特别说明

     还有种方法不用编写LocaleSettingViewHandler类的,只需要LocaleManager类即可,需要把LocaleManager类中的currentLocale和jspx中view标签的locale属性绑定。不过这样应该需要在每一个jspx页面中进行设置。可以根据需求灵活选择不同的方法。

[html]  view plain copy
  1. <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

转自:http://blog.csdn.net/ygj26/article/details/8286558

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值