« Internationalization » : différence entre les versions

De GENIUS
Aller à la navigation Aller à la recherche
Aucun résumé des modifications
 
(14 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
The internationalization is manages by <font color=#556B2F>'''GENIUS'''</font> using <font color=#FF8C00>i18n</font> 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 https://www.gnu.org/savannah-checkouts/gnu/gettext.
== The different steps to follow ==
 
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:
# Prepare the parts of the code to be translated
# Prepare the parts of the code to be translated
# Extract the strings to be translated inside a "'''POT'''" file (using [https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/xgettext-Invocation.html#index-xgettext xgettext])
# Extract the strings to be translated inside a "'''POT'''" file (using [https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/xgettext-Invocation.html#index-xgettext xgettext])
# Create a "'''PO'''" file (using [https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/msginit-Invocation.html#index-msginit|msginit] or [https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/msgmerge-Invocation.html#index-msgmerge|msgmerge]) inside which we will set the translation
# Create a "'''PO'''" file (using [https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/msginit-Invocation.html#index-msginit msginit] or [https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/msgmerge-Invocation.html#index-msgmerge msgmerge]) inside which we will set the translation
# Eventually concatenate with other "po" files -for example coming from <font color=#556B2F>'''GENIUS'''</font>- (using [https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/msgcat-Invocation.html#index-msgcat|msgcat]))
# Eventually concatenate with other "po" files -for example coming from <font color=#556B2F>'''GENIUS'''</font>- (using [https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/msgcat-Invocation.html#index-msgcat msgcat]))
# Create a message bundle java file (using [https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/msgfmt-Invocation.html#index-msgfmt|msgfmt])
# Create a message bundle java file (using [https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/msgfmt-Invocation.html#index-msgfmt msgfmt])
# At last, create a i18n.properties file to indicate the domain of the message resource bundles
# 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 <font color=#556B2F>'''GENIUS'''</font> : {{:GIntTool.jar|}}.


== Prepare code to translate ==
== Prepare code to translate ==
Ligne 17 : Ligne 17 :
<u>Methods Usage</u>:
<u>Methods Usage</u>:


| <font color=#4169E1>GEnvironment.tr(String message)</font> | Translate the message message |
{| class="wikitable"
| <font color=#4169E1>GEnvironment.trf(String formatString, Object... args)</font> | Translate the format string and then calls to String.format with args |
|-
| <font color=#4169E1>GEnvironment.trn(String singularString, String pluralString, long n)</font> | Translates the singularString and pluralString and returns the translated pluralString if n > 1|
| <font color=#4169E1>GEnvironment.tr(String message)</font>
| Translate the message message
|-
| <font color=#4169E1>GEnvironment.trf(String formatString, Object... args)</font>
| Translate the format string and then calls to String.format with args
|-
| <font color=#4169E1>GEnvironment.trn(String singularString, String pluralString, long n)</font>
| 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.
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.
Ligne 45 : Ligne 53 :
== Create a "POT" file ==
== Create a "POT" file ==


To create such '''POT''' file (here called '''test.pot'''), we will have to use [[https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/xgettext-Invocation.html#index-xgettext|xgettext]] as is:
To create such '''POT''' file (here called '''test.pot'''), we will have to use [[https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/xgettext-Invocation.html#index-xgettext 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


<code>
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.
where $path_java_sources represent the path to go to search for java sources.
Ligne 57 : Ligne 64 :
== Create a "PO" file" ==
== Create a "PO" file" ==


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 73 : Ligne 76 :
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).
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 !
''<font color=#FF0000>Note : 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 !</font>''
 
<syntaxhighlight>
"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 ""


</syntaxhighlight>
"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 [[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]]


== Concatenate "po" files" (optional) ==
== Concatenate "po" files" (optional) ==


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


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:
* dirRes is the directory where the resources will be stored
* "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)
* "messFileName" is the prefix of the name of the final file (where the .class suffix will be added)
* "ll" is the language (as "en", "fr", "es", ...)
 
So, the final name will be ''dirRes/messFileName_ll.class''


== Create the i18n.properties file ==
== Create the i18n.properties file ==
Ligne 117 : Ligne 116 :
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.


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


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

Dernière version du 1 avril 2019 à 10:33

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).

Note : 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 prefix of the name of the final file (where the .class suffix will be added)
  • "ll" is the language (as "en", "fr", "es", ...)

So, the final name will be dirRes/messFileName_ll.class

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 →