Friday, December 11, 2009

Internationalization in GWT

Hi All,

Today I am going to post how to do internationalization using GWT in a simple way.

Please refer to the help guide of gwt if you need about locale..PLease check here :

I have used  static String Internationalization and  I have extended Constant Interface.

I have described the process in steps.

In gwt.xml  inhereit the below values
    inherits name=""
    extend-property name="locale" values="en"
   extend-property name="locale" values="sp"
   extend-property name="locale" values="fr"    

Step2:  Create  properties files in the client folder: 

Note: _en etc are the language code for the locale.

    Don't forget this meta tag in the main html file:
    meta name="gwt:property" content="locale=en"
meta http-equiv="content-type" content="text/html;charset=utf-8"

You must also ensure that all relevant source and .properties files are 
set to be in the UTF-8 charset in your IDE. 

Step 2: create a entry point

public class Test implements EntryPoint {
    MyMessages myMessages = (MyMessages) GWT.create(MyMessages.class);

    public void onModuleLoad() {

        Label enlbl = new Label(myMessages.add());
        Label splbl = new Label(myMessages.stockWatcher());

        final ListBox lbox = new ListBox();
        lbox.addChangeHandler(new ChangeHandler() {

            public void onChange(ChangeEvent event) {
                if (lbox.getSelectedIndex()==0) {
                }else if (lbox.getSelectedIndex() == 1) {
                } else if (lbox.getSelectedIndex() == 2) {
                } else if (lbox.getSelectedIndex() == 3) {

        RootPanel.get().add(enlbl, 10, 0);
        RootPanel.get().add(splbl, 60, 0);

In the Module load please note the red line
Reason:Because these are interfaces, not classes, you can't instantiate them directly. Instead, you use the GWT.create(Class) method. Then you'll be able to use these interfaces' accessor methods to retrieve the appropriate strings.
to get and set the locale to load the url please use the following JSNI code

private native void changeLocale(String newLocale)/*-{
            var currLocation = $wnd.location.toString();
            var locArray = currLocation.split("?");
            $wnd.location.href = locArray[0]+"?locale="+newLocale;

Create a new interface


public interface MyMessages extends Constants {
      String stockWatcher();

      String symbol();

      String price();

      String change();

      String remove();

      String add();

thats it, whenver you select a locale from the listbox the page load with the selected locale. thats all.

I refered to for this workout.

Please download the complete eclipse source code here

If you find any mistakes please comment here to correct it.


No comments: