My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

.

.

.

.

.

.

.

.


[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

sâmbătă, 27 februarie 2016

[OmniFaces utilities (2.3)] Parsing the given object representing the locale to a Locale object


[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

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

Visitors Starting 4 September 2015

Locations of Site Visitors