java - JSF 2.0 set locale throughout session from browser and programmatically -
this question has answer here:
how detect locale application based on initial browser request , use throughout browsing session untill user changes locale , how force new locale through remaining session?
create session scoped managed bean follows:
@managedbean @sessionscoped public class localemanager { private locale locale; @postconstruct public void init() { locale = facescontext.getcurrentinstance().getexternalcontext().getrequestlocale(); } public locale getlocale() { return locale; } public string getlanguage() { return locale.getlanguage(); } public void setlanguage(string language) { locale = new locale(language); facescontext.getcurrentinstance().getviewroot().setlocale(locale); } }
to set current locale of views, bind <f:view>
of master template.
<f:view locale="#{localemanager.locale}">
to change it, bind <h:selectonemenu>
language options.
<h:form> <h:selectonemenu value="#{localemanager.language}" onchange="submit()"> <f:selectitem itemvalue="en" itemlabel="english" /> <f:selectitem itemvalue="nl" itemlabel="nederlands" /> <f:selectitem itemvalue="es" itemlabel="espaƱol" /> </h:selectonemenu> </h:form>
to improve seo of internationalized pages (otherwise marked duplicate content), bind language <html>
well.
<html lang="#{localemanager.language}">
Comments
Post a Comment