1 5 package org.exoplatform.portal.faces.component; 6 7 import java.util.List ; 8 import java.util.Locale ; 9 import java.util.ResourceBundle ; 10 import javax.faces.context.FacesContext; 11 import javax.servlet.http.HttpServletRequest ; 12 import org.exoplatform.container.SessionContainer; 13 import org.exoplatform.faces.core.component.ComponentVisitor; 14 import org.exoplatform.faces.core.component.UIExoComponent; 15 import org.exoplatform.portal.PortalConstants; 16 import org.exoplatform.portal.session.PortalResources; 17 22 public class ChangeLocaleVisitor extends ComponentVisitor { 23 private ResourceBundle res_ ; 24 25 public ChangeLocaleVisitor() { 26 27 } 28 29 30 public void changeLocale(UIPortal uiPortal) { 31 String lang = uiPortal.getPortalConfigModel().getLocale() ; 32 FacesContext context = FacesContext.getCurrentInstance() ; 33 HttpServletRequest request = 34 (HttpServletRequest ) context.getExternalContext().getRequest(); 35 String langParam = request.getParameter(PortalConstants.LANGUAGE_PARAMETER) ; 36 if(langParam != null) lang = langParam ; 37 if (lang == null || lang.length() == 0) { 38 Locale requestLocale = request.getLocale() ; 39 lang = requestLocale.getLanguage() ; 40 } 41 changeLocale(uiPortal, lang) ; 42 } 43 44 public void changeLocale(UIPortal uiPortal, String locale) { 45 PortalResources presources = 46 (PortalResources) SessionContainer.getComponent(PortalResources.class) ; 47 presources.changeLocaleConfig(locale, uiPortal.getOwner()) ; 48 res_ = presources.getApplicationOwnerResource() ; 49 visit(uiPortal) ; 50 traverse(uiPortal.getRootContainer()); 51 } 52 53 public void changeLocale(UIPage uiPage) { 54 PortalResources presources = 55 (PortalResources) SessionContainer.getComponent(PortalResources.class) ; 56 res_ = presources.getLocaleConfig().getOwnerResourceBundle(uiPage.getOwner()) ; 57 traverse(uiPage) ; 58 } 59 60 public void changeLocale(UISinglePage comp, String locale) { 61 PortalResources presources = 62 (PortalResources) SessionContainer.getComponent(PortalResources.class) ; 63 presources.changeLocaleConfig(locale, "anonymous") ; 64 res_ = presources.getApplicationOwnerResource() ; 65 List children = comp.getChildren() ; 66 for(int i = 0; i < children.size() ; i++) { 67 UIExoComponent child = (UIExoComponent) children.get(i); 68 visit(child) ; 69 } 70 } 71 72 public void traverse(UIExoComponent component) { 73 visit(component) ; 74 if(component instanceof UIContainer) { 75 List children = component.getChildren() ; 76 for(int i = 0; i < children.size() ; i++) { 77 UIExoComponent child = (UIExoComponent) children.get(i); 78 traverse(child) ; 79 } 80 } 81 } 82 83 public void visit(UIExoComponent comp) { 84 if(comp instanceof UIBasicComponent) { 85 UIBasicComponent uiComp = (UIBasicComponent) comp ; 86 uiComp.changeLocale(res_) ; 87 } 88 } 89 } | Popular Tags |