gettext

From Wikipedia, the free encyclopedia
  (Redirected from GNU gettext)
Jump to navigation Jump to search

gettext
Original author(s)Sun Microsystems
Developer(s)various
Initial release1990; 29 years ago (1990)[1]
Stable release
0.20.1 / May 12, 2019; 4 months ago (2019-05-12)
Repositoryvarious based on OpenSolaris and GNU gettext
Operating systemCross-platform
TypeInternationalization and localization
LicenseVarious free software licenses
Websitewww.gnu.org/software/gettext/

In computing, gettext is an internationalization and localization (i18n and l10n) system commonly used for writing multilingual programs on Unix-like computer operating systems. One of the main benefit of gettext is that it separates programming from translating.[2] The most commonly used implementation of gettext is GNU gettext,[3] released by the GNU Project in 1995. The runtime library is libintl. gettext provides an option to use different strings for any number of plural forms of nouns, but it has no support for grammatical gender.

History[edit]

Initially, POSIX provided no means of localizing messages. Two proposals were raised in the late 1980s, the 1988 Uniforum gettext and the 1989 X/Open catgets (XPG-3 § 5). Sun Microsystems implemented the first gettext in 1993. The Unix and POSIX developers never really agreed on what kind of interface to use (the other option is the X/Open catgets), so many C libraries, including glibc, implemented both.[4] As of August 2019, whether gettext should be part of POSIX was still a point of debate in the Austin Group, despite the fact that its old foe has already fallen out of use. Concerns cited included its dependence on the system-set locale (a global variable subject to multithreading problems) and its support for newer C-language extensions involving wide strings.[5]

The GNU Project decided that the message-as-key approach of gettext is simpler and more friendly.[6] They released GNU gettext, a free software implementation of the system in 1995.[1] Gettext, GNU or not, has since been ported onto many programming languages.[7] The simplicity of po and widespread editor support even lead to its adoption in non-program contexts for text documents or as an intermediate between other localization formats, with converters like po4a (po for anything) and Translate Toolkit emerging to provide such a bridge.[8][9]

Operation[edit]

Programming[edit]

Typical gettext workflow. The de.po instance on the left shows a "renewing" of translations via msgmerge.

The basic interface of gettext is the gettext(const char*) function, which accepts a strings that the user will see in the original language, usually english.. To save typing time, and to reduce code clutter, this function is commonly aliased to _:

printf(gettext("My name is %s.\n"), my_name);
printf(_("My name is %s.\n"), my_name); // same, but shorter

gettext() then uses the supplied strings as keys for looking up translations, and will return the original string when no translation is available. This is in contrast to POSIX catgets(),[10] AmigaOS GetString(), or Microsoft Windows LoadString() where a programmatic ID (often an integer) is used. To handle the case where the same original-language text can have different meanings, gettext has functions like cgettext() that accept an additional "context" string.

xgettext is run on the sources to produce a .pot (Portable Object Template) file, which contains a list of all the translatable strings extracted from the sources. Comments starting with /// are used to give translators hints, although other prefixes are also configurable to further limit the scope. One such common prefix is TRANSLATORS:.

For example, an input file with a comment might look like:

/// TRANSLATORS: %s contains the user's name as specified in Preferences
printf(_("My name is %s.\n"), my_name);

xgettext is run using the command:

xgettext -c /

The resultant .pot file looks like this with the comment (note that xgettext recognizes the string as a C-language printf format string):

#. TRANSLATORS: %s contains the user's name as specified in Preferences
#, c-format
#: src/name.c:36
msgid "My name is %s.\n"
msgstr ""

In POSIX shell script, gettext provides a gettext.sh library one can include that provides the many same functions gettext provides in similar languages.[11] GNU bash also has a simplified construct $"msgid" for the simple gettext function, although it depends on the C library to provide a gettext() function.[12]

Translating[edit]

The translator derives a .po (Portable Object) file from the template using the msginit program, then fills out the translations.[13] msginit initializes the translations so, for instance, for a French language translation, the command to run would be:[14]

msginit --locale=fr --input=name.pot

This will create fr.po. The translator then edits the resultant file, either by hand or with a translation tool like Poedit, or Emacs with its editing mode for .po files. An edited entry will look like:

#: src/name.c:36
msgid "My name is %s.\n"
msgstr "Je m'appelle %s.\n"

Finally, the .po files are compiled with msgfmt into binary .mo (Machine Object) files. GNU gettext may use its own file name extension .gmo on systems with another gettext implementation.[15] These are now ready for distribution with the software package.

GNU msgfmt can also perform some checks relevant to the format string used by the programming language. It also allows for outputting to language-specific formats other than MO.[16]

