Internationalization in JSF 2.0

后端 未结 1 1460
灰色年华
灰色年华 2021-02-02 04:04

I\'m wondering how internationalization works in jsf? I have read tutorial on coreservlets.com about it, but in my case it works slightly differently. In that tutorial said tha

1条回答
  •  遥遥无期
    2021-02-02 04:34

    Lets say you have following two messages files

        messages.properties
        messages_de.properties
    

    Setting the Application Locale
    There are three ways of setting the Application Locale and I think you need the first one here.

    1-You can let the browser choose the locale.

    Set the default and supported locales in WEB-INF/faces-config.xml:

    
       
          
             en
             de
          
      
    
    

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

    2-You can set the locale programatically.

    Call the setLocale method of the UIViewRoot object:

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

    3-You can set the locale for an individual page
    By using the f:view element with a locale attribute—for example:

    
    

    The locale can be dynamically set:

    
    


    Declaring message bundles
    Now that the Locale is set you can use one of the following two ways to declare message bundles

    1-Via faces-config The simplest way is to supply a file named faces-config.xml in the WEB-INF directory of your application, with the following contents:

    
    
       
          
             com.corejsf.messages
             msgs
          
       
    
    

    2-At each JSF page that needs access it. 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:

    
    

    In either case, the messages in the bundle are accessible through a map variable with the name msgs.

    Showing appropriate label on button Now lets say default properties file i.e english has property

    next=Next
    

    and German has equivallent i.e

    next=Weiter
    

    And you have set the locale and declared mesg bundle you can access it to put the label on a command button like

    
    

    Above Answer is extracted and modified from Hortsmen Core Java Server Faces book.

    0 讨论(0)
提交回复
热议问题