Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Gettext
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Programming=== [[File:Gettext.svg|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 (computer science)|string]] that the user will see in the original language, usually English. To save typing time and reduce code clutter, this function is commonly [[aliasing (computing)|aliased]] 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]] <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]] <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]] <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 (programming language)|C]]-language [[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]], 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]] 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>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)