Gettext: Difference between revisions
m (Created page with ' A note on PO. *.po is a file name extension of gettext, the GNU internationalization and localization (i18n) library. Gettext uses the following principle: * All source code mu…') |
m (using an external editor) |
||
Line 1: | Line 1: | ||
{{Stub}} | |||
== Introduction == | |||
'''Gettext''' is the name of the popular GNU internationalization and localization (i18n) library used in free software. | |||
See also: [[Software localization]]. | |||
== Principle == | |||
Gettext uses the following principle: | |||
* All source code must use Gettext calls when using strings. (e.g. ''print("salut")'' would become ''print (gettext ("salut")'' or the shorcut ''print (_("salut"))'' | * All source code must use Gettext calls when using strings. (e.g. ''print("salut")'' would become ''print (gettext ("salut")'' or the shorcut ''print (_("salut"))'' | ||
* The language used in the code will be the key for all translation languages | * The language used in the code will be the key for all translation languages | ||
* xgetttext then can extract all these from the source and produce a *.pot template file. | * xgetttext then can extract all these from the source and produce a *.pot template file. | ||
* This template file then is use to produce editable *.po files that look like this | * This template file then is use to produce editable *.po files that look like this | ||
#: src/name.c:36 | #: src/name.c:36 | ||
msgid "My name is %s.\n" | msgid "My name is %s.\n" | ||
msgstr "Je m'appelle %s.\n" | msgstr "Je m'appelle %s.\n" | ||
Msgid (the key and original language) and msgstr the translated string can be displayd side-by side by so-called PO editors. | |||
Gettext is used in slightly different ways in different languages. | |||
; PHP source code example | |||
(1) Sample code with a string that needs translation | |||
<source lang="php"> | |||
<?php | |||
echo _("Hello World!"); | |||
?> | |||
</source> | |||
(2) Translations then should go a directory, e.g. ''locale'' | |||
Each locale will sit in its own subdirectory. (more details needed here ..) | |||
(3) Sample code to initialize the run-time localization | |||
<source lang="php"> | |||
<?php | |||
$locale = "de_DE"; | |||
if (isSet($_GET["locale"])) $locale = $_GET["locale"]; | |||
putenv("LC_ALL=$locale"); | |||
setlocale(LC_ALL, $locale); | |||
bindtextdomain("messages", "./locale"); | |||
textdomain("messages"); | |||
?> | |||
</source> | |||
; Java | |||
Gettext can be used with the [http://code.google.com/p/gettext-commons/ gettext-commons] library. Java source code modifications rather would look like this: | |||
I18n i18n = I18nFactory.getI18n(getClass()); | |||
System.out.println(i18n.tr("This text will be translated")); | |||
== Tools == | |||
There exist several PO editors, e.g. | |||
* [http://translate.sourceforge.net/wiki/virtaal/index Virtaal]. | |||
* [http://www.poedit.net/ Poedit] | |||
* Some programming editors (like [[emacs]] also have PO support. | |||
Toolkits for localization and translation | |||
* [http://en.wikipedia.org/wiki/Translate_Toolkit Translate Toolkit] (Wikipedia) can handle PO files. | |||
== Complications == | |||
* Dealing with arguments | |||
* Handling plural | |||
* ... | |||
== Links == | == Links == | ||
* [http://en.wikipedia.org/wiki/Gettext GNU Gettext] | * [http://en.wikipedia.org/wiki/Gettext GNU Gettext] (Wikipedia) | ||
*[http://mel.melaxis.com/devblog/2005/08/06/localizing-php-web-sites-using-gettext/ Localizing PHP web sites using gettext] by Pablo Hoch, 2005. | |||
[[Category: Programming]] | [[Category: Programming]] | ||
[[Category: Ergonomics and human-computer interaction]] | [[Category: Ergonomics and human-computer interaction]] |
Latest revision as of 18:12, 21 January 2010
Introduction
Gettext is the name of the popular GNU internationalization and localization (i18n) library used in free software.
See also: Software localization.
Principle
Gettext uses the following principle:
- All source code must use Gettext calls when using strings. (e.g. print("salut") would become print (gettext ("salut") or the shorcut print (_("salut"))
- The language used in the code will be the key for all translation languages
- xgetttext then can extract all these from the source and produce a *.pot template file.
- This template file then is use to produce editable *.po files that look like this
#: src/name.c:36 msgid "My name is %s.\n" msgstr "Je m'appelle %s.\n"
Msgid (the key and original language) and msgstr the translated string can be displayd side-by side by so-called PO editors.
Gettext is used in slightly different ways in different languages.
- PHP source code example
(1) Sample code with a string that needs translation
<?php
echo _("Hello World!");
?>
(2) Translations then should go a directory, e.g. locale Each locale will sit in its own subdirectory. (more details needed here ..)
(3) Sample code to initialize the run-time localization
<?php
$locale = "de_DE";
if (isSet($_GET["locale"])) $locale = $_GET["locale"];
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
bindtextdomain("messages", "./locale");
textdomain("messages");
?>
- Java
Gettext can be used with the gettext-commons library. Java source code modifications rather would look like this:
I18n i18n = I18nFactory.getI18n(getClass()); System.out.println(i18n.tr("This text will be translated"));
Tools
There exist several PO editors, e.g.
Toolkits for localization and translation
- Translate Toolkit (Wikipedia) can handle PO files.
Complications
- Dealing with arguments
- Handling plural
- ...
Links
- GNU Gettext (Wikipedia)
- Localizing PHP web sites using gettext by Pablo Hoch, 2005.