Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

gettext

From Wikipedia, the free encyclopedia
GNU internationalization and localization software
This article has multiple issues. Please helpimprove it or discuss these issues on thetalk page.(Learn how and when to remove these messages)
icon
This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "Gettext" – news ·newspapers ·books ·scholar ·JSTOR
(August 2008) (Learn how and when to remove this message)
This articlecontainsinstructions or advice. Wikipedia is not a guidebook; please helprewrite such content to be encyclopedic or move it toWikiversity,Wikibooks, orWikivoyage.(January 2025)
(Learn how and when to remove this message)

gettext
Original authorSun Microsystems[1]
Developervarious
Initial release1990; 36 years ago (1990)[2]
Stable release
1.0[3] Edit this on Wikidata / 28 January 2026; 21 days ago (28 January 2026)
Operating systemCross-platform
TypeInternationalization and localization
LicenseVariousfree software licenses
Websitewww.gnu.org/software/gettext/ Edit this on Wikidata
Repositoryvarious based on OpenSolaris and GNU gettext

Incomputing,gettext is aninternationalization and localization (i18n and l10n) system commonly used for writing multilingual programs onUnix-like computeroperating systems. One of the main benefits of gettext is that it separates programming from translating.[4] The most commonly used implementation of gettext isGNU gettext,[5] released by theGNU Project in 1995. The runtime library islibintl. gettext provides an option to use different strings for any number ofplural forms of nouns, but this feature has no support forgrammatical gender. The mainfilename extensions used by this system are.POT (Portable Object Template),.PO (Portable Object) and .MO (Machine Object).[6]

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.[1] The Unix and POSIX developers never really agreed on what kind of interface to use (the other option is the X/Open catgets), so manyC libraries, includingglibc, implemented both.[7] As of August 2019[update], whether gettext should be part of POSIX was still a point of debate in theAustin Group, despite the fact that its old foe has already fallen out of use. Concerns cited included its dependence on the system-set locale (aglobal variable subject to multithreading problems) and its support for newer C-language extensions involving wide strings.[8]

TheGNU 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.)[9] They released GNU gettext, afree software implementation of the system in 1995.[2] Gettext, GNU or not, has since been ported to many programming languages.[10] 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.[11][12]

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 thegettext(const char*) function, which accepts astring that the user will see in the original language, usually English. To save typing time and reduce code clutter, this function is commonlyaliased to_:[13]

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 toPOSIXcatgets(),[14]AmigaOSGetString(),[15] orMicrosoft WindowsLoadString() 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 likecgettext() 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 isTRANSLATORS:.

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 aC-languageprintf 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 POSIXshell script, gettext provides agettext.sh library one can include that provides the many same functions gettext provides in similar languages.[16]GNU bash also has a simplified construct$"msgid" for the simple gettext function, although it depends on the C library to provide agettext() function.[17]

Translating

[edit]

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

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

This will createfr.po. The translator then edits the resultant file, either by hand or with a translation tool likePoedit, orEmacs 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 withmsgfmt into binary.mo (Machine Object) files. GNU gettext may use its own file name extension.gmo on systems with another gettext implementation.[19] These are now ready for distribution with the software package.

GNUmsgfmt can also perform some checks relevant to theformat string used by the programming language. It also allows for outputting to language-specific formats other than MO;[20] theX/Open equivalent isgencat.

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

Running

[edit]

The user, onUnix-type systems, sets theenvironment variableLC_MESSAGES, and the program will display strings in the selected language, if there is an.mo file for it.

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

Plural form

[edit]

Thengettext() interface accounts for the count of a noun in the string. As with the convention ofgettext(), it is often aliased toN_ 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-styleternary operator. Suppose we want to translate for theSlovene 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 theUnicode consortium.[22] msginit also prefills the appropriate rule when creating a file for one specific language.[18]

Implementations

[edit]

In addition toC, gettext has the following implementations:C# for bothASP.NET[23][24] and forWPF,[25]Perl,[26]PHP,[27]Python,[28]R,[29]Scala,[30] andNode.js.[31]

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

See also

[edit]
Wikimedia Commons has media related toGNU gettext.

References

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

External links

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Gettext&oldid=1323850607"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp