# Gettext

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Gettext
> Markdown URL: https://mediated.wiki/source/Gettext.md
> Source: https://en.wikipedia.org/wiki/Gettext
> Source revision: 1323850607
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{short description|GNU internationalization and localization software}}
{{multiple issues|
{{More citations needed|date=August 2008}}
{{how-to|date=January 2025}}
}}
{{lowercase title}}
{{Use dmy dates|date=December 2020}}
{{Infobox software
| name                   = gettext
| logo                   = 
| author                 = [Sun Microsystems](/source/Sun_Microsystems)<ref name=sun>{{cite web |url=https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/gettext.html |title=About gettext |website=gnu.org |access-date=2024-05-09}}</ref>
| developer              = various
| released               = {{start date and age|1990}}<ref name=history>{{cite web|url=http://compgroups.net/comp.unix.solaris/History-of-gettext-et-al |archive-url=https://web.archive.org/web/20120323032211/http://compgroups.net/comp.unix.solaris/History-of-gettext-et-al |url-status=usurped |archive-date=23 March 2012 |title=History of gettext() et al? - comp.unix.solaris |website=Compgroups.net |access-date=2016-04-03}}</ref>
| discontinued           = 
| latest release version = {{wikidata|property|preferred|references|edit|Q937302|P348|P548=Q2804309}}
| latest release date    = {{start date and age|{{wikidata|qualifier|preferred|single|Q937302|P348|P548=Q2804309|P577}}}}
| programming language   = 
| operating system       = [Cross-platform](/source/Cross-platform)
| platform               = 
| size                   = 
| language               = 
| language count         = <!-- DO NOT include this parameter unless you know what it does -->
| language footnote      = 
| genre                  = [Internationalization and localization](/source/Internationalization_and_localization)
| license                = Various [free software license](/source/free_software_license)s
| alexa                  = 
| repo                   = various based on OpenSolaris and GNU gettext
}}
In [computing](/source/computing), '''gettext''' is an [internationalization and localization](/source/internationalization_and_localization) (i18n and l10n) system commonly used for writing multilingual programs on [Unix-like](/source/Unix-like) computer [operating system](/source/operating_system)s. One of the main benefits of gettext is that it separates programming from translating.<ref>{{Cite web|url=https://www.linuxjournal.com/article/5966|title=Bridging the Digital Divide in South Africa {{!}} Linux Journal|last=Martindale|first=Linda|date=2002-11-01|website=linuxjournal.com|publisher=[Linux Journal](/source/Linux_Journal)|url-status=live|archive-url=https://archive.today/20190917015344/https://www.linuxjournal.com/article/5966|archive-date=17 September 2019|access-date=2019-09-17}}</ref> The most commonly used implementation of gettext is '''GNU gettext''',<ref>{{Cite web|url=https://www.linuxjournal.com/article/6176|title=Introduction to Internationalization Programming {{!}} Linux Journal|last=Tykhomyrov|first=Olexiy Ye|date=2002-11-01|website=linuxjournal.com|publisher=[Linux Journal](/source/Linux_Journal)|url-status=live|archive-url=https://archive.today/20190917020307/https://www.linuxjournal.com/article/6176|archive-date=17 September 2019|access-date=2019-09-17}}</ref> released by the [GNU Project](/source/GNU_Project) in 1995. The runtime library is '''libintl'''. gettext provides an option to use different strings for any number of [plural form](/source/plural_form)s of nouns, but this feature has no support for [grammatical gender](/source/grammatical_gender). The main [filename extension](/source/filename_extension)s used by this system are [.POT](/source/.POT) (Portable Object Template), [.PO](/source/Portable_object_(computing)) (Portable Object) and .MO (Machine Object).<ref name=":0" />

==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](/source/Sun_Microsystems) implemented the first gettext in 1993.<ref name=sun /> 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](/source/C_library), including [glibc](/source/glibc), implemented both.<ref>{{cite web |title=Message Translation |url=https://www.gnu.org/software/libc/manual/html_node/Message-Translation.html |website=The GNU C Library}}</ref> {{As of|2019|08}}, whether gettext should be part of POSIX was still a point of debate in the [Austin Group](/source/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](/source/global_variable) subject to multithreading problems) and its support for newer C-language extensions involving wide strings.<ref>{{cite web |title=0001122: POSIX should include gettext() and friends - Austin Group Defect Tracker |url=http://austingroupbugs.net/view.php?id=1122 |website=Austin Group Defect Tracker}}</ref>

The [GNU Project](/source/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.)<ref>{{cite web |title=The Programmer's View |url=http://ftp.gnu.org/old-gnu/Manuals/gettext-0.10.35/html_chapter/gettext_8.html |website=gettext 0.10.35| date=30 April 1998}}</ref> They released GNU gettext, a [free software](/source/free_software) implementation of the system in 1995.<ref name=history /> Gettext, GNU or not, has since been ported to many programming languages.<ref>{{cite web |title=GNU gettext utilities: List of Programming Languages |url=https://www.gnu.org/software/gettext/manual/html_node/List-of-Programming-Languages.html#List-of-Programming-Languages}}</ref> The simplicity of po and widespread editor support even led 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.<ref>{{cite web |title=po4a |url=https://po4a.org/index.php.en |website=po4a.org}}</ref><ref>{{cite web |title=The essential toolkit for localization engineers |url=https://toolkit.translatehouse.org/ |website=Translate Toolkit}}</ref>

==Operation==

===Programming===
thumb|Typical gettext workflow.  The de.po instance on the left shows a "renewing" of translations via msgmerge.
The basic interface of gettext is the {{code|gettext(const char*)}} function, which accepts a [string](/source/string_(computer_science)) that the user will see in the original language, usually English. To save typing time and reduce code clutter, this function is commonly [aliased](/source/aliasing_(computing)) to <code>_</code>:<ref>{{Cite web |date= |title=GNU gettext utilities: How Marks Appear in Sources |url=https://www.gnu.org/software/gettext/manual/gettext.html#How-Marks-Appear-in-Sources |url-status=live |archive-url=https://web.archive.org/web/20240325095308/https://www.gnu.org/software/gettext/manual/gettext.html#How-Marks-Appear-in-Sources |archive-date=2024-03-25 |access-date=2024-04-03 |website=www.gnu.org}}</ref>

<syntaxhighlight lang="C">
printf(gettext("My name is %s.\n"), my_name);
printf(_("My name is %s.\n"), my_name); // same, but shorter
</syntaxhighlight>

<code>gettext()</code> 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](/source/POSIX) <code>catgets()</code>,<ref>{{cite web|url=https://www.gnu.org/software/gettext/manual/html_node/catgets.html |website=gnu.org |title=About catgets |access-date=2017-10-24}}</ref> [AmigaOS](/source/AmigaOS) <code>GetString()</code>,<ref>{{Cite web |title=AmigaOS Manual: Python Modules and Packages - AmigaOS Documentation Wiki |url=https://wiki.amigaos.net/wiki/AmigaOS_Manual:_Python_Modules_and_Packages#GetString.28.29 |access-date=2023-07-09 |website=wiki.amigaos.net}}</ref> or [Microsoft Windows](/source/Microsoft_Windows) <code>LoadString()</code>  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 <code>cgettext()</code> that accept an additional "context" string.

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

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

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

<code>xgettext</code> 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](/source/C_(programming_language))-language [printf](/source/printf) format string):
<syntaxhighlight lang="po">
#. TRANSLATORS: %s contains the user's name as specified in Preferences
#, c-format
#: src/name.c:36
msgid "My name is %s.\n"
msgstr ""
</syntaxhighlight>

In POSIX [shell script](/source/shell_script), gettext provides a <code>gettext.sh</code> library one can include that provides the many same functions gettext provides in similar languages.<ref>{{cite web |title=GNU gettext utilities: sh |url=https://www.gnu.org/software/gettext/manual/html_node/sh.html}}</ref> [GNU bash](/source/GNU_bash) also has a simplified construct <code>$"msgid"</code> for the simple gettext function, although it depends on the C library to provide a <code>gettext()</code> function.<ref>{{cite web |title=GNU gettext utilities: bash |url=https://www.gnu.org/software/gettext/manual/html_node/bash.html}}</ref>

===Translating===
The translator derives a <code>.po</code> (Portable Object) file from the template using the <code>msginit</code> program, then fills out the translations.<ref name=msginit>{{cite web|url=https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/PO-Files.html |title=GNU gettext utilities: PO Files |website=Gnu.org |access-date=2016-04-03}}</ref> <code>msginit</code> initializes the translations so, for instance, for a French language translation, the command to run would be:<ref name=":0">{{cite web|url=http://www.icanlocalize.com/site/tutorials/how-to-translate-with-gettext-po-and-pot-files/ |title=How to Translate With GetText PO and POT Files |website=Icanlocalize.com |access-date=2016-04-03}}</ref>

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

This will create <code>fr.po</code>. The translator then edits the resultant file, either by hand or with a translation tool like [Poedit](/source/Poedit), or [Emacs](/source/Emacs) with its editing mode for <code>.po</code> files. An edited entry will look like:
<syntaxhighlight lang="po">
#: src/name.c:36
msgid "My name is %s.\n"
msgstr "Je m'appelle %s.\n"
</syntaxhighlight>

Finally, the .po files are compiled with <code>msgfmt</code> into binary <code>.mo</code> (Machine Object) files.  GNU gettext may use its own file name extension <code>.gmo</code> on systems with another gettext implementation.<ref>{{cite web | url=https://www.gnu.org/software/gettext/manual/html_node/Files.html | title=Files Conveying Translations|website=Gnu.org | access-date=2014-04-22}}</ref> These are now ready for distribution with the software package.

GNU <code>msgfmt</code> can also perform some checks relevant to the [format string](/source/format_string) used by the programming language. It also allows for outputting to language-specific formats other than MO;<ref>{{cite web |title=msgfmt Invocation |url=https://www.gnu.org/software/gettext/manual/html_node/msgfmt-Invocation.html |website=GNU gettext utilities}}</ref> the [X/Open](/source/X%2FOpen) equivalent is <code>gencat</code>.

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

===Running===
The user, on [Unix](/source/Unix)-type systems, sets the [environment variable](/source/environment_variable) <code>LC_MESSAGES</code>, and the program will display strings in the selected language, if there is an <code>.mo</code> file for it.

Users on [GNU variants](/source/GNU_variants) can also use the environment variable <code>LANGUAGE</code> instead. Its main difference from the Unix variable is that it supports multiple languages, separated with a colon, for fallback.<ref>{{cite web|url=https://www.gnu.org/software/gettext/manual/html_node/Locale-Environment-Variables.html#Locale-Environment-Variables |title=GNU gettext utilities: Locale Environment Variables |website=Gnu.org |access-date=2016-04-03}}</ref>

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

<syntaxhighlight lang="C">
// parameters: english singular, english plural, integer count
printf(ngettext("%d translated message", "%d translated messages", n), n);
</syntaxhighlight>

A header in the <code>""</code> (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](/source/%3F%3A). Suppose we want to translate for the [Slovene language](/source/Slovene_language):
<syntaxhighlight lang="po">
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"
</syntaxhighlight>

Since now there are four plural forms, the final po would look like:<!-- example from gettext-tools/po/sl.po -->
<syntaxhighlight lang="po">
#: src/msgfmt.c:876
#, c-format
msgid "%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"
</syntaxhighlight>

Reference plural rules for languages are provided by the [Unicode consortium](/source/Unicode_consortium).<ref>{{cite web |title=Language Plural Rules |url=https://unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html |website=unicode.org}}</ref> msginit also prefills the appropriate rule when creating a file for one specific language.<ref name=msginit/>

==Implementations==
In addition to [C](/source/C_(programming_language)), gettext has the following implementations: [C#](/source/C_Sharp_(programming_language)) for both [ASP.NET](/source/ASP.NET)<ref>{{cite web|url=https://code.google.com/p/gettext-cs-utils/ |title=Google Code Archive - Long-term storage for Google Code Project Hosting |website=Code.google.com |access-date=2016-04-03}}</ref><ref>{{cite web|url=https://github.com/turquoiseowl/i18n |title=turquoiseowl/i18n: Smart internationalization for ASP.NET |website=GitHub.com |access-date=2016-04-03}}</ref> and for [WPF](/source/Windows_Presentation_Foundation),<ref>{{cite web|url=https://github.com/robert-j-engdahl/ngettext-wpf |title=NGettext.Wpf - Proper internationalization support for WPF (via NGettext)|website=[GitHub](/source/GitHub)|date=16 August 2019}}</ref> [Perl](/source/Perl),<ref>{{cite web|url=https://github.com/gflohr/libintl-perl |title=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 |website=github.com |access-date=2017-09-14}}</ref> [PHP](/source/PHP),<ref>{{cite web|url=https://secure.php.net/manual/en/book.gettext.php |title=Gettext |website=php.net |access-date=2017-10-24}}</ref> [Python](/source/Python_(programming_language)),<ref>{{cite web|url=https://docs.python.org/library/gettext.html|title=gettext – Multilingual internationalization services – Python 3.7.0 documentation|website=docs.python.org|access-date=2018-09-21}}</ref> [R](/source/R_(programming_language)),<ref>{{Cite web|title=gettext: Translate Text Messages|url=https://rdrr.io/r/base/gettext.html|access-date=2021-11-13|website=rdrr.io|language=en}}</ref> [Scala](/source/Scala_(programming_language)),<ref>{{cite web|url=https://github.com/makkarpov/scalingua|title=makkarpov/scalingua: A simple gettext-like internationalization library for Scala|website=github.com|access-date=2016-04-28}}</ref> and [Node.js](/source/Node.js).<ref>{{cite web|url=https://github.com/DanielBaulig/node-gettext |title=DanielBaulig/node-gettext: An adaption of Joshua I. Miller's Javascript Gettext library for node.js |website=GitHub.com |access-date=2016-04-03}}</ref>

GNU gettext has native support for Objective-C, but there is no support for the [Swift programming language](/source/Swift_programming_language) yet. A commonly used gettext implementation on these Cocoa platforms is POLocalizedString.<ref>{{cite web |title=hulab/POLocalizedString: gettext for iOS/OS X/watchOS/tvOS |url=https://github.com/hulab/POLocalizedString |website=GitHub |publisher=hulab |date=19 September 2019}}</ref> The Microsoft Outlook for iOS team also provides a LocalizedStringsKit library with a gettext-like API.<ref>{{cite web |title=microsoft/LocalizedStringKit: Generate .strings files directly from your code |url=https://github.com/microsoft/LocalizedStringKit |website=GitHub |publisher=Microsoft |date=12 February 2020}}</ref>

==See also==
{{Commons category|GNU gettext}}
{{Portal|Free and open-source software}}
* [gtranslator](/source/gtranslator)
* [Poedit](/source/Poedit)
* [Translate Toolkit](/source/Translate_Toolkit)
* [Virtaal](/source/Virtaal)
* [Weblate](/source/Weblate)

==References==
{{Reflist}}

==External links==
* {{official website|https://www.gnu.org/software/gettext/gettext.html|Official GNU gettext site}}

Category:GNU Project software
Category:Internationalization and localization
Category:Software-localization tools

---
Adapted from the Wikipedia article [Gettext](https://en.wikipedia.org/wiki/Gettext) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Gettext?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
