Reply To: Handling Multilanguage JSF Application with Rewrite

Splash Forums Rewrite Users Handling Multilanguage JSF Application with Rewrite Reply To: Handling Multilanguage JSF Application with Rewrite

#24012

zozan
Participant

Sure:
My LocaleBean.java:

@ManagedBean
@RequestScoped
public class LocaleBean{          
    private String lang;
    
    public Locale getLocale(){
        Locale locale;
        FacesContext context = FacesContext.getCurrentInstance();
        if (lang != null){
            locale = new Locale(lang);
            context.getViewRoot().setLocale(locale);            
        }else{
            if(context == null){
                locale = context.getApplication().getDefaultLocale();
            }else{
                locale = context.getViewRoot().getLocale();
                if(locale == null){
                    locale = context.getApplication().getDefaultLocale();
                }
            }
        }    
        return locale;
    }

    public String getLang() {
        return lang;
    }

    public void setLang(String lang) {
        this.lang = lang;
    }            
}

My Rewrite Configuration Class:

@Inject
    private LocaleBean localeBean;
    
    private static Map<String, Properties> langToViews = new HashMap<String, Properties>();
    private static Map<String, Properties> langToPaths = new HashMap<String, Properties>();
    
    private String getView(String lang, String path){
        Properties viewMap = getViewMap(lang);
        String view = viewMap.getProperty(path);
        return String.format("/%s.xhtml", view);
    }
    private Properties getViewMap(String lang){
        Properties props = langToViews.get(lang);
        if(props == null){
            props = new Properties();
            try{
                props.load(getClass().getResourceAsStream(String.format("text_%s.properties",lang)));
                langToViews.put(lang, props);
            } catch(IOException e){
                throw new RuntimeException(e);
            }
        }
        return props;
    }
    private String getPath(String lang, String view){
        Properties pathMap = getPathMap(lang);
        String path = pathMap.getProperty(view);
        return String.format("/%s/%s",lang, path);
    }
    private Properties getPathMap(String lang){
        Properties props = langToPaths.get(lang);
        if(props == null){
            props = new Properties();
            try{
                props.load(getClass().getResourceAsStream(String.format("text_%s.properties",lang)));
                langToPaths.put(lang, props);
            }catch (IOException e){
                throw new RuntimeException(e);
            }
        }
        return props;
    }

   @Override
   public Configuration getConfiguration(final ServletContext context)
   {
       
     return ConfigurationBuilder.begin()
             
             .addRule()
             .when(Direction.isInbound().and(Path.matches("/{lang}/{path}")))
             .perform(new HttpOperation(){
                 @Override
                 public void performHttp(HttpServletRewrite event, EvaluationContext context){                     
                     String lang = (String) Evaluation.property("lang").retrieve(event, context);
                     String path = (String) Evaluation.property("path").retrieve(event, context);
                     String view = getView(lang, path);
                     localeBean.setLang(lang);
                     Forward.to(view).perform(event, context);                
                 }
             })
             .addRule()
             .when(Direction.isOutbound().and(Path.matches("/{view}.xhtml")))
             .perform(new HttpOperation(){
                 @Override
                 public void performHttp(HttpServletRewrite event, EvaluationContext context){
                     String lang = localeBean.getLocale().getLanguage();
                     String view = (String) Evaluation.property("view").retrieve(event, context);
                     String path = getPath(lang, view);
                     Substitute.with(path).perform(event, context);
                 }
             })
             //Redirect to Home
            .addRule()             
            .when(Path.matches("/"))
            .perform(Redirect.temporary(context.getContextPath() + "/{lang}/Home"))
             .where("lang").bindsTo(El.property(localeBean.getLang()));                                         
   }    
   @Override
   public int priority()
   {
     return 10;
   }

Lnaguage Selection Part of My Home.xhtml:

<h:form>
         <h:selectOneMenu value="#{localeBean.lang}" onchange="submit()">
             <f:selectItem itemValue="en" itemLabel="English" />                                                                                                            
             <f:selectItem itemValue="ar" itemLabel="العربیە" />
         </h:selectOneMenu>
     </h:form> 

text.properties:

Home=Home
    Forum=Forum
    About=About
    Contact=Contact
    Login=Login

text_en.properties:

Home=Home
    Forum=Forum
    About=About
    Contact=Contact
    Login=Login

text_ar.properties:

Home=البیت
    Forum=منتدی
    About=معلومات عنا
    Contact=اتصل بنا
    Login=تسجیل الدخول
  • This reply was modified 8 years, 7 months ago by  zozan.
  • This reply was modified 8 years, 7 months ago by  zozan.
  • This reply was modified 8 years, 7 months ago by  zozan.
  • This reply was modified 8 years, 7 months ago by  zozan.