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 plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章