Gettext

Last updated

gettext
Original author(s) Sun Microsystems
Developer(s) various
Initial release1990;34 years ago (1990) [1]
Stable release
0.22.5 [2]   OOjs UI icon edit-ltr-progressive.svg / 21 February 2024;42 days ago (21 February 2024)
Repository various based on OpenSolaris and GNU gettext
Operating system Cross-platform
Type Internationalization and localization
License Various free software licenses
Website www.gnu.org/software/gettext/   OOjs UI icon edit-ltr-progressive.svg

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 benefits of gettext is that it separates programming from translating. [3] The most commonly used implementation of gettext is GNU gettext, [4] 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 this feature has no support for grammatical gender. The main filename extensions used by this system are .POT (Portable Object Template), .PO (Portable Object) and .MO (Machine Object). [5]

Contents

History

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. [6] 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. [7]

The GNU Project decided that the message-as-key approach of gettext is simpler and more friendly. (Most other systems, including catgets, requires the developer to come up with "key" names for every string.) [8] They released GNU gettext, a free software implementation of the system in 1995. [1] Gettext, GNU or not, has since been ported to many programming languages. [9] 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. [10] [11]

Operation

Programming

Typical gettext workflow. The de.po instance on the left shows a "renewing" of translations via msgmerge. Gettext.svg
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 string that the user will see in the original language, usually English. To save typing time and reduce code clutter, this function is commonly aliased to _ [12] :

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(), [13] AmigaOS GetString(), [14] 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 Preferencesprintf(_("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:36msgid"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. [15] 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. [16]

Translating

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

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:36msgid"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. [18] 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; [19] the X/Open equivalent is gencat.

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

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. [20]

Plural form

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 countprintf(ngettext("%d translated message","%d translated messages",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 operator. Suppose we want to translate for the Slovene language:

msgid""msgstr"""...""Language: sl\n""Plural-Forms: nplurals=4; plural=(n%100==1 ? 1 : n%100==2 ? 2 : n%100==3 || n%100==4 ? 3 : 0);\n"

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

#: src/msgfmt.c:876#, c-formatmsgid"%d translated message"msgid_plural"%d translated messages"msgstr[0]"%d prevedenih sporočil"msgstr[1]"%d prevedeno sporočilo"msgstr[2]"%d prevedeni sporočili"msgstr[3]"%d prevedena sporočila"

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

Implementations

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

GNU gettext has native support for Objective-C, but there is no support for the Swift programming language yet. A commonly used gettext implementation on these Cocoa platforms is POLocalizedString. [31] The Microsoft Outlook for iOS team also provides a LocalizedStringsKit library with a gettext-like API. [32]

See also

Related Research Articles

<span class="mw-page-title-main">AWK</span> Programming language

AWK is a domain-specific language designed for text processing and typically used as a data extraction and reporting tool. Like sed and grep, it is a filter, and is a standard feature of most Unix-like operating systems.

man page Unix software documentation

A man page is a form of software documentation usually found on a Unix or Unix-like operating system. Topics covered include computer programs, formal standards and conventions, and even abstract concepts. A user may invoke a man page by issuing the man command.

<span class="mw-page-title-main">Internationalization and localization</span> Process of making software accessible to people in different areas of the world

In computing, internationalization and localization (American) or internationalisation and localisation (British), often abbreviated i18n and l10n respectively, are means of adapting computer software to different languages, regional peculiarities and technical requirements of a target locale.

The C standard library or libc is the standard library for the C programming language, as specified in the ISO C standard. Starting from the original ANSI C standard, it was developed at the same time as the C library POSIX specification, which is a superset of it. Since ANSI C was adopted by the International Organization for Standardization, the C standard library is also called the ISO C library.

<span class="mw-page-title-main">MinGW</span> Free and open-source software for developing applications in Microsoft Windows

MinGW, formerly mingw32, is a free and open source software development environment to create Microsoft Windows applications.

The archiver, also known simply as ar, is a Unix utility that maintains groups of files as a single archive file. Today, ar is generally used only to create and update static library files that the link editor or linker uses and for generating .deb packages for the Debian family; it can be used to create archives for any purpose, but has been largely replaced by tar for purposes other than static libraries. An implementation of ar is included as one of the GNU Binutils.

The printf family of functions in the C programming language are a set of functions that take a format string as input among a variable sized list of other values and produce as output a string that corresponds to the format specifier and given input values. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data to characters. The design has been copied to expose similar functionality in other programming languages.

In computer programming, glob patterns specify sets of filenames with wildcard characters. For example, the Unix Bash shell command mv *.txttextfiles/ moves all files with names ending in .txt from the current directory to the directory textfiles. Here, * is a wildcard and *.txt is a glob pattern. The wildcard * stands for "any string of any length including empty, but excluding the path separator characters ".

xargs is a command on Unix and most Unix-like operating systems used to build and execute commands from standard input. It converts input from standard input into arguments to a command.

In computing, POSIX Threads, commonly known as pthreads, is an execution model that exists independently from a programming language, as well as a parallel execution model. It allows a program to control multiple different flows of work that overlap in time. Each flow of work is referred to as a thread, and creation and control over these flows is achieved by making calls to the POSIX Threads API. POSIX Threads is an API defined by the Institute of Electrical and Electronics Engineers (IEEE) standard POSIX.1c, Threads extensions .

tr (Unix) Unix text formatting utility

tr is a command in Unix, Plan 9, Inferno, and Unix-like operating systems. It is an abbreviation of translate or transliterate, indicating its operation of replacing or removing specific characters in its input data set.

In computing, echo is a command that outputs the strings that are passed to it as arguments. It is a command available in various operating system shells and typically used in shell scripts and batch files to output status text to the screen or a computer file, or as a source part of a pipeline.

cmp (Unix) Computer file comparison utility

In computing, cmp is a command-line utility on Unix and Unix-like operating systems that compares two files of any type and writes the results to the standard output. By default, cmp is silent if the files are the same; if they differ, the byte and line number at which the first difference occurred is reported. The command is also available in the OS-9 shell.

In Unix-like and some other operating systems, find is a command-line utility that locates files based on some user-specified criteria and either prints the pathname of each matched object or, if another action is requested, performs that action on each matched object.

In computing, tee is a command in command-line interpreters (shells) using standard streams which reads standard input and writes it to both standard output and one or more files, effectively duplicating its input. It is primarily used in conjunction with pipes and filters. The command is named after the T-splitter used in plumbing.

The Translate Toolkit is a localization and translation toolkit. It provides a set of tools for working with localization file formats and files that might need localization. The toolkit also provides an API on which to develop other localization tools.

<span class="mw-page-title-main">Gtranslator</span> Free computer-assisted translation software

Gtranslator is a specialized computer-assisted translation software and po file editor for the internationalization and localization (i18n) of software that uses the gettext system. It handles all forms of gettext po files and includes features such as Find/Replace, Translation Memory, different Translator Profiles, Messages Table, Easy Navigation and Editing of translation messages and comments of the translation where accurate. Gtranslator includes also a plugin system with plugins such as Alternate Language, Insert Tags, Open Tran, Integration with Subversion, and Source Code Viewer. Gtranslator is written in the programming language C for the GNOME desktop environment. It is available as free software under the terms of the GNU General Public License (GPL).

Getopt is a C library function used to parse command-line options of the Unix/POSIX style. It is a part of the POSIX specification, and is universal to Unix-like systems. It is also the name of a Unix program for parsing command line arguments in shell scripts.

printf (Unix) Standard UNIX utility

In Unix and Unix-like operating systems, printf is a shell builtin that formats and prints data.

References

  1. 1 2 "History of gettext() et al? - comp.unix.solaris". Compgroups.net. Archived from the original on 23 March 2012. Retrieved 3 April 2016.{{cite web}}: CS1 maint: unfit URL (link)
  2. Bruno Haible (22 February 2024). "GNU gettext 0.22.5 released" . Retrieved 7 March 2024.
  3. Martindale, Linda (1 November 2002). "Bridging the Digital Divide in South Africa | Linux Journal". linuxjournal.com. Linux Journal. Archived from the original on 17 September 2019. Retrieved 17 September 2019.
  4. Tykhomyrov, Olexiy Ye (1 November 2002). "Introduction to Internationalization Programming | Linux Journal". linuxjournal.com. Linux Journal. Archived from the original on 17 September 2019. Retrieved 17 September 2019.
  5. 1 2 "How to Translate With GetText PO and POT Files". Icanlocalize.com. Retrieved 3 April 2016.
  6. "Message Translation". The GNU C Library.
  7. "0001122: POSIX should include gettext() and friends - Austin Group Defect Tracker". Austin Group Defect Tracker.
  8. "The Programmer's View". gettext 0.10.35. 30 April 1998.
  9. "GNU gettext utilities: List of Programming Languages".
  10. "po4a". po4a.org.
  11. "The essential toolkit for localization engineers". Translate Toolkit.
  12. "GNU gettext utilities: How Marks Appear in Sources". www.gnu.org. Archived from the original on 25 March 2024. Retrieved 3 April 2024.
  13. "About catgets". gnu.org. Retrieved 24 October 2017.
  14. "AmigaOS Manual: Python Modules and Packages - AmigaOS Documentation Wiki". wiki.amigaos.net. Retrieved 9 July 2023.
  15. "GNU gettext utilities: sh".
  16. "GNU gettext utilities: bash".
  17. 1 2 "GNU gettext utilities: PO Files". Gnu.org. Retrieved 3 April 2016.
  18. "Files Conveying Translations". Gnu.org. Retrieved 22 April 2014.
  19. "msgfmt Invocation". GNU gettext utilities.
  20. "GNU gettext utilities: Locale Environment Variables". Gnu.org. Retrieved 3 April 2016.
  21. "Language Plural Rules". unicode.org.
  22. "Google Code Archive - Long-term storage for Google Code Project Hosting". Code.google.com. Retrieved 3 April 2016.
  23. "turquoiseowl/i18n: Smart internationalization for ASP.NET". GitHub.com. Retrieved 3 April 2016.
  24. "NGettext.Wpf - Proper internationalization support for WPF (via NGettext)". GitHub . 16 August 2019.
  25. "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.
  26. "Gettext". php.net. Retrieved 24 October 2017.
  27. "gettext – Multilingual internationalization services – Python 3.7.0 documentation". docs.python.org. Retrieved 21 September 2018.
  28. "gettext: Translate Text Messages". rdrr.io. Retrieved 13 November 2021.
  29. "makkarpov/scalingua: A simple gettext-like internationalization library for Scala". github.com. Retrieved 28 April 2016.
  30. "DanielBaulig/node-gettext: An adaption of Joshua I. Miller's Javascript Gettext library for node.js". GitHub.com. Retrieved 3 April 2016.
  31. "hulab/POLocalizedString: gettext for iOS/OS X/watchOS/tvOS". GitHub. hulab. 19 September 2019.
  32. "microsoft/LocalizedStringKit: Generate .strings files directly from your code". GitHub. Microsoft. 12 February 2020.