luni, 29 iunie 2015

[OmniFaces utilities 2.1] Get all application resource bundles registered as <resource-bundle> in faces-config.xml


[OmniFaces utilities] The getResourceBundles() method returns all application resource bundles registered as <resource-bundle> in faces-config.xml. If there are no resource bundles registered, then this method just returns an empty map.

Method:
Usage:

Let's suppose that we have a resource bundle with "short" messages and a resource bundle with "full" messages:

Is quite simple, just check the content of the msgs.ShortMessages:

English and French (msgs.ShortMessages_en.properties and msgs.ShortMessages_fr.properties):


English and French (msgs.FullMessages_en.properties and msgs.FullMessages_fr.properties):


Now, in faces-config.xml, we declare both resource bundles, as below (for the "short" messages we have attached the var named, short, and for the "full" messages, the var named, full):

<application>
 <locale-config>
  <default-locale>en</default-locale>
  <supported-locale>en</supported-locale>
  <supported-locale>fr</supported-locale>
 </locale-config>
 <resource-bundle>
  <base-name>msgs.ShortMessages</base-name>
  <var>short</var>
 </resource-bundle>
 <resource-bundle>
  <base-name>msgs.FullMessages</base-name>
  <var>full</var>
  </resource-bundle>
 </application>

Now, you can use both resource bundles in the application views by indicating the proper var. For example, a dummy usage is below:

<h:outputText value="#{short['NAME']}"/>
<h:outputText value="#{short['SURNAME']}"/>
<h:outputText value="#{full['NAME']}"/>
<h:outputText value="#{full['SURNAME']}"/>

Now, programmatically speaking, we can access these resource bundles via Faces#getResourceBundles(), as below:

Map<String, ResourceBundle> resourcebundles = Faces.getResourceBundles();
for (Map.Entry<String, ResourceBundle> entry : resourcebundles.entrySet()) {
     // entry.getKey() - this is the var name (short, full)
     // entry.getValue().getString("NAME")); - for short var
     // entry.getValue().getString("SURNAME")); - for full var
}

Niciun comentariu:

Trimiteți un comentariu