There are numerous posts on how to implement Internationalization using properties file. Here my post explains how to implement it using "List Resource Bundle".
Internationalization provides easy adaptation of the
application to local languages.
As a simple example, people accessing the application in US will see the
content in English (Assuming the browser language is set to English) and People
in Italy will see the content displayed in Italian.
This can be achieved by not hardcoding the label String in
pages, rather fetching it from resource Bundles.
There are 3 types of Resource Bundles:
- .properties files
- .xliff files
- .java files ( List Resource Bundles)
Here we going to discuss the step by step implementation of
third type.
Creating and using List Resource Bundles.
a) Create a project and set the project properties.
- Create a ADF Project.
- Right Click on the Project and Open the Project Properties, Select Resource Bundle from the list in the left side.
- Radio button “One Bundle per project” will be selected by default and a default value will be already entered for the Resource Bundle Name. i.e ViewControllerBundle . Change the name according to your project naming standards.
- Select “List Resource Bundle” in the “Resource Bundle Type” dropdown. Click OK.
b) Create the Page /Page Fragment and Resource Bundles
- Create / locate a Jspx Page / Jsff Page Fragment where you want to add the String from the ResourceBundle, add the required component to the page ( For Ex, Output Text).
- Open the property inspector and click the icon beside the value attribute field of OutputText to open the property Menu.
- In this property menu , select “ Select Text Resource” and a popup will be opened, Fill in the “Display Value” field as the String which need to be displayed and the “Key” field will be automatically filled up, Optionally Enter the “Description”. Click OK.
- A <Resource Bundle Name>.java file will be automatically created and the Key –Value pair array will be created in the java class.
<Resource Bundle Name> is the name which you gave in the project properties, “Default Resource Bundle Name” field as explained earlier.
The Java class Extends the class ListResourceBundle.java and implements the abstract method getContents(). - This will be the default Bundle for the project and if you need other Bundles to support other languages, you need to create .java files with the name format <Resource Bundle Name>_<Lang Code>.java
For ex. ViewControllerBundle_it.java , “it” means “Italian” here.
This java file need to extend the Base Class, Default Resource Bundle ( Here its ViewControllerBundle.java) , Add the Key-Value pair array with “Key” the same as Base Resource Bundle Key and the “Value” refers to the language specific String. - Now the af:outputText in the page looks like below. Its value is referenced from the Resource Bundle and it is dependent on the language preference of the browser.
If English,
it will display as “My Name”.
If Italian,
it will display as “Il mio nome”.
c) Configure the faces-config.xml
- Next step is register the Locale and Resource Bundle in faces-config.xml. Open the faces-config.xml in the path WEB-INF/faces-config.xml in your Viewcontroller project.
- In the Overview tab, click Application. ( Image Below )
Under the Resource Bundle Section add a row and enter the Fully Qualified Resource Bundle Name as the “Base Name” and a String say “res” in “Var Name”.
Using the String "res" we can access the strings in the bundle using an EL #{res['MY_NAME']}
Under the Locale Config Section , Enter the Default Locale . Here I enter “en” (English) and “it”(Italian) as Supported Locale because we have two .java resource bundle files, ViewControllerBundle.java( This is the default bundle and for English) and ViewControllerBundle_it.java (This is for Italian language, if you change the browser language settings) . - In the same way you can add as many ViewControllerBundle_<lang> .java files and corresponding supported locale entries in faces-config.xml for supporting different languages.
d) Changing the Language preference of the browser and running the application
- Change / Set the language preference in the browser. (Image Below)
Here I change the preference of IE Browser to set English as the primary language by moving it to top. Then the String will be fetched from the default bundle for English.
And also I add a new language Italian.
If I move Italian language up and make it as a primary language, Then the String will be fetched from the <Resource Bundle Name>_it.java which is for Italian. - This is the Screen when we set the Primary Language as “English” and run the application
- This is the Screen when we set the Primary Language as “Italian” and run the application.
No comments:
Post a Comment