Internationalization in JSF 2.0

I am wondering how internationalization works in jsf? I read a tutorial on coreservlets.com about this, but in my case it works a little differently. This tutorial says that I should use

FacesContext.getCurrentInstance().getViewRoot().setLocale(newLocale);

in actionListener (listener for changing locales), as well as bean support, must have a property getCurrentLocale()in order to use it in the tag <f:view>.

I have 2 properties files with messages (by default and with the specified language), they are registered in faces-config.xml. <f:view>I have only one page (index.xhtml)

<f:view locale="#{bean.locale}">
...
</f:view>

Also I have 2 buttons (with actionListener) for each locale. With bean support, I just change the current locale variable (don't use getViewRoot().setLocale(newLocale)). But locale changes for all pages (even if they don't have <f:view locale="#{bean.locale}">)

+5
source share
1 answer

Suppose you have two message files

    messages.properties
    messages_de.properties

Configuring application locales
There are three ways to install a Locale application, and I think you need the first one .

1 - You can allow the browser to select the locale.

Set the default values ​​and supported locales to WEB-INF/faces-config.xml:

<faces-config>
   <application>
      <locale-config>
         <default-locale>en</default-locale>
         <supported-locale>de</supported-locale>
      </locale-config>
  </application>
</faces-config>

When a browser connects to your application, it usually includes the Accept-Language value in the HTTP header

2. .

setLocale UIViewRoot:

UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
viewRoot.setLocale(new Locale("de"));

3 -
f:view locale, :

<f:view locale="de">

:

<f:view locale="#{user.locale}"/>



, Locale,

1-Via faces-config - faces-config.xml WEB-INF :

<?xml version="1.0"?>
<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">
   <application>
      <resource-bundle>
         <base-name>com.corejsf.messages</base-name>
         <var>msgs</var>
      </resource-bundle>
   </application>
</faces-config>

2 - JSF, . f: loadBundle JSF, , :

<f:loadBundle basename="com.corejsf.messages" var="msgs"/>

map msgs.

, i.e english

next=Next

i.e

next=Weiter

mesg, , ,

<h:commandButton value="#{msgs.next}"/>

Hortsmen Core Java Server Faces.

+22

All Articles