In later phases of the developmental workflow, msgmerge can be used to "update" an old translation to a newer template. There is also msgunfmt for reverse-compiling .mo files, and many other utilities for batch processing.

Running[edit]

The user, on Unix-type systems, sets the environment variable LC_MESSAGES, and the program will display strings in the selected language, if there is an .mo file for it.

Users on GNU variants can also use the environment variable LANGUAGE instead. Its main difference from the Unix variable is that it supports multiple languages, separated with a colon, for fallback.[17]

Plural form[edit]

The ngettext() interface accounts for the count of a noun in the string. As with the convention of gettext(), it is often aliased to N_ in practical use. Consider the code sample:

// parameters: english singular, english plural, integer count
printf(ngettext("I have written an article.\n", "I have written %d articles.\n", n), n);

A header in the "" (empty string) entry of the PO file stores some metadata, one of which is the plural form that the language uses, usually specified using a C-style ternary expression. Let's say we want to translate for the Irish language:

msgid ""
msgstr ""
"..."
"Language: ga\n"
"Plural-Forms: nplurals=3; plural=n==1 ? 0 : n==2 ? 1 : 2;\n"

Since now there are three plural forms, the final po would look like:

#: src/wiki.c:29
msgid "I have written an article.\n"
msgid_plural "I have written %d articles.\n"
msgstr[0] "Irish speaker needed"
msgstr[1] ""
msgstr[2] ""

Reference plural rules for languages are provided by the Unicode consortium.[18] msginit also prefills the appropriate rule when creating a file for one specific language.[13]

Implementations[edit]

In addition to C, gettext has the following implementations: C# for both ASP.NET[19][20] and for WPF,[21] Perl,[22] PHP,[23] Python,[24] Scala,[25] and Node.js.[26]

See also[edit]

References[edit]

  1. ^ a b "History of gettext() et al? - comp.unix.solaris". Compgroups.net. Retrieved 3 April 2016.
  2. ^ Martindale, Linda (1 November 2002). "Bridging the Digital Divide in South Africa | Linux Journal". www.linuxjournal.com. Linux Journal. Archived from the original on 17 September 2019. Retrieved 17 September 2019.
  3. ^ Tykhomyrov, Olexiy Ye (1 November 2002). "Introduction to Internationalization Programming | Linux Journal". www.linuxjournal.com. Linux Journal. Archived from the original on 17 September 2019. Retrieved 17 September 2019.
  4. ^ "Message Translation". The GNU C Library.
  5. ^ "0001122: POSIX should include gettext() and friends - Austin Group Defect Tracker". Austin Group Defect Tracker.
  6. ^ "The Programmer's View". gettext 0.10.35. 30 April 1998.
  7. ^ "GNU gettext utilities: List of Programming Languages".
  8. ^ "po4a". po4a.org.
  9. ^ "The essential toolkit for localization engineers". Translate Toolkit.
  10. ^ "About catgets". gnu.org. Retrieved 24 October 2017.
  11. ^ "GNU gettext utilities: sh".
  12. ^ "GNU gettext utilities: bash".
  13. ^ a b "GNU gettext utilities: PO Files". Gnu.org. Retrieved 3 April 2016.
  14. ^ "How to Translate With GetText PO and POT Files". Icanlocalize.com. Retrieved 3 April 2016.
  15. ^ "Files Conveying Translations". Gnu.org. Retrieved 22 April 2014.
  16. ^ "msgfmt Invocation". GNU gettext utilities.
  17. ^ "GNU gettext utilities: Locale Environment Variables". Gnu.org. Retrieved 3 April 2016.
  18. ^ "Language Plural Rules". unicode.org.
  19. ^ "Google Code Archive - Long-term storage for Google Code Project Hosting". Code.google.com. Retrieved 3 April 2016.
  20. ^ "turquoiseowl/i18n: Smart internationalization for ASP.NET". GitHub.com. Retrieved 3 April 2016.
  21. ^ "NGettext.Wpf - Proper internationalization support for WPF (via NGettext)".
  22. ^ "libintl-perl - An Internationalization Library for Perl That Aims To Be Compatible With the Uniforum Message Translations System as Implemented For Example in GNU Gettext". github.com. Retrieved 14 September 2017.
  23. ^ "Gettext". php.net. Retrieved 24 October 2017.
  24. ^ "gettext — Multilingual internationalization services — Python 3.7.0 documentation". docs.python.org. Retrieved 21 September 2018.
  25. ^ "makkarpov/scalingua: A simple gettext-like internationalization library for Scala". github.com. Retrieved 28 April 2016.
  26. ^ "DanielBaulig/node-gettext: An adaption of Joshua I. Miller's Javascript Gettext library for node.js". GitHub.com. Retrieved 3 April 2016.

External links[edit]