Thursday, August 18, 2011

Internationalization/Localization From the Start

The title says it all. "...From the Start". Don't wait until someone wants to know if your application works with a foreign language to do the work. Retrofitting and removing strings etc. will drive you nuts and will be very unproductive. Instead, put the processes in place at the start so that your answer to their question is "Yes, of course, always has...". Here's some gory details on how, and how nice GWT is in doing the hard work for you:


Determining Locale


Before any of the following code examples will take affect, the system must know what locale it should be running against. This is done by adding the languages you support in your gwt.xml file. For example, to support English add:

<extend-property name="locale" values="en"/>


To add support for another language, say Spanish, add:


<extend-property name="locale” values="es”/>



To include the internationalization code into our Gwt application, we must add:
<inherits name="com.google.gwt.i18n.I18N”/> to our gwt.xml file.


The system will look at the locale running locally on the machine running our application and will tie that to the locale property. If the system sees “es” as the locale, then the application will automatically pick up the Spanish files. If we had created OurStrings.properties and OurStrings_es.properties, then the first file is used by default and the second is used if the locale matches “es”.





Internationalization


To implement an international scheme, we will use static string substitution to make the coding easier and to make the compiled javascript smaller and quicker. The process is as follows:


1. Create a properties file - AppMessages.properties, in the main client folder. This file has ids to strings such as

fileMenu = File

editMenu = Edit

helpMenu = help


2. Create a java interface class of the same naming - AppMessages.java in the main client folder. This file implements the properties file strings:


public interface AppMessages extends Messages
{
String fileMenu();

String editMenu();

String helpMenu();

}

there must be a method for each entry in the properties file.


3. In the main class that runs at startup, in the onModuleLoad() method, construct the class as:


AppMessage msgs = (AppMessages) GWT.create(AppMessages.class));


4. Now you can use the msgs class to find strings such as:


msgs.fileMenu(); which will return “File”.

msgs.editMenu(); which will return "Edit" and so on.



To pass parameters on strings, the properties file would have:
hello = hello {0}


The interface would be:
String hello(String who);


The call would be:
msgs.hello(“Eric”); which will return “hello Eric”.




Localization


Localization deals with the display of dates, times and numbers. Dates displayed in English using month/day/year are displayed differently in other countries, sometimes like day/month/year. So, 5/24/2001 in the US would be displayed as 24/5/2001 in England. Times should be displayed according to the timezone and numbers displayed according to the country formatting where decimal points and comma’s are sometimes intermixed. Gwt has automatic classes built in to support localization. Yes automatic. Based on the locale for the application, the dates, times, currencies will be translated for you:


Data today = new Date();
Wednesday Mar 23 12:11:44 UYT 2011


DateTimeFormat.getFullDateFormat().format(today);
Wednesday, March 23, 2011


DateTimeFormat.getFullTimeFormat().format(today);
12:11:44 PM Etc/GMT+3


There is also Long, Medium and Short formats as well.
DateTimeFormat.getShortDateFormat().format(today);
3/23/11
DataTimeFormat.getShortTimeFormat().format(today);
12:11 PM


For numeric displays, similar builtin Gwt classes are available:
double number = 22919.60;


NumberFormat.getDecimalFormat().format(number);
22,919.6


NumberFormat.getPercentFormat().format(number);
2,291,960%


NumberFormat.getCurrencyFormat().format(number);
$22,919.60


GWT does a lot for you here. There's no excuse to not take advantage of this. Yes, the world puts up with English software for sure, but the goodwill you get for having localized and internationalized applications might just get your company a lot more sales.


No comments:

Post a Comment