[OmniFaces utilities] The
parseLocale() method parses the given object representing the locale to a Locale object. If it is null, then return null. Else if it is already an instance of Locale, then just return it. Else if it is in pattern ISO 639 alpha-2/3, optionally followed by "_" and ISO 3166-1 alpha-2 country code, then split the language/country and construct a new Locale with it. Else parse it via Locale#forLanguageTag(String) and return it.Method
Usage
We will pass to Utils#parseLocale() a string of type xxx_yy. The xxx part represents the ISO 639 alpha-2/3 code, and the yy represents the ISO 3166-1 alpha-2 country code. For example for Japan we have, jpn_JP.
<h:form>
<h:outputText value="Country: #{localeBean.locale.country}"/>
<h:outputText value="Display Country:
#{localeBean.locale.displayCountry}"/>
<h:outputText value="Display Language:
#{localeBean.locale.displayLanguage}"/>
<h:commandButton value="Japan"
action="#{localeBean.localeAction('jpn_JP')}"/>
<h:commandButton value="Italy"
action="#{localeBean.localeAction('ita_IT')}"/>
<h:commandButton value="Lule
Sami" action="#{localeBean.localeAction('smj')}"/>
</h:form>
import
org.omnifaces.util.Faces;
import
org.omnifaces.util.Utils;
import
java.util.Locale;
...
@Named
@RequestScoped
public class
LocaleBean {
private Locale locale;
@PostConstruct
public void init(){
// init with the current locale
locale=Faces.getLocale();
}
public void localeAction(String code) {
locale = Utils.parseLocale(code);
}
public Locale getLocale() {
return locale;
}
}
The output will be:
// initial
Country: US
Display
Country: United States
Display
Language: English
// after
pressing Japan button
Country: JP
Display
Country: the Japan
Display
Language: Japanese
// after
pressing the Italy button
Country: IT
Display
Country: Italy
Display
Language: Italian
// after
pressing the Lule Sami button
Country:
Display
Country:
Display
Language: Lule Sami







JSF 2 Tutorials at www.mkyong.com
JavaServer Faces (JSF) Tutorial 

















Niciun comentariu :
Trimiteți un comentariu