1.) In your postal service xhtml template, brand certain that y'all define f:view locale earlier the h:body tag:
<f:view locale="#{languageBean.currentLocale}" />
2.) Create a LanguageBean managed-bean:
package com.sido.mg.mbean; import java.io.Serializable; import java.util.LinkedHashMap; import java.util.Locale; import java.util.Map; import javax.enterprise.context.SessionScoped; import javax.faces.context.FacesContext; import javax.inject.Inject; import javax.inject.Named; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author Edward P. Legaspi * @since January 17, 2013 */ @Named @SessionScoped populace shape LanguageBean implements Serializable { person static concluding long serialVersionUID = 1L; person static MapsupportedLocales; person Logger log = LoggerFactory.getLogger(LanguageBean.class); @Inject person transient Locale currentLocale; static { supportedLocales = novel LinkedHashMap<>(); supportedLocales.put("English", Locale.US); supportedLocales.put("日本語 言語", Locale.JAPAN); } populace Map getSupportedLocales() { provide supportedLocales; } populace Locale getCurrentLocale() { if (currentLocale == zilch || currentLocale.equals("")) { currentLocale = Locale.US; } provide currentLocale; } populace void setCurrentLocale(Locale currentLocale) { this.currentLocale = currentLocale; } populace String switchLanguage(String language) { log.debug("changing locale to={}", language); if (language.equals("EN")) { currentLocale = Locale.US; } else { currentLocale = Locale.JAPAN; } provide FacesContext.getCurrentInstance().getViewRoot().getViewId() + "?faces-redirect=true"; } populace String getEntrustLocale() { if (currentLocale == Locale.US) { provide "9"; } else { provide "28"; } } }
3.) The flags:
<ul class="right"> <li><p:commandLink immediate="true" action="#{languageBean.switchLanguage('EN')}"> <h:graphicImage library="img" name="flags/us.png"></h:graphicImage> </p:commandLink></li> <li><p:commandLink immediate="true" action="#{languageBean.switchLanguage('JP')}"> <h:graphicImage library="img" name="flags/jp.png"></h:graphicImage> </p:commandLink></li> </ul>
4.) Now inwards your receiver bean's PostConstruct annotated method, read the context parameter as well as educate the locale:
@PostConstruct populace void init() throws RuntimeException, IOException { ExternalContext context = FacesContext.getCurrentInstance().getExternalContext(); MaprequestParameters = context.getRequestParameterMap(); // educate locale endeavor { languageBean.setCurrentLocale(LocaleUtils.toLocale(requestParameters.get("locale"))); } choose handgrip of (IllegalArgumentException | NullPointerException e) { languageBean.setCurrentLocale(Locale.JAPAN); } }
5.) For the initial page load, the offset fourth dimension a asking has been have y'all withdraw to manually reload the page for the locale to accept effect:
<script type="text/javascript"> window.onload = function() { if (!window.location.hash) { window.location = window.location + '#loaded'; window.location.reload(); } } </script>
0 komentar:
Please comment if there are any that need to be asked.