blob: e41f8bff0ddd46d3b78f7b285651acf272e36235 [file] [log] [blame]
Barry Warsaw0691a6b2000-08-30 03:27:10 +00001\section{\module{gettext} ---
2 Multilingual internationalization services}
3
4\declaremodule{standard}{gettext}
5\modulesynopsis{Multilingual internationalization services.}
Andrew M. Kuchlingc62af022004-01-08 15:01:08 +00006\moduleauthor{Barry A. Warsaw}{barry@zope.com}
7\sectionauthor{Barry A. Warsaw}{barry@zope.com}
Barry Warsaw0691a6b2000-08-30 03:27:10 +00008
9
10The \module{gettext} module provides internationalization (I18N) and
11localization (L10N) services for your Python modules and applications.
Fred Draked576e9d2000-08-30 04:19:20 +000012It supports both the GNU \code{gettext} message catalog API and a
Barry Warsaw0691a6b2000-08-30 03:27:10 +000013higher level, class-based API that may be more appropriate for Python
14files. The interface described below allows you to write your
15module and application messages in one natural language, and provide a
16catalog of translated messages for running under different natural
17languages.
18
19Some hints on localizing your Python modules and applications are also
20given.
21
22\subsection{GNU \program{gettext} API}
23
24The \module{gettext} module defines the following API, which is very
25similar to the GNU \program{gettext} API. If you use this API you
26will affect the translation of your entire application globally. Often
27this is what you want if your application is monolingual, with the choice
28of language dependent on the locale of your user. If you are
29localizing a Python module, or if your application needs to switch
30languages on the fly, you probably want to use the class-based API
31instead.
32
Fred Draked576e9d2000-08-30 04:19:20 +000033\begin{funcdesc}{bindtextdomain}{domain\optional{, localedir}}
Barry Warsaw0691a6b2000-08-30 03:27:10 +000034Bind the \var{domain} to the locale directory
35\var{localedir}. More concretely, \module{gettext} will look for
Fred Draked576e9d2000-08-30 04:19:20 +000036binary \file{.mo} files for the given domain using the path (on \UNIX):
Barry Warsaw0691a6b2000-08-30 03:27:10 +000037\file{\var{localedir}/\var{language}/LC_MESSAGES/\var{domain}.mo},
38where \var{languages} is searched for in the environment variables
Fred Draked576e9d2000-08-30 04:19:20 +000039\envvar{LANGUAGE}, \envvar{LC_ALL}, \envvar{LC_MESSAGES}, and
40\envvar{LANG} respectively.
Barry Warsaw0691a6b2000-08-30 03:27:10 +000041
Fred Draked576e9d2000-08-30 04:19:20 +000042If \var{localedir} is omitted or \code{None}, then the current binding
43for \var{domain} is returned.\footnote{
Fred Drake91f2f262001-07-06 19:28:48 +000044 The default locale directory is system dependent; for example,
45 on RedHat Linux it is \file{/usr/share/locale}, but on Solaris
46 it is \file{/usr/lib/locale}. The \module{gettext} module
47 does not try to support these system dependent defaults;
48 instead its default is \file{\code{sys.prefix}/share/locale}.
49 For this reason, it is always best to call
Fred Draked576e9d2000-08-30 04:19:20 +000050 \function{bindtextdomain()} with an explicit absolute path at
51 the start of your application.}
Barry Warsaw0691a6b2000-08-30 03:27:10 +000052\end{funcdesc}
53
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +000054\begin{funcdesc}{bind_textdomain_codeset}{domain\optional{, codeset}}
55Bind the \var{domain} to \var{codeset}, changing the encoding of
56strings returned by the \function{gettext()} family of functions.
57If \var{codeset} is omitted, then the current binding is returned.
58
59\versionadded{2.4}
60\end{funcdesc}
61
Fred Draked576e9d2000-08-30 04:19:20 +000062\begin{funcdesc}{textdomain}{\optional{domain}}
Barry Warsaw0691a6b2000-08-30 03:27:10 +000063Change or query the current global domain. If \var{domain} is
64\code{None}, then the current global domain is returned, otherwise the
65global domain is set to \var{domain}, which is returned.
66\end{funcdesc}
67
68\begin{funcdesc}{gettext}{message}
69Return the localized translation of \var{message}, based on the
70current global domain, language, and locale directory. This function
71is usually aliased as \function{_} in the local namespace (see
72examples below).
73\end{funcdesc}
74
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +000075\begin{funcdesc}{lgettext}{message}
76Equivalent to \function{gettext()}, but the translation is returned
77in the preferred system encoding, if no other encoding was explicitly
78set with \function{bind_textdomain_codeset()}.
79
80\versionadded{2.4}
81\end{funcdesc}
82
Barry Warsaw0691a6b2000-08-30 03:27:10 +000083\begin{funcdesc}{dgettext}{domain, message}
84Like \function{gettext()}, but look the message up in the specified
85\var{domain}.
86\end{funcdesc}
87
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +000088\begin{funcdesc}{ldgettext}{domain, message}
89Equivalent to \function{dgettext()}, but the translation is returned
90in the preferred system encoding, if no other encoding was explicitly
91set with \function{bind_textdomain_codeset()}.
92
93\versionadded{2.4}
94\end{funcdesc}
95
Martin v. Löwisd8996052002-11-21 21:45:32 +000096\begin{funcdesc}{ngettext}{singular, plural, n}
97
98Like \function{gettext()}, but consider plural forms. If a translation
99is found, apply the plural formula to \var{n}, and return the
100resulting message (some languages have more than two plural forms).
101If no translation is found, return \var{singular} if \var{n} is 1;
102return \var{plural} otherwise.
103
104The Plural formula is taken from the catalog header. It is a C or
105Python expression that has a free variable n; the expression evaluates
106to the index of the plural in the catalog. See the GNU gettext
107documentation for the precise syntax to be used in .po files, and the
108formulas for a variety of languages.
109
110\versionadded{2.3}
111
112\end{funcdesc}
113
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000114\begin{funcdesc}{lngettext}{singular, plural, n}
115Equivalent to \function{ngettext()}, but the translation is returned
116in the preferred system encoding, if no other encoding was explicitly
117set with \function{bind_textdomain_codeset()}.
118
119\versionadded{2.4}
120\end{funcdesc}
121
Martin v. Löwisd8996052002-11-21 21:45:32 +0000122\begin{funcdesc}{dngettext}{domain, singular, plural, n}
123Like \function{ngettext()}, but look the message up in the specified
124\var{domain}.
125
126\versionadded{2.3}
127\end{funcdesc}
128
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000129\begin{funcdesc}{ldngettext}{domain, singular, plural, n}
130Equivalent to \function{dngettext()}, but the translation is returned
131in the preferred system encoding, if no other encoding was explicitly
132set with \function{bind_textdomain_codeset()}.
133
134\versionadded{2.4}
135\end{funcdesc}
136
137
Martin v. Löwisd8996052002-11-21 21:45:32 +0000138
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000139Note that GNU \program{gettext} also defines a \function{dcgettext()}
140method, but this was deemed not useful and so it is currently
141unimplemented.
142
143Here's an example of typical usage for this API:
144
145\begin{verbatim}
146import gettext
147gettext.bindtextdomain('myapplication', '/path/to/my/language/directory')
148gettext.textdomain('myapplication')
149_ = gettext.gettext
150# ...
151print _('This is a translatable string.')
152\end{verbatim}
153
154\subsection{Class-based API}
155
156The class-based API of the \module{gettext} module gives you more
157flexibility and greater convenience than the GNU \program{gettext}
158API. It is the recommended way of localizing your Python applications and
159modules. \module{gettext} defines a ``translations'' class which
160implements the parsing of GNU \file{.mo} format files, and has methods
161for returning either standard 8-bit strings or Unicode strings.
Andrew M. Kuchling099bd522004-07-10 16:01:10 +0000162Instances of this ``translations'' class can also install themselves
163in the built-in namespace as the function \function{_()}.
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000164
Martin v. Löwisa55ffae2002-01-11 06:58:49 +0000165\begin{funcdesc}{find}{domain\optional{, localedir\optional{,
166 languages\optional{, all}}}}
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000167This function implements the standard \file{.mo} file search
168algorithm. It takes a \var{domain}, identical to what
Barry Warsaw91b81c42001-10-18 19:41:48 +0000169\function{textdomain()} takes. Optional \var{localedir} is as in
170\function{bindtextdomain()} Optional \var{languages} is a list of
171strings, where each string is a language code.
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000172
173If \var{localedir} is not given, then the default system locale
Fred Draked576e9d2000-08-30 04:19:20 +0000174directory is used.\footnote{See the footnote for
175\function{bindtextdomain()} above.} If \var{languages} is not given,
176then the following environment variables are searched: \envvar{LANGUAGE},
177\envvar{LC_ALL}, \envvar{LC_MESSAGES}, and \envvar{LANG}. The first one
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000178returning a non-empty value is used for the \var{languages} variable.
Barry Warsaw91b81c42001-10-18 19:41:48 +0000179The environment variables should contain a colon separated list of
180languages, which will be split on the colon to produce the expected
181list of language code strings.
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000182
183\function{find()} then expands and normalizes the languages, and then
184iterates through them, searching for an existing file built of these
185components:
186
187\file{\var{localedir}/\var{language}/LC_MESSAGES/\var{domain}.mo}
188
189The first such file name that exists is returned by \function{find()}.
Martin v. Löwisa55ffae2002-01-11 06:58:49 +0000190If no such file is found, then \code{None} is returned. If \var{all}
191is given, it returns a list of all file names, in the order in which
192they appear in the languages list or the environment variables.
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000193\end{funcdesc}
194
Fred Draked576e9d2000-08-30 04:19:20 +0000195\begin{funcdesc}{translation}{domain\optional{, localedir\optional{,
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000196 languages\optional{, class_\optional{,
197 fallback\optional{, codeset}}}}}}
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000198Return a \class{Translations} instance based on the \var{domain},
199\var{localedir}, and \var{languages}, which are first passed to
Martin v. Löwisa55ffae2002-01-11 06:58:49 +0000200\function{find()} to get a list of the
201associated \file{.mo} file paths. Instances with
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000202identical \file{.mo} file names are cached. The actual class instantiated
203is either \var{class_} if provided, otherwise
204\class{GNUTranslations}. The class's constructor must take a single
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000205file object argument. If provided, \var{codeset} will change the
206charset used to encode translated strings.
Martin v. Löwisa55ffae2002-01-11 06:58:49 +0000207
208If multiple files are found, later files are used as fallbacks for
209earlier ones. To allow setting the fallback, \function{copy.copy}
210is used to clone each translation object from the cache; the actual
211instance data is still shared with the cache.
212
213If no \file{.mo} file is found, this function raises
214\exception{IOError} if \var{fallback} is false (which is the default),
215and returns a \class{NullTranslations} instance if \var{fallback} is
216true.
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000217
218\versionchanged[Added the \var{codeset} parameter]{2.4}
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000219\end{funcdesc}
220
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000221\begin{funcdesc}{install}{domain\optional{, localedir\optional{, unicode
Georg Brandl602b9ba2006-02-19 13:26:36 +0000222 \optional{, codeset\optional{, names}}}}}
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000223This installs the function \function{_} in Python's builtin namespace,
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000224based on \var{domain}, \var{localedir}, and \var{codeset} which are
225passed to the function \function{translation()}. The \var{unicode}
226flag is passed to the resulting translation object's \method{install}
227method.
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000228
Georg Brandl602b9ba2006-02-19 13:26:36 +0000229For the \var{names} parameter, please see the description of the
230translation object's \method{install} method.
231
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000232As seen below, you usually mark the strings in your application that are
Fred Drake91f2f262001-07-06 19:28:48 +0000233candidates for translation, by wrapping them in a call to the
234\function{_()} function, like this:
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000235
236\begin{verbatim}
237print _('This string will be translated.')
238\end{verbatim}
239
240For convenience, you want the \function{_()} function to be installed in
241Python's builtin namespace, so it is easily accessible in all modules
242of your application.
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000243
244\versionchanged[Added the \var{codeset} parameter]{2.4}
Georg Brandl602b9ba2006-02-19 13:26:36 +0000245\versionchanged[Added the \var{names} parameter]{2.5}
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000246\end{funcdesc}
247
248\subsubsection{The \class{NullTranslations} class}
249Translation classes are what actually implement the translation of
250original source file message strings to translated message strings.
251The base class used by all translation classes is
252\class{NullTranslations}; this provides the basic interface you can use
253to write your own specialized translation classes. Here are the
254methods of \class{NullTranslations}:
255
Fred Draked576e9d2000-08-30 04:19:20 +0000256\begin{methoddesc}[NullTranslations]{__init__}{\optional{fp}}
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000257Takes an optional file object \var{fp}, which is ignored by the base
258class. Initializes ``protected'' instance variables \var{_info} and
Martin v. Löwisa55ffae2002-01-11 06:58:49 +0000259\var{_charset} which are set by derived classes, as well as \var{_fallback},
260which is set through \method{add_fallback}. It then calls
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000261\code{self._parse(fp)} if \var{fp} is not \code{None}.
262\end{methoddesc}
263
264\begin{methoddesc}[NullTranslations]{_parse}{fp}
265No-op'd in the base class, this method takes file object \var{fp}, and
266reads the data from the file, initializing its message catalog. If
267you have an unsupported message catalog file format, you should
268override this method to parse your format.
269\end{methoddesc}
270
Martin v. Löwiscebcc612003-08-05 05:54:15 +0000271\begin{methoddesc}[NullTranslations]{add_fallback}{fallback}
Martin v. Löwisa55ffae2002-01-11 06:58:49 +0000272Add \var{fallback} as the fallback object for the current translation
273object. A translation object should consult the fallback if it cannot
274provide a translation for a given message.
275\end{methoddesc}
276
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000277\begin{methoddesc}[NullTranslations]{gettext}{message}
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000278If a fallback has been set, forward \method{gettext()} to the fallback.
Martin v. Löwisa55ffae2002-01-11 06:58:49 +0000279Otherwise, return the translated message. Overridden in derived classes.
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000280\end{methoddesc}
281
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000282\begin{methoddesc}[NullTranslations]{lgettext}{message}
283If a fallback has been set, forward \method{lgettext()} to the fallback.
284Otherwise, return the translated message. Overridden in derived classes.
285
286\versionadded{2.4}
287\end{methoddesc}
288
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000289\begin{methoddesc}[NullTranslations]{ugettext}{message}
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000290If a fallback has been set, forward \method{ugettext()} to the fallback.
Martin v. Löwisa55ffae2002-01-11 06:58:49 +0000291Otherwise, return the translated message as a Unicode string.
292Overridden in derived classes.
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000293\end{methoddesc}
294
Martin v. Löwisd8996052002-11-21 21:45:32 +0000295\begin{methoddesc}[NullTranslations]{ngettext}{singular, plural, n}
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000296If a fallback has been set, forward \method{ngettext()} to the fallback.
Martin v. Löwisd8996052002-11-21 21:45:32 +0000297Otherwise, return the translated message. Overridden in derived classes.
298
299\versionadded{2.3}
300\end{methoddesc}
301
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000302\begin{methoddesc}[NullTranslations]{lngettext}{singular, plural, n}
303If a fallback has been set, forward \method{ngettext()} to the fallback.
304Otherwise, return the translated message. Overridden in derived classes.
305
306\versionadded{2.4}
307\end{methoddesc}
308
Martin v. Löwisd8996052002-11-21 21:45:32 +0000309\begin{methoddesc}[NullTranslations]{ungettext}{singular, plural, n}
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000310If a fallback has been set, forward \method{ungettext()} to the fallback.
Martin v. Löwisd8996052002-11-21 21:45:32 +0000311Otherwise, return the translated message as a Unicode string.
312Overridden in derived classes.
313
314\versionadded{2.3}
315\end{methoddesc}
316
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000317\begin{methoddesc}[NullTranslations]{info}{}
Fred Draked576e9d2000-08-30 04:19:20 +0000318Return the ``protected'' \member{_info} variable.
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000319\end{methoddesc}
320
321\begin{methoddesc}[NullTranslations]{charset}{}
Fred Draked576e9d2000-08-30 04:19:20 +0000322Return the ``protected'' \member{_charset} variable.
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000323\end{methoddesc}
324
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000325\begin{methoddesc}[NullTranslations]{output_charset}{}
326Return the ``protected'' \member{_output_charset} variable, which
327defines the encoding used to return translated messages.
328
329\versionadded{2.4}
330\end{methoddesc}
331
332\begin{methoddesc}[NullTranslations]{set_output_charset}{charset}
333Change the ``protected'' \member{_output_charset} variable, which
334defines the encoding used to return translated messages.
335
336\versionadded{2.4}
337\end{methoddesc}
338
Georg Brandl602b9ba2006-02-19 13:26:36 +0000339\begin{methoddesc}[NullTranslations]{install}{\optional{unicode
340 \optional{, names}}}
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000341If the \var{unicode} flag is false, this method installs
Fred Draked576e9d2000-08-30 04:19:20 +0000342\method{self.gettext()} into the built-in namespace, binding it to
343\samp{_}. If \var{unicode} is true, it binds \method{self.ugettext()}
344instead. By default, \var{unicode} is false.
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000345
Georg Brandl602b9ba2006-02-19 13:26:36 +0000346If the \var{names} parameter is given, it must be a sequence containing
347the names of functions you want to install in the builtin namespace in
348addition to \function{_()}. Supported names are \code{'gettext'} (bound
349to \method{self.gettext()} or \method{self.ugettext()} according to the
350\var{unicode} flag), \code{'ngettext'} (bound to \method{self.ngettext()}
351or \method{self.ungettext()} according to the \var{unicode} flag),
352\code{'lgettext'} and \code{'lngettext'}.
353
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000354Note that this is only one way, albeit the most convenient way, to
355make the \function{_} function available to your application. Because it
356affects the entire application globally, and specifically the built-in
357namespace, localized modules should never install \function{_}.
358Instead, they should use this code to make \function{_} available to
359their module:
360
361\begin{verbatim}
362import gettext
363t = gettext.translation('mymodule', ...)
364_ = t.gettext
365\end{verbatim}
366
367This puts \function{_} only in the module's global namespace and so
368only affects calls within this module.
Georg Brandl602b9ba2006-02-19 13:26:36 +0000369
370\versionchanged[Added the \var{names} parameter]{2.5}
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000371\end{methoddesc}
372
373\subsubsection{The \class{GNUTranslations} class}
374
375The \module{gettext} module provides one additional class derived from
376\class{NullTranslations}: \class{GNUTranslations}. This class
377overrides \method{_parse()} to enable reading GNU \program{gettext}
378format \file{.mo} files in both big-endian and little-endian format.
Barry Warsaw50889232003-04-24 18:14:49 +0000379It also coerces both message ids and message strings to Unicode.
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000380
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000381\class{GNUTranslations} parses optional meta-data out of the
382translation catalog. It is convention with GNU \program{gettext} to
383include meta-data as the translation for the empty string. This
Barry Warsaw50889232003-04-24 18:14:49 +0000384meta-data is in \rfc{822}-style \code{key: value} pairs, and should
385contain the \code{Project-Id-Version} key. If the key
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000386\code{Content-Type} is found, then the \code{charset} property is used
387to initialize the ``protected'' \member{_charset} instance variable,
Barry Warsaw50889232003-04-24 18:14:49 +0000388defaulting to \code{None} if not found. If the charset encoding is
389specified, then all message ids and message strings read from the
390catalog are converted to Unicode using this encoding. The
391\method{ugettext()} method always returns a Unicode, while the
392\method{gettext()} returns an encoded 8-bit string. For the message
393id arguments of both methods, either Unicode strings or 8-bit strings
394containing only US-ASCII characters are acceptable. Note that the
395Unicode version of the methods (i.e. \method{ugettext()} and
396\method{ungettext()}) are the recommended interface to use for
397internationalized Python programs.
398
399The entire set of key/value pairs are placed into a dictionary and set
400as the ``protected'' \member{_info} instance variable.
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000401
402If the \file{.mo} file's magic number is invalid, or if other problems
403occur while reading the file, instantiating a \class{GNUTranslations} class
404can raise \exception{IOError}.
405
Barry Warsaw50889232003-04-24 18:14:49 +0000406The following methods are overridden from the base class implementation:
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000407
Barry Warsaw50889232003-04-24 18:14:49 +0000408\begin{methoddesc}[GNUTranslations]{gettext}{message}
409Look up the \var{message} id in the catalog and return the
410corresponding message string, as an 8-bit string encoded with the
411catalog's charset encoding, if known. If there is no entry in the
412catalog for the \var{message} id, and a fallback has been set, the
413look up is forwarded to the fallback's \method{gettext()} method.
414Otherwise, the \var{message} id is returned.
415\end{methoddesc}
Martin v. Löwisd8996052002-11-21 21:45:32 +0000416
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000417\begin{methoddesc}[GNUTranslations]{lgettext}{message}
418Equivalent to \method{gettext()}, but the translation is returned
419in the preferred system encoding, if no other encoding was explicitly
420set with \method{set_output_charset()}.
421
422\versionadded{2.4}
423\end{methoddesc}
424
Barry Warsaw50889232003-04-24 18:14:49 +0000425\begin{methoddesc}[GNUTranslations]{ugettext}{message}
426Look up the \var{message} id in the catalog and return the
427corresponding message string, as a Unicode string. If there is no
428entry in the catalog for the \var{message} id, and a fallback has been
429set, the look up is forwarded to the fallback's \method{ugettext()}
430method. Otherwise, the \var{message} id is returned.
431\end{methoddesc}
432
433\begin{methoddesc}[GNUTranslations]{ngettext}{singular, plural, n}
434Do a plural-forms lookup of a message id. \var{singular} is used as
435the message id for purposes of lookup in the catalog, while \var{n} is
436used to determine which plural form to use. The returned message
437string is an 8-bit string encoded with the catalog's charset encoding,
438if known.
439
440If the message id is not found in the catalog, and a fallback is
441specified, the request is forwarded to the fallback's
442\method{ngettext()} method. Otherwise, when \var{n} is 1 \var{singular} is
443returned, and \var{plural} is returned in all other cases.
444
445\versionadded{2.3}
446\end{methoddesc}
447
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000448\begin{methoddesc}[GNUTranslations]{lngettext}{singular, plural, n}
449Equivalent to \method{gettext()}, but the translation is returned
450in the preferred system encoding, if no other encoding was explicitly
451set with \method{set_output_charset()}.
452
453\versionadded{2.4}
454\end{methoddesc}
455
Barry Warsaw50889232003-04-24 18:14:49 +0000456\begin{methoddesc}[GNUTranslations]{ungettext}{singular, plural, n}
457Do a plural-forms lookup of a message id. \var{singular} is used as
458the message id for purposes of lookup in the catalog, while \var{n} is
459used to determine which plural form to use. The returned message
460string is a Unicode string.
461
462If the message id is not found in the catalog, and a fallback is
463specified, the request is forwarded to the fallback's
464\method{ungettext()} method. Otherwise, when \var{n} is 1 \var{singular} is
465returned, and \var{plural} is returned in all other cases.
466
467Here is an example:
468
469\begin{verbatim}
470n = len(os.listdir('.'))
471cat = GNUTranslations(somefile)
472message = cat.ungettext(
473 'There is %(num)d file in this directory',
474 'There are %(num)d files in this directory',
Andrew M. Kuchling099bd522004-07-10 16:01:10 +0000475 n) % {'num': n}
Barry Warsaw50889232003-04-24 18:14:49 +0000476\end{verbatim}
477
478\versionadded{2.3}
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000479\end{methoddesc}
480
Fred Draked0726c32000-09-07 18:55:08 +0000481\subsubsection{Solaris message catalog support}
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000482
483The Solaris operating system defines its own binary
484\file{.mo} file format, but since no documentation can be found on
485this format, it is not supported at this time.
486
487\subsubsection{The Catalog constructor}
488
Fred Draked0726c32000-09-07 18:55:08 +0000489GNOME\index{GNOME} uses a version of the \module{gettext} module by
490James Henstridge, but this version has a slightly different API. Its
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000491documented usage was:
492
493\begin{verbatim}
494import gettext
495cat = gettext.Catalog(domain, localedir)
496_ = cat.gettext
497print _('hello world')
498\end{verbatim}
499
500For compatibility with this older module, the function
Raymond Hettingerf17d65d2003-08-12 00:01:16 +0000501\function{Catalog()} is an alias for the \function{translation()}
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000502function described above.
503
504One difference between this module and Henstridge's: his catalog
505objects supported access through a mapping API, but this appears to be
506unused and so is not currently supported.
507
508\subsection{Internationalizing your programs and modules}
509Internationalization (I18N) refers to the operation by which a program
510is made aware of multiple languages. Localization (L10N) refers to
511the adaptation of your program, once internationalized, to the local
512language and cultural habits. In order to provide multilingual
513messages for your Python programs, you need to take the following
514steps:
515
516\begin{enumerate}
517 \item prepare your program or module by specially marking
518 translatable strings
519 \item run a suite of tools over your marked files to generate raw
520 messages catalogs
521 \item create language specific translations of the message catalogs
522 \item use the \module{gettext} module so that message strings are
523 properly translated
524\end{enumerate}
525
526In order to prepare your code for I18N, you need to look at all the
527strings in your files. Any string that needs to be translated
Fred Drake91f2f262001-07-06 19:28:48 +0000528should be marked by wrapping it in \code{_('...')} --- that is, a call
529to the function \function{_()}. For example:
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000530
531\begin{verbatim}
532filename = 'mylog.txt'
533message = _('writing a log message')
534fp = open(filename, 'w')
535fp.write(message)
536fp.close()
537\end{verbatim}
538
Fred Draked576e9d2000-08-30 04:19:20 +0000539In this example, the string \code{'writing a log message'} is marked as
540a candidate for translation, while the strings \code{'mylog.txt'} and
541\code{'w'} are not.
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000542
Barry Warsawb4162902001-01-31 21:21:45 +0000543The Python distribution comes with two tools which help you generate
544the message catalogs once you've prepared your source code. These may
545or may not be available from a binary distribution, but they can be
546found in a source distribution, in the \file{Tools/i18n} directory.
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000547
Barry Warsawb4162902001-01-31 21:21:45 +0000548The \program{pygettext}\footnote{Fran\c cois Pinard has
549written a program called
Barry Warsawddef8882000-09-13 12:04:47 +0000550\program{xpot} which does a similar job. It is available as part of
551his \program{po-utils} package at
Fred Drakeef139492003-07-22 00:49:11 +0000552\url{http://www.iro.umontreal.ca/contrib/po-utils/HTML/}.} program
Barry Warsawb4162902001-01-31 21:21:45 +0000553scans all your Python source code looking for the strings you
554previously marked as translatable. It is similar to the GNU
555\program{gettext} program except that it understands all the
Fred Drake2884d6d2003-07-02 12:27:43 +0000556intricacies of Python source code, but knows nothing about C or \Cpp
Barry Warsawb4162902001-01-31 21:21:45 +0000557source code. You don't need GNU \code{gettext} unless you're also
Fred Drake91f2f262001-07-06 19:28:48 +0000558going to be translating C code (such as C extension modules).
Barry Warsawb4162902001-01-31 21:21:45 +0000559
560\program{pygettext} generates textual Uniforum-style human readable
561message catalog \file{.pot} files, essentially structured human
562readable files which contain every marked string in the source code,
563along with a placeholder for the translation strings.
564\program{pygettext} is a command line script that supports a similar
565command line interface as \program{xgettext}; for details on its use,
566run:
567
568\begin{verbatim}
569pygettext.py --help
570\end{verbatim}
571
572Copies of these \file{.pot} files are then handed over to the
573individual human translators who write language-specific versions for
574every supported natural language. They send you back the filled in
575language-specific versions as a \file{.po} file. Using the
576\program{msgfmt.py}\footnote{\program{msgfmt.py} is binary
577compatible with GNU \program{msgfmt} except that it provides a
578simpler, all-Python implementation. With this and
579\program{pygettext.py}, you generally won't need to install the GNU
580\program{gettext} package to internationalize your Python
581applications.} program (in the \file{Tools/i18n} directory), you take the
582\file{.po} files from your translators and generate the
583machine-readable \file{.mo} binary catalog files. The \file{.mo}
584files are what the \module{gettext} module uses for the actual
585translation processing during run-time.
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000586
587How you use the \module{gettext} module in your code depends on
588whether you are internationalizing your entire application or a single
589module.
590
591\subsubsection{Localizing your module}
592
593If you are localizing your module, you must take care not to make
594global changes, e.g. to the built-in namespace. You should not use
Fred Draked576e9d2000-08-30 04:19:20 +0000595the GNU \code{gettext} API but instead the class-based API.
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000596
597Let's say your module is called ``spam'' and the module's various
598natural language translation \file{.mo} files reside in
Fred Draked576e9d2000-08-30 04:19:20 +0000599\file{/usr/share/locale} in GNU \program{gettext} format. Here's what
600you would put at the top of your module:
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000601
602\begin{verbatim}
603import gettext
604t = gettext.translation('spam', '/usr/share/locale')
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000605_ = t.lgettext
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000606\end{verbatim}
607
608If your translators were providing you with Unicode strings in their
609\file{.po} files, you'd instead do:
610
611\begin{verbatim}
612import gettext
613t = gettext.translation('spam', '/usr/share/locale')
614_ = t.ugettext
615\end{verbatim}
616
617\subsubsection{Localizing your application}
618
619If you are localizing your application, you can install the \function{_()}
620function globally into the built-in namespace, usually in the main driver file
621of your application. This will let all your application-specific
622files just use \code{_('...')} without having to explicitly install it in
623each file.
624
625In the simple case then, you need only add the following bit of code
626to the main driver file of your application:
627
628\begin{verbatim}
629import gettext
630gettext.install('myapplication')
631\end{verbatim}
632
Fred Draked576e9d2000-08-30 04:19:20 +0000633If you need to set the locale directory or the \var{unicode} flag,
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000634you can pass these into the \function{install()} function:
635
636\begin{verbatim}
637import gettext
638gettext.install('myapplication', '/usr/share/locale', unicode=1)
639\end{verbatim}
640
641\subsubsection{Changing languages on the fly}
642
643If your program needs to support many languages at the same time, you
644may want to create multiple translation instances and then switch
645between them explicitly, like so:
646
647\begin{verbatim}
648import gettext
649
Johannes Gijsbers2014c032004-09-11 17:33:25 +0000650lang1 = gettext.translation('myapplication', languages=['en'])
651lang2 = gettext.translation('myapplication', languages=['fr'])
652lang3 = gettext.translation('myapplication', languages=['de'])
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000653
654# start by using language1
655lang1.install()
656
657# ... time goes by, user selects language 2
658lang2.install()
659
660# ... more time goes by, user selects language 3
661lang3.install()
662\end{verbatim}
663
664\subsubsection{Deferred translations}
665
Neal Norwitz563d12d2002-06-24 02:22:39 +0000666In most coding situations, strings are translated where they are coded.
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000667Occasionally however, you need to mark strings for translation, but
668defer actual translation until later. A classic example is:
669
670\begin{verbatim}
671animals = ['mollusk',
672 'albatross',
673 'rat',
674 'penguin',
675 'python',
676 ]
677# ...
678for a in animals:
679 print a
680\end{verbatim}
681
682Here, you want to mark the strings in the \code{animals} list as being
683translatable, but you don't actually want to translate them until they
684are printed.
685
686Here is one way you can handle this situation:
687
688\begin{verbatim}
689def _(message): return message
690
691animals = [_('mollusk'),
692 _('albatross'),
693 _('rat'),
694 _('penguin'),
695 _('python'),
696 ]
697
698del _
699
700# ...
701for a in animals:
702 print _(a)
703\end{verbatim}
704
705This works because the dummy definition of \function{_()} simply returns
706the string unchanged. And this dummy definition will temporarily
707override any definition of \function{_()} in the built-in namespace
Fred Draked576e9d2000-08-30 04:19:20 +0000708(until the \keyword{del} command).
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000709Take care, though if you have a previous definition of \function{_} in
710the local namespace.
711
712Note that the second use of \function{_()} will not identify ``a'' as
713being translatable to the \program{pygettext} program, since it is not
714a string.
715
716Another way to handle this is with the following example:
717
718\begin{verbatim}
719def N_(message): return message
720
721animals = [N_('mollusk'),
722 N_('albatross'),
723 N_('rat'),
724 N_('penguin'),
725 N_('python'),
726 ]
727
728# ...
729for a in animals:
730 print _(a)
731\end{verbatim}
732
733In this case, you are marking translatable strings with the function
Fred Draked576e9d2000-08-30 04:19:20 +0000734\function{N_()},\footnote{The choice of \function{N_()} here is totally
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000735arbitrary; it could have just as easily been
Fred Draked576e9d2000-08-30 04:19:20 +0000736\function{MarkThisStringForTranslation()}.
737} which won't conflict with any definition of
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000738\function{_()}. However, you will need to teach your message extraction
739program to look for translatable strings marked with \function{N_()}.
740\program{pygettext} and \program{xpot} both support this through the
741use of command line switches.
742
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000743\subsubsection{\function{gettext()} vs. \function{lgettext()}}
744In Python 2.4 the \function{lgettext()} family of functions were
745introduced. The intention of these functions is to provide an
746alternative which is more compliant with the current
747implementation of GNU gettext. Unlike \function{gettext()}, which
748returns strings encoded with the same codeset used in the
749translation file, \function{lgettext()} will return strings
750encoded with the preferred system encoding, as returned by
751\function{locale.getpreferredencoding()}. Also notice that
752Python 2.4 introduces new functions to explicitly choose
753the codeset used in translated strings. If a codeset is explicitly
754set, even \function{lgettext()} will return translated strings in
755the requested codeset, as would be expected in the GNU gettext
756implementation.
757
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000758\subsection{Acknowledgements}
759
760The following people contributed code, feedback, design suggestions,
761previous implementations, and valuable experience to the creation of
762this module:
763
764\begin{itemize}
765 \item Peter Funk
766 \item James Henstridge
Fred Drake74f5a562002-11-22 14:28:53 +0000767 \item Juan David Ib\'a\~nez Palomar
Fred Draked576e9d2000-08-30 04:19:20 +0000768 \item Marc-Andr\'e Lemburg
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000769 \item Martin von L\"owis
770 \item Fran\c cois Pinard
771 \item Barry Warsaw
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000772 \item Gustavo Niemeyer
Barry Warsaw0691a6b2000-08-30 03:27:10 +0000773\end{itemize}