« Internationalization » : différence entre les versions

De GENIUS
Aller à la navigation Aller à la recherche
Aucun résumé des modifications
Aucun résumé des modifications
Ligne 66 : Ligne 66 :
Then, we will create a '''PO''' file using using [[https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/msginit-Invocation.html#index-msginit msginit]] if the file does not exist:
Then, we will create a '''PO''' file using using [[https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/msginit-Invocation.html#index-msginit msginit]] if the file does not exist:


<syntaxhighlight>
msginit.exe --input=potFileName --output=poFileName
msginit.exe --input=potFileName --output=poFileName
</syntaxhighlight>


... or [[https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/msgmerge-Invocation.html#index-msgmerge msgmerge]] if it already exists:
... or [[https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/msgmerge-Invocation.html#index-msgmerge msgmerge]] if it already exists:


<syntaxhighlight>
msgmerge.exe -U poFileName potFileName
msgmerge.exe -U poFileName potFileName
</syntaxhighlight>


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, ...).
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, ...).
Ligne 82 : Ligne 78 :
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 !
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 !


<syntaxhighlight>
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Type: text/plain; charset=UTF-8\n"
 
...
...
 
msgid "Button"
msgid "Button"
msgstr "Bouton"
msgstr "Bouton"
 
msgid "Character string entry:"
msgid "Character string entry:"
msgstr "Saisie d'une chaîne de caractères:"
msgstr "Saisie d'une chaîne de caractères:"
 
msgid "Integer entry:"
msgid "Integer entry:"
msgstr ""
msgstr ""
 
</syntaxhighlight>


If you prefer not to enter directly inside the file, you may also use some utilities as [[http://poedit.net/.|poedit]]  
If you prefer not to enter directly inside the file, you may also use some utilities as [[http://poedit.net/.|poedit]]  
Ligne 104 : Ligne 97 :
In case you use other po files (coming from other applications), it is possible to concatenate them using [[https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/msgcat-Invocation.html#index-msgcat msgcat]]:
In case you use other po files (coming from other applications), it is possible to concatenate them using [[https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/msgcat-Invocation.html#index-msgcat msgcat]]:


<syntaxhighlight>
msgcat –o finalpoFileName poFileName  otherPoFileNames
msgcat –o finalpoFileName poFileName  otherPoFileNames
</syntaxhighlight>


== Create Java resources ==
== Create Java resources ==
Ligne 112 : Ligne 103 :
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 [https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/msgfmt-Invocation.html#index-msgfmt msgfmt] command will be used:
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 [https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/msgfmt-Invocation.html#index-msgfmt msgfmt] command will be used:


<syntaxhighlight>
msgfmt --verbose --java2 -d dirRes -r messFileName -l ll poFileName
msgfmt --verbose --java2 -d dirRes -r messFileName -l ll poFileName
</syntaxhighlight>


... where:
... where:
Ligne 124 : Ligne 113 :
This file has just to contain one line indicating the name of the resource file.
This file has just to contain one line indicating the name of the resource file.


<syntaxhighlight >
basename=messFileName
basename=messFileName
</syntaxhighlight>


== A good solution to store all these files ... ==
== A good solution to store all these files ... ==

Version du 10 juillet 2017 à 08:00

The different steps to follow

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:

  1. Prepare the parts of the code to be translated
  2. Extract the strings to be translated inside a "POT" file (using xgettext)
  3. Create a "PO" file (using msginit or msgmerge) inside which we will set the translation
  4. Eventually concatenate with other "po" files -for example coming from GENIUS- (using msgcat))
  5. Create a message bundle java file (using msgfmt)
  6. At last, create a i18n.properties file to indicate the domain of the message resource bundles

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":

  1. if the french translation file exists, it will be displayed in french
  2. 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 [xgettext] 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


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 [msginit] if the file does not exist:

msginit.exe --input=potFileName --output=poFileName

... or [msgmerge] 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 [[1]]

Concatenate "po" files" (optional)

In case you use other po files (coming from other applications), it is possible to concatenate them using [msgcat]:

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 msgfmt 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

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:

  1. select your Java projet then click on the right button of the mouse to select Properties
  2. on the displayed frame, select "Java Build Path" then the Libraries tab.
  3. push on the "Add Class Folder" button and select the right directory

That's all ...


Return to the introduction ↑ Go to the next page →