« Internationalization » : différence entre les versions
Aucun résumé des modifications |
Aucun résumé des modifications |
||
Ligne 1 : | Ligne 1 : | ||
The internationalization is manages by <font color=#556B2F>'''GENIUS'''</font> using <font color=#FF8C00>i18n</font> standard via the | The internationalization is manages by <font color=#556B2F>'''GENIUS'''</font> using <font color=#FF8C00>i18n</font> standard via the [https://www.gnu.org/savannah-checkouts/gnu/gettext xgettext] toolset, that allows creating message bundles for each language that are accessed in runtime to get the translated strings. Thus, the first point is to get this toolset via [https://www.gnu.org/savannah-checkouts/gnu/gettext xgettext]. | ||
Then we have to execute the following steps: | Then we have to execute the following steps: |
Version du 5 mai 2017 à 12:46
The internationalization is manages by GENIUS using i18n standard via the xgettext toolset, that allows creating message bundles for each language that are accessed in runtime to get the translated strings. Thus, the first point is to get this toolset via xgettext.
Then we have to execute the following steps:
- Prepare the parts of the code to be translated
- Extract the strings to be translated inside a "POT" file (using xgettext)
- Create a "PO" file (using [1] or [2]) inside which we will set the translation
- Eventually concatenate with other "po" files -for example coming from GENIUS- (using [3]))
- Create a message bundle java file (using [4])
- At last, create a i18n.properties file to indicate the domain of the message resource bundles
As these steps may be a bit complex to execute (specially under Windows OS where shell syntax is sometimes difficult to use, it is possible to use a utility provided with GENIUS : GIntTool.jar.
Prepare code to translate
The translation of the messages in the code is done using the following static methods from GEnvironment]. If no translation is available the original message is returned.
Methods Usage:
| GEnvironment.tr(String message) | Translate the message message | | GEnvironment.trf(String formatString, Object... args) | Translate the format string and then calls to String.format with args | | GEnvironment.trn(String singularString, String pluralString, long n) | Translates the singularString and pluralString and returns the translated pluralString if n > 1|
Only strings used with these three methods will be extracted by the xgettext tool and stored in the POT file ; other strings (identifiers, configuration file names, etc) will not be modified.
At last, we wil have to call to the other static method GEnvironment.initI18n() to specify which language to use:
GEnvironment.initI18n(new Locale("es")); // Spanish language
If no parameter is provided (or null argument), no translation is done and we will display what is hard coded.
GEnvironment.initI18n();
If the locale is wrong or if there is no available po files, the operating system locale will be used. Thus, for example, if the OS locale is "fr":
- if the french translation file exists, it will be displayed in french
- if the french translation file does not exist, the display will correspond to what is hardcoded.
GEnvironment.initI18n(new Locale("xx"));
Create a "POT" file
To create such POT file (here called test.pot), we will have to use [[5]] as is:
xgettext.exe -ktrc:1c,2 -ktrnc:1c,2,3 -ktr -kmarktr -ktrn:1,2
--from-code=UTF-8 --force-po
-o po/test.pot $path_java_sources/*.java
</syntaxhighlight>
where $path_java_sources represent the path to go to search for java sources.
Create a "PO" file"
Then, we will create a PO file using using [[6]] if the file does not exist:
msginit.exe --input=potFileName --output=poFileName
... or [[7]] if it already exists:
msgmerge.exe -U poFileName potFileName
The suffix of the potFileName will be .pot as the suffix of the poFileName file will be .ll.po where ll is the locale used (fr, en, sp, ...).
Once this PO file is created, we will have to modify it to add the right translation. Below is an example of a part of such file (the third string is not translated).
Be careful of the fact that inside the header the predefined character set is not allways put to UTF-8 but sometimes to CP1252 ; in case of it, change it !
"Content-Type: text/plain; charset=UTF-8\n"
...
msgid "Button"
msgstr "Bouton"
msgid "Character string entry:"
msgstr "Saisie d'une chaîne de caractères:"
msgid "Integer entry:"
msgstr ""
If you prefer not to enter directly inside the file, you may also use some utilities as [[8]]
Concatenate "po" files" (optional)
In case you use other po files (coming from other applications), it is possible to concatenate them using [[9]]:
msgcat –o finalpoFileName poFileName otherPoFileNames
Create Java resources
The last step is to create Java message resource bundles files with messages that will be incorporated to the application jar file. To generate these resource bundles the [10] command will be used:
msgfmt --verbose --java2 -d dirRes -r messFileName -l ll poFileName
... where:
- dirRes is the directory where the resources will be stored
- messFileName is the name of the final file (where the .class suffix will be added)
Create the i18n.properties file
This file has just to contain one line indicating the name of the resource file.
basename=messFileName
</syntaxhighlight>
A good solution to store all these files ...
In order not to be sure no to loose intermediate files (POT file, PO file, resource file, ...), a solution is to store all of them in a single directory (for example named po/). That is exactly the solution used by the GIntTool.jar tool. So, to tell to Eclipse to search classes at the right place, you will have just to do this:
- select your Java projet then click on the right button of the mouse to select Properties
- on the displayed frame, select "Java Build Path" then the Libraries tab.
- push on the "Add Class Folder" button and select the right directory
That's all ...