jsf - Domain based I18N with JSF2.0 -


i have implement i18n based on domain using jsf2.

for example if domain is

www.bookstore.com      --> should go english site www.bookstore.com.cn   --> should go chinese site www.bookstore.co.jp    --> should go japanese site 

we have properties files proper translation. when change browser language/locale, able see translated content. don't need behavior actually. setting locale based on domain in filter, seems not working. here code, wrong in code, problem ?

authenticationfilter.java

public class authenticationfilter implements filter {      private static log log = logfactory.getlog(authenticationfilter.class);       @override     public void init(filterconfig config) throws servletexception {     }      @override     public void dofilter(servletrequest request, servletresponse response,filterchain chain) throws ioexception, servletexception {          request.setcharacterencoding("utf-8");         response.setcharacterencoding("utf-8");          httpservletrequest httprequest = (httpservletrequest) request;         httpservletresponse httpresponse = (httpservletresponse) response;          facescontext facescontext = facesutil.getfacescontext(httprequest, httpresponse);         facescontext.getexternalcontext().setresponsecharacterencoding("utf-8");                  locale requestlocale = localeutil.getlocalefromdomain(httprequest);                  if(requestlocale == null){             requestlocale = localeutil.getdefault();         }          facescontext.getviewroot().setlocale(requestlocale);         chain.dofilter(httprequest, httpresponse);     }      @override     public void destroy() {     }  } 

facesutil.java

public class facesutil {    public static facescontext getfacescontext(httpservletrequest request, httpservletresponse response) {     facescontext facescontext = facescontext.getcurrentinstance();     if (facescontext == null) {       lifecyclefactory lifecyclefactory = (lifecyclefactory) factoryfinder.getfactory(factoryfinder.lifecycle_factory);       lifecycle lifecycle = lifecyclefactory.getlifecycle(lifecyclefactory.default_lifecycle);       facescontextfactory contextfactory = (facescontextfactory) factoryfinder.getfactory(factoryfinder.faces_context_factory);       facescontext = contextfactory.getfacescontext(request.getsession().getservletcontext(), request, response, lifecycle);       uiviewroot view = facescontext.getapplication().getviewhandler().createview(facescontext, "");       facescontext.setviewroot(view);       facescontextwrapper.setcurrentinstance(facescontext);     }     return facescontext;   }    // wrap protected facescontext.setcurrentinstance() in inner class.   private static abstract class facescontextwrapper extends facescontext {     protected static void setcurrentinstance(facescontext facescontext) {       facescontext.setcurrentinstance(facescontext);     }   }  } 

localeutil.java

public class localeutil {      public static locale getlocalefromdomain(httpservletrequest request){             locale locale=locale.getdefault();             string domain = request.getheader("host");             if(domain.contains(".com.cn"))            {                locale = new locale(locale.simplified_chinese.getlanguage(), locale.china.getcountry());             }else if(domain.contains(".co.jp"))            {                locale = new locale(locale.japanese.getlanguage(), locale.japan.getcountry());             }else if(domain.contains(".co.kr"))            {                locale = new locale(locale.korean.getlanguage(), locale.korea.getcountry());             }else if(domain.contains(".com.tw"))            {               locale = new locale(locale.chinese.getlanguage(), locale.taiwan.getcountry());                          }            return locale;         }              public static locale getdefault() {         locale defaultlocale = new locale("en", "us");         return defaultlocale;     }      } 

faces-config.xml

<?xml version="1.0" encoding="utf-8"?> <faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"     xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"     xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">      <application>         <locale-config>             <default-locale>en</default-locale>             <supported-locale>en_us</supported-locale>             <supported-locale>zh</supported-locale>             <supported-locale>zh_cn</supported-locale>             <supported-locale>zh_tw</supported-locale>             <supported-locale>ja</supported-locale>             <supported-locale>ja_jp</supported-locale>         </locale-config>         <resource-bundle>             <base-name>com.bookstore.component.bookstoretexthandler</base-name>             <var>msg</var>         </resource-bundle>     </application>  </faces-config>  

bookstoretexthandler.java

public class bookstoretexthandler extends resourcebundle {      protected static final string bundle_name = "/var/app/conf/bookstore";     protected static final string bundle_extension = "properties";     protected static final control utf8_control = new utf8control();      public bookstoretexthandler() {         locale locale = facescontext.getcurrentinstance().getviewroot().getlocale();         setparent(resourcebundle.getbundle(bundle_name, locale, utf8_control));     }      @override     protected object handlegetobject(string key) {         return parent.getobject(key);     }      @override     public enumeration<string> getkeys() {         return parent.getkeys();     }      protected static class utf8control extends control {         @override         public resourcebundle newbundle(string basename, locale locale, string format, classloader loader, boolean reload)                 throws illegalaccessexception, instantiationexception, ioexception {              string bundlename = tobundlename(basename, locale);             string resourcename = toresourcename(bundlename, bundle_extension);             resourcebundle bundle = null;              inputstreamreader reader = null;             fileinputstream fis = null;             try {                 file file = new file(resourcename);                  if (file.isfile()) { // checks existance                     fis = new fileinputstream(file);                     reader = new inputstreamreader(fis, charset.forname("utf-8"));                     bundle = new propertyresourcebundle(reader);                 }             } {                 ioutils.closequietly(reader);                 ioutils.closequietly(fis);             }             return bundle;         }     }  } 

i have bookstore.properties, bookstore_en.properties, bookstore_en_us.properties, bookstore_zh.properties, bookstore_zh_cn.properties, bookstore_zh_tw.properties, bookstore.properties_ja, bookstore_ja_jp.properties, files in /var/app/conf/ location.

intropage.xhtml

<ui:composition lang="en" xmlns="http://www.w3.org/1999/xhtml"                 xmlns:f="http://java.sun.com/jsf/core"                 xmlns:h="http://java.sun.com/jsf/html"                 xmlns:c="http://java.sun.com/jsp/jstl/core"                 xmlns:fn="http://java.sun.com/jsp/jstl/functions"                 xmlns:ui="http://java.sun.com/jsf/facelets">      <div class="frame">         <h1>#{msg['bookstore.intro.title']}</h1>             </div>      <div>         <p style="line-height: 1.6em; font-family: verdana, arial, sans-serif;">             <h:outputtext value="#{msg['bookstore.intro.paragraph.1']}" escape="false"/>         </p>         <p style="line-height: 1.6em; font-family: verdana, arial, sans-serif;">             <h:outputtext value="#{msg['bookstore.intro.paragraph.2']}" escape="false"/>         </p>         <p style="line-height: 1.6em; font-family: verdana, arial, sans-serif; font-weight: bold;">             <h:outputtext value="#{msg['bookstore.intro.paragraph.3']}" escape="false"/>         </p>     </div>  </ui:composition> 

when trying different domains [ have changed host file locally ], when debug filter code, giving correct locale based on domain. don't know why getting english content. if change browser language, can see translated content. wrong in code ?


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -