GWT GWT - Internationalization If the targeted group of users of our application are people of different nationalities then we adapt it to support multiple languages. The first thing that comes to us to measure the translation of texts will be permanent. The translation of texts, we can leave the linguists, but our task is to write such applications to support these texts was possible and when adding subsequent translations do not require programming work.
course fairly quickly, each developer could work around this problem by devising its own mechanism, but the developer lazy (read: effective ) przegrzebie documentation to find a mechanism which has already been invented. How it looks in GWT ...
key element that will allow us the location of solid text is a module that is included with com.google.gwt.i18n it offers several mechanisms: Constants, Messages , ConstantsWithLookup , Dictionary , Localizable . Let us focus on two basic Constants and Messages that should be used in most cases.
If you want to take advantage of these mechanisms Our module must inherit from the module com.google.gwt.i18n.I18N (file MyModule.gwt.xml packages com.mycompany.mymodule ):
\u0026lt;module>
\u0026lt;inherits name = " com.google.gwt.i18n.I18N "/>
\u0026lt;/ module>
\u0026lt;extend-property name="locale" values="pl,en"/>
The next step is to create files containing the text of a permanent for the various languages \u200b\u200band the default file.
Default File
MyConstans.properties (included
com.mycompany.mymodule.client ):
HelloWorld = Hello World
goodbyeWorld = Goodbye World
file for Polish language
MyConstans_pl.properties (included
com.mycompany.mymodule.client ):
= HelloWorld Hello World
goodbyeWorld = Goodbye World
File for English
MyConstans_en.properties (included
com.mycompany.mymodule.client ):
HelloWorld = "Hello World
goodbyeWorld = Goodbye World
next step is in the package stworznie
com.mycompany.mymodule.client Interface
MyConstans where method names will correspond to keys in files with fixed texts:
public interface extends Constants {MyConstants
String HelloWorld ();
goodbyeWorld String ();}
To use our lyrics are enough two lines of code in the code the customer class (eg
MyModule.java )
MyConstants myConstants = (MyConstants) GWT.create (MyConstants.class);
Window.alert (myConstants.helloWorld ());
second mechanism, which somehow appear to extend to Constants Messages, it differs only by the fact that the text may have solid arguments. Assume that the file ErrorMessages.properties we text:
permissionDenied = Error {0}: {1} You do not have permission to access to {2}
then he or she defines an interface similar to
Constants but the method should have as many input arguments if it has been wymienonych constants in the texts. Interface Definition
ErrorMessages.java :
ErrorMessages public interface extends Messages {String
permissionDenied (int errorCode, String username, String resource);}
to text to be displayed permanently in the language we have two possibilities. The first is to property setting \u0026lt;meta name="gwt:property" content="locale=pl"> directly in html page:
\u0026lt;html>
\u0026lt;head>
\u0026lt;meta name = "gwt: module" content = "com.mycompany.mymodule.MyModule">
\u0026lt;meta name="gwt:property" content="locale=pl">
\u0026lt;/ head>
\u0026lt;body>
\u0026lt;script src = "gwt.js"> \u0026lt;/ script>
\u0026lt;/ body>
\u0026lt;/ html> The second option
This adds a parameter to the URL:
http://www.mycompany.com/MyModule.html?locale=pl
mechanisms, which have been described can only be used in the code under which it is generated JavaScript (which is included in the package client). While compiling GWT generates for each location a separate JavaScript file.
sum GWT provides a fairly comprehensive mechanism of internationalization, although the mechanism would be sufficient for me Messages , because I can not immediately predict that my text will not be fixed once the arguments. Another thing that gives me a disadvantage is that you can not use these mechanisms on the server side, forcing us to use the standard solution provided by the Java ResourceBundle .