How to load locale from POST/GET parameter
This page will summarize how to load a resource bundle dynamically from a GET/POST parameter. 1.) In your mail xhtml template, make sure ...
https://www.czetsuyatech.com/2013/01/java-load-locale-from-request-param.html
This page will summarize how to load a resource bundle dynamically from a GET/POST parameter.
1.) In your mail xhtml template, make sure that you define f:view locale before the h:body tag:
2.) Create a LanguageBean managed-bean:
3.) The flags:
4.) Now in your receiver bean's PostConstruct annotated method, read the context parameter and set the locale:
5.) For the initial page load, the first time a request has been receive you need to manually reload the page for the locale to take effect:
1.) In your mail xhtml template, make sure that you define f:view locale before 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 Jan 17, 2013
*/
@Named
@SessionScoped
public class LanguageBean implements Serializable {
private static final long serialVersionUID = 1L;
private static Map supportedLocales;
private Logger log = LoggerFactory.getLogger(LanguageBean.class);
@Inject
private transient Locale currentLocale;
static {
supportedLocales = new LinkedHashMap<>();
supportedLocales.put("English", Locale.US);
supportedLocales.put("日本語 言語", Locale.JAPAN);
}
public Map getSupportedLocales() {
return supportedLocales;
}
public Locale getCurrentLocale() {
if (currentLocale == null || currentLocale.equals("")) {
currentLocale = Locale.US;
}
return currentLocale;
}
public void setCurrentLocale(Locale currentLocale) {
this.currentLocale = currentLocale;
}
public String switchLanguage(String language) {
log.debug("changing locale to={}", language);
if (language.equals("EN")) {
currentLocale = Locale.US;
} else {
currentLocale = Locale.JAPAN;
}
return FacesContext.getCurrentInstance().getViewRoot().getViewId() + "?faces-redirect=true";
}
public String getEntrustLocale() {
if (currentLocale == Locale.US) {
return "9";
} else {
return "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 in your receiver bean's PostConstruct annotated method, read the context parameter and set the locale:
@PostConstruct
public void init() throws RuntimeException, IOException {
ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
Map requestParameters = context.getRequestParameterMap();
// set locale
try {
languageBean.setCurrentLocale(LocaleUtils.toLocale(requestParameters.get("locale")));
} catch (IllegalArgumentException | NullPointerException e) {
languageBean.setCurrentLocale(Locale.JAPAN);
}
}
5.) For the initial page load, the first time a request has been receive you need to manually reload the page for the locale to take effect:
<script type="text/javascript">
window.onload = function() {
if (!window.location.hash) {
window.location = window.location + '#loaded';
window.location.reload();
}
}
</script>




Post a Comment