8.11. Specifying a Certain Locale for a Web Page
So how does the application find out about which locale the user is associated with? GWT provides two ways. One involves examining the URL with which the user accesses the application and determining whether the page will display English, Spanish, or German languages. The URL includes the locale in a query string.
The URL has one parameter/value pair appended to its end in the form locale=es_ES. You may have guessed that if the locale is "es," then the translations from GwtAjaxConstants_es.properties are used. If the locale is "es_ES," then the application uses GwtAjaxConstants_es_ES.properties. If the locale is "es" but the developer has not provided a resource with that suffix (even if you have one with a "es_ES" suffix!), then the application uses the default localein this case, English (the translations inside GwtAjaxConstants.properties).
A typical way to implement this mechanism in a web application is to have icons representing the various countries. The user clicks on an icon, and that link determines the value of the locale parameter.
Another more static way to accomplish the same thing is to include this type of meta tag in the HTML file (e.g., GwtAjax.html).
<meta name="gwt:property" content="locale=es_ES">
This tag embeds the locale inside a particular web page. The query string/parameter method offers a more dynamic mechanism, since you can use it with just one web page. The i18n effort described here is admittedly limited, in that we have not translated the entire application, including the Label and Button text. For example, we could easily add a translated Button label by including the code in a properties file:
then add this method definition to the GwtConstants interface.
Finally, the Java file for the GUI client could include this code:
final GwtAjaxConstants myConstants = (GwtAjaxConstants) GWT. create(GwtAjaxConstants.class); Button but = new Button(myConstants.buttonText());