Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Lets say you have following two messages files</p> <pre><code> messages.properties messages_de.properties </code></pre> <p><strong>Setting the Application Locale</strong> <br>There are three ways of setting the Application Locale and I think <strong>you need the first one</strong> here.</p> <p><strong>1-You can let the browser choose the locale.</strong> </p> <p>Set the default and supported locales in <code>WEB-INF/faces-config.xml:</code></p> <pre><code>&lt;faces-config&gt; &lt;application&gt; &lt;locale-config&gt; &lt;default-locale&gt;en&lt;/default-locale&gt; &lt;supported-locale&gt;de&lt;/supported-locale&gt; &lt;/locale-config&gt; &lt;/application&gt; &lt;/faces-config&gt; </code></pre> <p>When a browser connects to your application, it usually includes an Accept-Language value in the HTTP header </p> <p><strong>2-You can set the locale programatically.</strong> </p> <p>Call the setLocale method of the UIViewRoot object:</p> <pre><code>UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot(); viewRoot.setLocale(new Locale("de")); </code></pre> <p><strong>3-You can set the locale for an individual page</strong> <br>By using the <code>f:view</code> element with a locale attribute—for example:</p> <pre><code>&lt;f:view locale="de"&gt; </code></pre> <p>The locale can be dynamically set:</p> <pre><code>&lt;f:view locale="#{user.locale}"/&gt; </code></pre> <p><br><strong>Declaring message bundles</strong><br> Now that the Locale is set you can use one of the following two ways to declare message bundles</p> <p><strong>1-Via faces-config</strong> The simplest way is to supply a file named faces-config.xml in the WEB-INF directory of your application, with the following contents:</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;faces-config xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" version="2.0"&gt; &lt;application&gt; &lt;resource-bundle&gt; &lt;base-name&gt;com.corejsf.messages&lt;/base-name&gt; &lt;var&gt;msgs&lt;/var&gt; &lt;/resource-bundle&gt; &lt;/application&gt; &lt;/faces-config&gt; </code></pre> <p><strong>2-At each JSF page that needs access it.</strong> Instead of using a global resource bundle declaration, you can add the f:loadBundle element to each JSF page that needs access to the bundle, like this:</p> <pre><code>&lt;f:loadBundle basename="com.corejsf.messages" var="msgs"/&gt; </code></pre> <p>In either case, the messages in the bundle are accessible through a map variable with the name msgs.</p> <p><strong>Showing appropriate label on button</strong> Now lets say default properties file i.e english has property</p> <pre><code>next=Next </code></pre> <p>and German has equivallent i.e</p> <pre><code>next=Weiter </code></pre> <p>And you have set the locale and declared mesg bundle you can access it to put the label on a command button like</p> <pre><code>&lt;h:commandButton value="#{msgs.next}"/&gt; </code></pre> <p><em>Above Answer is extracted and modified from <strong><a href="http://rads.stackoverflow.com/amzn/click/0137012896">Hortsmen Core Java Server Faces book</a></strong>.</em> </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload