Here we go from dutch to english.
In this example I am using resourcebundles to change the values / labels to the right language. You can use properties or java files for the resourcebundles. To use these resourcebundles in our application we have to configure this in the faces-config.xml or do this in the JSF page.
The JSF page I made for this demo
<?xml version='1.0' encoding='windows-1252'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:af="http://xmlns.oracle.com/adf/faces/rich"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<c:set var="viewcontrollerBundle"
value="#{adfBundle['nl.whitehorses.locale.view.ViewControllerBundle']}"/>
<jsp:directive.page contentType="text/html;charset=windows-1252"/>
<f:view>
<af:document>
<af:messages/>
<af:form>
<af:panelFormLayout>
<af:panelGroupLayout layout="horizontal">
<af:selectOneChoice label="Locale" id="choice"
valueChangeListener="#{UserPreferences.localeChangeListener}"
value="#{UserPreferences.language}"
autoSubmit="true">
<af:selectItem label="Dutch" value="nl"/>
<af:selectItem label="English" value="en"/>
</af:selectOneChoice>
<af:commandButton text="Refresh"/>
<af:spacer width="10" height="10"/>
<af:commandButton text="Dutch" disabled="#{UserPreferences.language == 'nl'}"
actionListener="#{UserPreferences.localeChangeListener}">
<af:setActionListener to="#{UserPreferences.language}" from="nl"/>
</af:commandButton>
<af:commandButton text="English" disabled="#{UserPreferences.language == 'en'}"
actionListener="#{UserPreferences.localeChangeListener}">
<af:setActionListener to="#{UserPreferences.language}" from="en"/>
</af:commandButton>
</af:panelGroupLayout>
<af:spacer width="10" height="20"/>
<af:outputLabel value="#{viewcontrollerBundle.OUTPUT}"/>
<af:outputLabel value="#{msg.OUTPUT}"/>
<af:outputLabel value="#{jmsg.java_output}"/>
<af:outputLabel value="100000">
<af:convertNumber/>
</af:outputLabel>
<af:selectOneChoice label="Label" value="2">
<af:forEach items="#{bindings.lovDataResult.rangeSet}" var="li">
<af:selectItem label="#{li.lovLabel}" value="#{li.lovValue}"/>
</af:forEach>
</af:selectOneChoice>
</af:panelFormLayout>
</af:form>
</af:document>
</f:view>
</jsp:root>
I use the c:set component in the JSF page to define the resourcebundle, but when you want to use this resourcebundle in more then one page you also can define this in the faces-config.xml.
Here is my faces-config.xml which I use in this demo.
<?xml version="1.0" encoding="windows-1252"?>
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee">
<application>
<default-render-kit-id>oracle.adf.rich</default-render-kit-id>
<locale-config>
<default-locale>nl</default-locale>
<supported-locale>nl</supported-locale>
<supported-locale>en</supported-locale>
</locale-config>
<resource-bundle>
<base-name>nl.whitehorses.locale.view.locale</base-name>
<var>msg</var>
</resource-bundle>
<resource-bundle>
<base-name>nl.whitehorses.locale.view.JavaLocale</base-name>
<var>jmsg</var>
</resource-bundle>
</application>
</faces-config>
In the faces-config we can define what our default locale is. The resourcebundle without an underscore and country code should contain the labels which matches the default locale. In this example I also support english so I need to create a xxxx_en.properties or xxx_en.java file which contain the english values. After this we need define the main resourcebundles ( without _en ) and under which variable name it is known.
Here you see an project overview of the different resourcebundles I used in this example
To format number values with the right decimal and number grouping separator we can use the trinidad-config.xml. Here we can use an EL expression to define the separator for the right Locale.
<?xml version="1.0" encoding="windows-1252"?>
<trinidad-config xmlns="http://myfaces.apache.org/trinidad/config">
<skin-family>blafplus-rich</skin-family>
<number-grouping-separator>#{UserPreferences.language=='nl' ? '.' : ','}</number-grouping-separator>
<decimal-separator>#{UserPreferences.language=='nl' ? ',' : '.'}</decimal-separator>
</trinidad-config>
To see the effect of these separators we need to use af:convertNumber on an inputtext or an outputtext.
In the JSF and the trinidad-config.xml I use this backing bean to set or read the selected language.
In this backing bean I also call an ADF MethodAction which refreshes the listbox with the right locale values.
package nl.whitehorses.bean;
import java.io.Serializable;
import java.util.Locale;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.binding.OperationBinding;
public class UserPreferences implements Serializable {
private String language;
public UserPreferences() {
language = Locale.getDefault().getLanguage();
}
private void changeLocale(String language){
System.out.println("changeLocale "+language);
this.language = language;
Locale newLocale = new Locale(this.language);
FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().setLocale(newLocale);
// refresh lov method action
DCBindingContainer bc = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
OperationBinding lovOper = bc.getOperationBinding("getLovData");
lovOper.execute();
}
public void localeChangeListener(ValueChangeEvent valueChangeEvent) {
changeLocale(valueChangeEvent.getNewValue().toString());
}
public void localeChangeListener(ActionEvent actionEvent) {
changeLocale(this.language);
}
public void setLanguage(String language) {
System.out.println("setLanguage "+language);
this.language = language;
}
public String getLanguage() {
return language;
}
}
Here an example of a java resourcebundle.
package nl.whitehorses.locale.view;
import java.util.ListResourceBundle;
public class JavaLocale_en extends ListResourceBundle {
public Object[][] getContents() {
return contents;
}
private Object[][] contents =
{ { "java_output", "java output" }
, { "java_output2","java output 2" }, };
}
Here you can download my 11g workspace