Internationalization : Différence entre versions

De Wiki
Aller à : navigation, rechercher
Ligne 1 : Ligne 1 :
The internationalization is manages by <font color=#556B2F>'''GENIUS'''</font> using <font color=#FF8C00>i18n</font> standard via the <font color=#FF8C00>xgettext</font> 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].
+
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:

  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 [1] or [2]) inside which we will set the translation
  4. Eventually concatenate with other "po" files -for example coming from GENIUS- (using [3]))
  5. Create a message bundle java file (using [4])
  6. 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":

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

Langage invalide

Vous devez spécifier un langage comme ceci : <source lang="html4strict">...</source>

Langages supportés pour la coloration syntaxique :

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


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

... or [[7]] if it already exists:

Langage invalide

Vous devez spécifier un langage comme ceci : <source lang="html4strict">...</source>

Langages supportés pour la coloration syntaxique :

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


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 !

Langage invalide

Vous devez spécifier un langage comme ceci : <source lang="html4strict">...</source>

Langages supportés pour la coloration syntaxique :

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


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

Langage invalide

Vous devez spécifier un langage comme ceci : <source lang="html4strict">...</source>

Langages supportés pour la coloration syntaxique :

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


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:

Langage invalide

Vous devez spécifier un langage comme ceci : <source lang="html4strict">...</source>

Langages supportés pour la coloration syntaxique :

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


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.

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

  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 →