Section 8.10. Accommodating Different Languages


8.10. Accommodating Different Languages

Most web sites make an effort to display their messages in different languages, or are simply required to because of the nature of their business. Figure 10 shows an informational message for a user with a Spanish locale specified. Pardon the translation if it does not make sense or unintentionally says something silly or worse, as I am not a professional translator (I used Google's language tool)! We're just using these translations as an example of how to implement the GWT's i18n mechanism.

Figure 8-5. An informational message in Spanish


If you are familiar with Java's mechanism involving ResourceBundles and property files, then you will recognize GWT's i18n methods. First, we create a default properties file named GwtAjaxConstants.properties to handle our messages. The application stores this file in the same directory as the GetAjax.java file: com.parkerriver.gwt.intro.client.

 #sample constant property to be translated in language specific versions of this #property file formSubmissionMsg=Form values were submitted... JsonMsg=JSON object values extracted... 

I specified the Spanish version of these messages in the file GwtAjaxConstants_es_ES.properties. Java developers use this file-naming convention for property files that represent certain locales. This one represents the "Spanish speakers in Spain" locale, which is specified by es_ES.

 formSubmissionMsg=Los valores de la forma fueron sometidos JsonMsg=JSON valores del software extrajeron... 

We also included a properties file for German speakers, which we do not have to show. That file name is GwtAjaxConstants_de_DE.properties. "de" is the language code for German; "DE" is the country code for Germany.

Next, you have to provide an interface that extends com.google.gwt.i18n.client.Constants.

NOTE

This is only one i18n mechanism that is included in the GWT framework. For a description of the others, see the documentation at:

                http://code.google.com/webtoolkit/documentation/com.google.gwt.doc. 

DeveloperGuide.Internationalization.html

This interface defines the methods your code will call to dynamically generate messages for specific locales. How the code accomplishes this task will be more apparent when I show the specific locale-related code in the GwtAjax.java class. Here is the code for the GwtAjaxConstants.java interface.

 package com.parkerriver.gwt.intro.client; /**  * Interface to represent the constants contained in resource bundle:  * /gwt/src/com/parkerriver/gwt/intro/client/GwtAjaxConstants.properties'.  */ public interface GwtAjaxConstants extends         com.google.gwt.i18n.client.Constants  {     /**      * Translated "Form values were submitted...".      *      * @return translated "Form values were submitted..."      */    String formSubmissionMsg();    /**      * Translated "JSON object values extracted...".      *      * @return translated "JSON object values extracted..."      */   String JsonMsg(); } 

This code defines two methods: formSubmissionMsg() and JsonMsg(). The method names line up with the names of the properties in the properties files.

 formSubmissionMsg=Form values were submitted... JsonMsg=JSON object values extracted... 

Here is the code inside the Java file GwtAjax.java that dynamically displays a translated message, depending on the particular locale.

 final GwtAjaxConstants myConstants = (GwtAjaxConstants) GWT.       create(GwtAjaxConstants.class); status.showStatus(true,     myConstants.formSubmissionMsg(),"green"); ... browserBox.setText(jobj.get("browser").isString().stringValue()); status.showStatus(true,     myConstants.JsonMsg(),"green"); 

The method chain using this JSON API is a little verbose. Here is an explanation. A JSONObject calls get(), passing in the key name as a String. This method returns the value associated with the key as a JSONValue object. The isString() method returns a JSONString, on which you call stringValue() to obtain the key value.

Remember that we have to include the following values in the module XML file. These values correspond to properties files of the form GwtAjaxConstants_de.properties. The latter resource will translate messages for the "de" locale, representing the German language (but not further delineating the country).

 <inherits name="com.google.gwt.i18n.I18N"/> <extend-property name="locale" values="es"/> <extend-property name="locale" values="de"/> <extend-property name="locale" values="es_ES"/> <extend-property name="locale" values="de_DE"/> 




Google Web Toolkit for Ajax
Google Web Toolkit GWT Java AJAX Programming: A step-by-step to Google Web Toolkit for creating Ajax applications fast
ISBN: 1847191002
EAN: 2147483647
Year: 2006
Pages: 29

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net