martes, 23 de agosto de 2011

GWT - I18N (Internacionalización)

Ejemplo de I18N (ingles y alemán)

GWT permite internacionalizar tanto los mensajes como las constantes:


StockWatcherConstants

package com.google.gwt.sample.stockwatcher.client;

import com.google.gwt.i18n.client.Constants;

public interface StockWatcherConstants extends Constants {
  @DefaultStringValue("StockWatcherrrr")
  String stockWatcher();

  @DefaultStringValue("Symbollll")
  String symbol();

  @DefaultStringValue("Priceee")
  String price();

  @DefaultStringValue("Changeee")
  String change();

  @DefaultStringValue("Removeee")
  String remove();

  @DefaultStringValue("Adddd")
  String add();
}


StockWatcherMessages

package com.google.gwt.sample.stockwatcher.client;

import java.util.Date;

import com.google.gwt.i18n.client.Messages;

public interface StockWatcherMessages extends Messages {
  @DefaultMessage("''{0}'' is not a valid symbol.")
  String invalidSymbol(String symbol);

  @DefaultMessage("Last update: {0,date,medium} {0,time,medium}")
  String lastUpdate(Date timestamp);

}

Una vez creados los mensajes y constantes que serán utilizados como locale por defecto, procedemos a crear la primera variante que sera para el idioma Aleman.

StockWatcherConstants_de.properties

stockWatcher = Aktienbeobachter
symbol = Symbol
price = Kurs
change = Änderung
remove = Entfernen
add = Hinzufügen

StockWatcherMessages_de.properties

lastUpdate = Letzte Aktualisierung: {0,date,medium} {0,time,medium}
invalidSymbol = ''{0}'' ist kein gültiges Aktiensymbol.



Usando la I18N

private StockWatcherConstants constants = GWT.create(StockWatcherConstants.class);

private StockWatcherMessages messages = GWT.create(StockWatcherMessages.class);


stocksFlexTable.setText(0, 0, constants.symbol());
stocksFlexTable.setText(0, 1, constants.price());
stocksFlexTable.setText(0, 2, constants.change());
stocksFlexTable.setText(0, 3, constants.remove());


Una vez internacionalizado el sitio, se le tendrá que indicar a GWT cual sera el LOCALE a utilizar. Siempre es una buena practica permitirle al usuario poder seleccionar el idioma que el desee.

Para indicar que el sitio tendrá que ser visto en aleman, se le tendra que agregar a la URL lo siguiente:

?locale=de

Para ingresar al locale por default
  • http://127.0.0.1:8888/StockWatcher.html?gwt.codesvr=127.0.0.1:9997

Para ingresar al locale en aleman
  • http://127.0.0.1:8888/StockWatcher.html?gwt.codesvr=127.0.0.1:9997&locale=de

How does GWT know which one to load at runtime? GWT uses client properties to produce customized JavaScript compilations of your GWT application using a mechanism called deferred binding



I18N & Deferred Binding

GWT generates a unique compilation for every combination of possible client property values. So, for example, if GWT supports 5 web browsers and you translate an application into 4 different languages, the GWT compiler produces a total of 20 different versions of your application. However, each user of your application is served only the code in the permutation matching his or her particular combination of web browser and locale.




Bibliografia

http://code.google.com/intl/es/webtoolkit/doc/latest/tutorial/i18n.html
http://code.google.com/intl/es/webtoolkit/doc/latest/DevGuideI18n.html


No hay comentarios:

Publicar un comentario