Loading locale from POST/GET parameter
Tech-Today

Loading 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 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>




- Social Login Using Rest Fb
This is an implementation tutorial on how we can use REST FB to enable facebook social login on our web application. Basically, it's a project created from javaee7-war template. To run this app you need to set up a Facebook application with callback...

- Do Some Initialization Work After The Web Application Initialization Process
We can achieve this by using a webListener. See code below: package com.czetsuya; import java.text.MessageFormat; import java.util.ResourceBundle; import javax.inject.Inject; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener;...

- How To Call A Javaee Rest Web Service With Basic Authentication Using Jquery Ajax
I don't really remember when I coded it, nor where I got it but I'm writing it here for future use :-) Below is the code I use to test CORS, http://en.wikipedia.org/wiki/Cross-origin_resource_sharing. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>...

- How To Load Property File From Glassfish Config's Folder
In seam you can define a component class in components.xml that will load the properties from the JBOSS_CONFIG folder. Add the ff lines: <component name="paramBean" class="com.ipil.PropertyBean" scope="application" auto-create="true" startup="true">...

- How To Auto Wire A Spring Bean From A Jsf Managed Bean In Liferay Portlet
There are 2 ways I know to auto-wire a spring bean into a jsf managed bean: 1.) Is through WebApplicationContext, invoke during jsf managed bean constructor: Below are the most vital files to perform this action: web.xml <?xml version="1.0"?>...



Tech-Today








.