duminică, 15 martie 2015

[OmniFaces utilities (2.0)] Get/set locales (default/supported)


[OmniFaces utilities] The getDefaultLocale() method returns the default locale, or null if there is none.
[OmniFaces utilities] The getSupportedLocales() method returns a list of all supported locales on this application, with the default locale as the first item, if any. This will return an empty list if there are no locales defined in faces-config.xml.
[OmniFaces utilities] The getLocale() method returns the current locale. If the locale set in the JSF view root is not null, then return it. Else if the client preferred locale is not null and is among supported locales, then return it. Else if the JSF default locale is not null, then return it. Else return the system default locale.
[OmniFaces utilities] The setLocale() method set the locale of the current view, which is to be used in localizing of the response.

Method Faces#getDefaultLocale() - returns the default locale, or null if there is none
See also: Faces#getContext()

 Method Faces#getSupportedLocales()  - returns a list of all supported locales on this application, with the default locale as the first item, if any
See also: Faces#getContext()

 Method Faces#getLocale()  - returns the current locale
See also: Faces#getContext()

 Method Faces#setLocale() set the locale of the current view
See also: Faces#getContext()
Usage:

The Faces#getDefaultLocale() will return the default locale found in faces-config.xml (inspect the <default-locale> tag content). Below you can see two examples:

·         no default locale specified (returns null)

// 'defaultlocale' will be 'null'
Locale defaultlocale = Faces.getDefaultLocale();

·         default locale specified in faces-config.xml (returns fr)

<application>            
 <locale-config>
  <default-locale>fr</default-locale>
  <supported-locale>fr</supported-locale>
  <supported-locale>en</supported-locale>          
  <supported-locale>de</supported-locale>     
 </locale-config>                            
 <resource-bundle>                       
  <base-name>players.msgs.PlayerMessages</base-name>
  <var>msg</var>
 </resource-bundle>                                                                                         
</application>

// 'defaultlocale' will be 'fr'
Locale defaultlocale = Faces.getDefaultLocale();

The Faces#getSupportedLocales() will returns a list of all supported locales on this application, with the default locale as the first item, if any. Below you can see two examples:

·         no supported locale specified:

// 'supportedlocales' will be an empty list
List<Locale> supportedlocales = Faces.getSupportedLocales();

·         supported locale specified in faces-config.xml (returns  [fr, de, en])

<application>            
 <locale-config>
  <default-locale>fr</default-locale>
  <supported-locale>fr</supported-locale>
  <supported-locale>en</supported-locale>          
  <supported-locale>de</supported-locale>     
 </locale-config>                            
 <resource-bundle>                       
  <base-name>players.msgs.PlayerMessages</base-name>
  <var>msg</var>
 </resource-bundle>                                                                                         
</application>

// 'supportedlocales' will be a list that contains  [fr, de, en]
List<Locale> supportedlocales = Faces.getSupportedLocales();

The Faces#setLocale() set the locale of the current view. This is the programmatically approach of <f:view locale="locale">. For example, we can set the locale as de:

// programmatic approach for <f:view locale="de">  
Locale delocale = new Locale("de");
Faces.setLocale(delocale);

The Faces#getLocale() invoke the sequence of methods, UIViewRoot#getLocale(), ExternalContext#getRequestLocale(), Application#getDefaultLocale(), Locale#getDefault(). When the locale is found, it is returned. If none of this methods returns a valid locale, then return null. Let's see some examples:
·         no locales were specified

// returns the locale set in the view by JSF, UIViewRoot#getLocale() (e.g. en_US)
Locale locale = Faces.getLocale();

·         default and supported locales specified in faces-config.xml and current locale was set programmatically (or via <f:view locale="de">) as de:

<application>            
 <locale-config>
  <default-locale>fr</default-locale>
  <supported-locale>fr</supported-locale>
  <supported-locale>en</supported-locale>          
  <supported-locale>de</supported-locale>     
 </locale-config>                            
 <resource-bundle>                       
  <base-name>players.msgs.PlayerMessages</base-name>
  <var>msg</var>
 </resource-bundle>                                                                                         
</application>

// programmatic approach for <f:view locale="de">  
Locale delocale = new Locale("de");
Faces.setLocale(delocale);

// returns the locale set in the view, UIViewRoot#getLocale() (e.g. de)
Locale locale = Faces.getLocale();

Niciun comentariu:

Trimiteți un comentariu