Developing I18N Applications in JSF

17 September, 2018


Related Blogs

Screenshot_17 Welcome=welcome to en use users   Abc-it-IT. properties Welcome=welcome to it IT users   Abc-it-IN. properties Welcome=welcome to it IN users    FORM.HTML - <html> <body> <from method=”post ”action=”/i18n”> <table> <tr><td>select language</tr><td> select country </td></tr> <td> < select name=”language”> <option value=”en”> English</option> < option value=”it”> Italian</option > < option value=”hi”> Hindi</option > </select> </td> <td> <select name=”country”> <option value”us”> numerical<10prion> <option value”it”> Itali<10prion> <option value”in”> India<10prion> </select> </td></tr> <tr><td><input type=”submit” value=”SUBMITLY”> </td></tr></TABLE> </FROM></body></html>   I18N Sevlet java - Package com. soft; Import java. text.*; Import java. util.*; Import java. servlet. servlet exception; Import java. servlet. servlet annotation. Web servlet; Import java. servlet. http*; @web servlet(“/i18n”) Public class I18N servlet extends http servlet { Protected void dopost(http servlet request)request, Http servletResponse response throws servlet exception, IoException Try{ String language=request. getparameter(“language”); String country=request. getparameter(“country”); Locale I = new loaf(Language, country); Number format nf=number format. Getinstance(i); Date format df= data format.getDataInstance (o,l); Resource bundle rb= ResourceBundle .getBundle(“abc” ,l); Print writer out= response. GetWriter(); Out. println(“<html>”) Out. println(“<body>”) Out. println(“<h1>”) Out. println(“number:”+nf. format(1345.2345)+”<br>”); Out. println(“date:”+nf. format(new date())+”<br>”); Out. println(“message:”+rb. Get string(“welcome”)); Out. println(“</h1></body></html>”); }catch(Exception e){ e.print stack trace(); } } }  

  • If we use the above approach to provid II8N Support then every time user has to specify lang country, it is not suggestible in web applications.
  • To overcome the above problems, we have to provide I18N Support with out depending on the user, by getting client browser provided language setup
  • When we submit a request from a client browser then browser provided lang will be send to server in the from at “accept lang” request header, here it we get “accept-language” request header value then it is possible to identify from which local the request is coming, with this, we are able to provide I18N Support without specifying lang & country by the user
  • To get a particular request header value, we have to use the following method from servlet request, public string get header (String header-name)
  • Public class I18N Servlet extends Http servlet{ = IL; Protected void do Get(Http Servlet Request request, Http Servlef Response response) Throws servlet Exception, Ioexception{

Try{ String locate=request. get header(“accept-language”); String Tokenizer st= new string tokenizer(local, “-”); Int count= st. count tokens(); Local i=null; If (count==1){ String token(); I=new locale (c lang); { If(count==2){ String lang=st. next token(); String country=st. next token(); I=new locale (c lang, country); } If(count==3){ String lang=st. next token(); String country=st. next token(); String sys-var=st. next token(); I=new locale (lang, country), sys-var); } Number Format nt=number formal, get Insurance(l);   Screenshot_18   Abc-en. properties Header1=Durga software solutions Header2=Registration Form Sid=student Id Sname=student Name Semail=email id Smobile=contact no Button=registration Success=student registration success Failure=student registration failure   Abc-it. Properties Header1=Durga software solutions Header2=Registration Form Sid=student Id Sname=student Name Semail=email id Smobile=contact no Button=registration Success=student registration success Failure=student registration failure   Abc-te. Properties Header1= software solutions Header2=Darakstu patram Sid=vidyardu Gurdimpu snkya Sname= vidyardi peru Semail= vidyuth samachara Gurthimlu Smobile= sampradimpu sankya Button=Darakasthu Success=Vidyardi Darakastu safalamayinadhi Failure= Vidyardi Darakastu cifalamayinadhi   Index.html <!Doctype html public11413c11D+D HTML 4.01 Transitional //EN”””> <html> <head> <meta http-equiv=”Refresh” Content=”2:URL=http://local host:1010/I18napp/registrationform.dss”> <title>insert title here</title> </head> <body> <h1>Application IS Loading….</h1> </body> </html>   Registrationform.JSP <%@Page language=”java”content type=”text/html; Charset=ISO-8859-1”page encoding=”ISO-8859-1”> %> <%@taglib prefix=”f”url= -------”h”------ <!DOCTYPE___ ---------------------------dtd”> <html> <head> <meta http-equiy=”content-type ”content=”text/html; Charset=ISO-8859-1”> <title>insert title here</title > </head> <body> <f:view> <h:form> <h:panelGrid columns=”2”> <h:output text value=”#{msg.sid}”/> <h:input text id=”sid” value=”#{regBean.sid}”/> <h: output text value=”#{msg. sname}”/> <h:input text id=”s name” value=”#{reg Bean. S name }”/> <h: output text value=”#{msg. s name}”/> <h:input text id=”s email” value=”#{reg Bean. S email }”/> <h: output text value=”#{msg. s mobile}”/> <h:input text id=”s mobile” value=”#{reg Bean. S mobile}”/> <h:command button value =”#{msg.button}”action”#{ reg Bean. registration}”/> </h:panelGrid> </h:from> </f:view> </body></html>   Success. Jsp <%@page language=”java” content type=”text/html, Charset=ISO-8859-1”page encoding=”ISO-8859-“%> <%@taglib prefix=”f”> ”r” </Doc type> <html> <head> <meta http-equiy=”content-type ”content=”text/html; <title>Insert title here</ title > <head> <body> <f:view> <h1><h:output text value=”#{msg. success}”/></h1> </f:view> </body></html>      Failure.jsp <%@page __________ <html> <head> <meta http-equiy=”content-type <h1><h:output text value=”#{msg.failure}”/></her> </f:view> </body></html>   Registration Package com. soft; Import java. util locate; Import javax. faces. component. UIview Root; Import javax. faces. context. Faces context; Public class Registration bean{ Private string sid; Private string sname; Private string semail; Private string smobile; -   ----           --    - getter()setter() Public string registration(){ String status=””; If (sid. starts with(“DSS-”)){ Status=”success”; }else{ Status=”failure”; } Return status; } Public string get Registration form(){ Return” Registration form”; } }   Faces-config.xml <?xml version=”1.0”encoding=”UTF-8”?> <Faces-config Xmlns=”” Version=”1,2”> <managed-bean> < managed-bean -name>reg Bean< /managed-bean-name > < managed-bean -class>com. Durga soft. Registration Bean < managed-bean -scope>session < managed-bean> < navigation-rule> < from-view-id>/registration form.jsp</      > < navigation-case> < from-outcome>success</from-outcome> <to-view-id>/success.jsp</to-view-id> </navigation-case> <navigation-case> <from-outcome>failure<  > <to-view-> failure.jsp<  > </navigation-case> </navigation-rule> <navigation-rule> <from-view-id >/menu.jsp< > </navigation-case> <from-outcome >registration form</from-outcome> <to-view-> registration form.jsp</   > </navigation-case> </navigation-rule> <application> <local-config> <default-locale>en</default-locale> <supported-locale>it</    > <sup      >te</          > </locale-config> <resource-bundle> <base-name>abc</base-name> <var <msg</var>> </resource-bundle> </application> </faces-config>   Web.xml Same as prev. applications.      

About Author


Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills .