Showing posts with label Localization. Show all posts
Showing posts with label Localization. Show all posts

Saturday, March 20, 2010

ADF Faces RC: Change the Global Date Format of Your Application

To change the date format of your application, without necessarily affecting other locale settings like Language(localized text) ,or to established a standard global date format across different locales/countries, define the <formatting-locale> element of the trinidad-config.xml as illustrated below:
<?xml version="1.0" encoding="windows-1252"?>
<trinidad-config xmlns="http://myfaces.apache.org/trinidad/config">
  <formatting-locale>en-GB</formatting-locale>
</trinidad-config>
You can also define it with an EL that resolve to a java.util.Locale instead of a hard text (like en-GB)
<?xml version="1.0" encoding="windows-1252"?>
<trinidad-config xmlns="http://myfaces.apache.org/trinidad/config">
  <formatting-locale>#{userSession.formattingLocale}</formatting-locale>
</trinidad-config>
default (based on my browser's locale):

with formatting-locale set to "en-GB":

with formatting-locale set to "fr-CA":

with formatting-locale set to "ja":


Below is a quotation from section A.6.2.8 (Formatting Dates and Numbers Locale) of the Web User Interface Developer's Guide:
By default, ADF Faces and MyFaces Trinidad will format dates (including the first day of the week) and numbers in the same locale used for localized text (which by default is the locale of the browser). If, however, you want dates and numbers formatted in a different locale, you can use the element, which takes an IANA-formatted locale (for example, ja, fr-CA) as its value. The contents of this element can also be an EL expression pointing at an IANA string or a java.util.Locale object.
Two things that I have observed:
  1. The formatting-locale is case sensitive. For example, replacing "en-GB" with "en-gb" will NOT make localization work.
  2. When using EL, the expression should resolved to a java.util.Locale. An expression that resolves to a string like "en-GB" will throw an exception.

To learn how to bind configuration values to managed bean and/or how to configure general locale settings, please see the following post:
ADF Faces RC : Supplement on How to Configure Pages for an End User to Specify Locale at Runtime

Cheers!

Wednesday, March 10, 2010

ADF Faces RC : Supplement on How to Configure Pages for an End User to Specify Locale at Runtime

I am completely lost when I read section 21.4 (Configuring Pages for an End User to Specify Locale at Runtime) of the Web User Interface Developer's Guide. Correct me if I am wrong, but it seems that, that section is still in draft mode as of this blog-posting, and that motivated me to write this post to supplement it. This post references some other sections of the guide to as not recreate instructions here. This post assumes that you already know how to configure resource bundles and how to bind the component labels to those resource bundles (also available on the guide).

We can configure our application so that end users can specify the preferred locale at runtime without them going through the browser settings. To support this we need to do the following:

  1. Register the locales and resource bundles used in your application in the faces-config.xml file.
  2. Add a String locale attribute to a session scope managed bean that holds relevant information for the user session.
  3. Create a selectOneChoice that list the supported locales from step 1 above.
  4. Set the locale attribute of the <f:view> tag of our pages to the attribute the we created.

Register the locales and resource bundles used in your application in the faces-config.xml file.

Please see section 21.3.3 (How to Register Locales and Resource Bundles in Your Application) of the Web User Interface Developer's Guide.


Add a String locale attribute to a session scope managed bean that holds relevant information for the user session

We can add the locale attribute to an existing session scope managed bean that holds other user session information or create a new one if nothing is available. We use "session scope" here so that the locale information is applied to the whole session instead of the user setting the local on every page. Below is a sample managed bean class:

package soadev.view.managed;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
public class UserSession implements Serializable {
    private String locale;
  
    public void setLocale(String locale) {
        this.locale = locale;
    }

    public String getLocale() {
        if (locale == null) {
            locale = FacesContext.getCurrentInstance().getViewRoot()
                .getLocale().toString();
        }
        return locale;
    }

    //gets all the supported locals plus the default locale defined in the faces-config.xml
    //and add it to the list of selectItems
    public List<SelectItem> getLocales() {
        List<SelectItem> items = new ArrayList<SelectItem>();
        Application application = FacesContext.getCurrentInstance()
                            .getApplication();
        Iterator supportedLocales = application.getSupportedLocales();
        while (supportedLocales.hasNext()) {
            Locale locale = (Locale) supportedLocales.next();
            items.add(new SelectItem(locale.toString(), locale.getDisplayName(locale)));
        }
        Locale defaultLocale = application.getDefaultLocale();
        items.add(new SelectItem(defaultLocale.toString(), defaultLocale.getDisplayName(defaultLocale)));
        return items;
    }
}

Create a selectOneChoice that list the supported locales from step 1 above.

Add somewhere in the page or in a user preferrence popup a selectOneChoice like below:
<af:selectOneChoice label="Select Locale" id="pt_soc2"
                     value="#{userSession.locale}"
                     autoSubmit="true">
  <f:selectItems id="si10" value="#{userSession.locales}"/>
</af:selectOneChoice>

Set the locale attribute of the <f:view> tag of our pages to the attribute the we created.

<?xml version='1.0' encoding='UTF-8'?>
<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">
  <jsp:directive.page contentType="text/html;charset=UTF-8"/>
  <f:view locale="#{userSession.locale}">

Caveat: To simplify the illustration in this post, we added our locale attribute directly to the session scope managed bean, but in practice we usually create a sort of UserPrefence class to hold the user preferred locale plus other preferences like adf skin, rowBandingIntervals, and etc... I just thought that maybe creating a post on how to implement user preference maybe worth. Is it?


Cheers!