Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 1 | \section{\module{gettext} --- |
| 2 | Multilingual internationalization services} |
| 3 | |
| 4 | \declaremodule{standard}{gettext} |
| 5 | \modulesynopsis{Multilingual internationalization services.} |
Barry Warsaw | a770e86 | 2001-01-15 17:08:45 +0000 | [diff] [blame] | 6 | \moduleauthor{Barry A. Warsaw}{barry@digicool.com} |
| 7 | \sectionauthor{Barry A. Warsaw}{barry@digicool.com} |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 8 | |
| 9 | |
| 10 | The \module{gettext} module provides internationalization (I18N) and |
| 11 | localization (L10N) services for your Python modules and applications. |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 12 | It supports both the GNU \code{gettext} message catalog API and a |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 13 | higher level, class-based API that may be more appropriate for Python |
| 14 | files. The interface described below allows you to write your |
| 15 | module and application messages in one natural language, and provide a |
| 16 | catalog of translated messages for running under different natural |
| 17 | languages. |
| 18 | |
| 19 | Some hints on localizing your Python modules and applications are also |
| 20 | given. |
| 21 | |
| 22 | \subsection{GNU \program{gettext} API} |
| 23 | |
| 24 | The \module{gettext} module defines the following API, which is very |
| 25 | similar to the GNU \program{gettext} API. If you use this API you |
| 26 | will affect the translation of your entire application globally. Often |
| 27 | this is what you want if your application is monolingual, with the choice |
| 28 | of language dependent on the locale of your user. If you are |
| 29 | localizing a Python module, or if your application needs to switch |
| 30 | languages on the fly, you probably want to use the class-based API |
| 31 | instead. |
| 32 | |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 33 | \begin{funcdesc}{bindtextdomain}{domain\optional{, localedir}} |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 34 | Bind the \var{domain} to the locale directory |
| 35 | \var{localedir}. More concretely, \module{gettext} will look for |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 36 | binary \file{.mo} files for the given domain using the path (on \UNIX): |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 37 | \file{\var{localedir}/\var{language}/LC_MESSAGES/\var{domain}.mo}, |
| 38 | where \var{languages} is searched for in the environment variables |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 39 | \envvar{LANGUAGE}, \envvar{LC_ALL}, \envvar{LC_MESSAGES}, and |
| 40 | \envvar{LANG} respectively. |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 41 | |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 42 | If \var{localedir} is omitted or \code{None}, then the current binding |
| 43 | for \var{domain} is returned.\footnote{ |
Fred Drake | 91f2f26 | 2001-07-06 19:28:48 +0000 | [diff] [blame] | 44 | 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 Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 50 | \function{bindtextdomain()} with an explicit absolute path at |
| 51 | the start of your application.} |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 52 | \end{funcdesc} |
| 53 | |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 54 | \begin{funcdesc}{textdomain}{\optional{domain}} |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 55 | Change or query the current global domain. If \var{domain} is |
| 56 | \code{None}, then the current global domain is returned, otherwise the |
| 57 | global domain is set to \var{domain}, which is returned. |
| 58 | \end{funcdesc} |
| 59 | |
| 60 | \begin{funcdesc}{gettext}{message} |
| 61 | Return the localized translation of \var{message}, based on the |
| 62 | current global domain, language, and locale directory. This function |
| 63 | is usually aliased as \function{_} in the local namespace (see |
| 64 | examples below). |
| 65 | \end{funcdesc} |
| 66 | |
| 67 | \begin{funcdesc}{dgettext}{domain, message} |
| 68 | Like \function{gettext()}, but look the message up in the specified |
| 69 | \var{domain}. |
| 70 | \end{funcdesc} |
| 71 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 72 | \begin{funcdesc}{ngettext}{singular, plural, n} |
| 73 | |
| 74 | Like \function{gettext()}, but consider plural forms. If a translation |
| 75 | is found, apply the plural formula to \var{n}, and return the |
| 76 | resulting message (some languages have more than two plural forms). |
| 77 | If no translation is found, return \var{singular} if \var{n} is 1; |
| 78 | return \var{plural} otherwise. |
| 79 | |
| 80 | The Plural formula is taken from the catalog header. It is a C or |
| 81 | Python expression that has a free variable n; the expression evaluates |
| 82 | to the index of the plural in the catalog. See the GNU gettext |
| 83 | documentation for the precise syntax to be used in .po files, and the |
| 84 | formulas for a variety of languages. |
| 85 | |
| 86 | \versionadded{2.3} |
| 87 | |
| 88 | \end{funcdesc} |
| 89 | |
| 90 | \begin{funcdesc}{dngettext}{domain, singular, plural, n} |
| 91 | Like \function{ngettext()}, but look the message up in the specified |
| 92 | \var{domain}. |
| 93 | |
| 94 | \versionadded{2.3} |
| 95 | \end{funcdesc} |
| 96 | |
| 97 | |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 98 | Note that GNU \program{gettext} also defines a \function{dcgettext()} |
| 99 | method, but this was deemed not useful and so it is currently |
| 100 | unimplemented. |
| 101 | |
| 102 | Here's an example of typical usage for this API: |
| 103 | |
| 104 | \begin{verbatim} |
| 105 | import gettext |
| 106 | gettext.bindtextdomain('myapplication', '/path/to/my/language/directory') |
| 107 | gettext.textdomain('myapplication') |
| 108 | _ = gettext.gettext |
| 109 | # ... |
| 110 | print _('This is a translatable string.') |
| 111 | \end{verbatim} |
| 112 | |
| 113 | \subsection{Class-based API} |
| 114 | |
| 115 | The class-based API of the \module{gettext} module gives you more |
| 116 | flexibility and greater convenience than the GNU \program{gettext} |
| 117 | API. It is the recommended way of localizing your Python applications and |
| 118 | modules. \module{gettext} defines a ``translations'' class which |
| 119 | implements the parsing of GNU \file{.mo} format files, and has methods |
| 120 | for returning either standard 8-bit strings or Unicode strings. |
| 121 | Translations instances can also install themselves in the built-in |
| 122 | namespace as the function \function{_()}. |
| 123 | |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 124 | \begin{funcdesc}{find}{domain\optional{, localedir\optional{, |
| 125 | languages\optional{, all}}}} |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 126 | This function implements the standard \file{.mo} file search |
| 127 | algorithm. It takes a \var{domain}, identical to what |
Barry Warsaw | 91b81c4 | 2001-10-18 19:41:48 +0000 | [diff] [blame] | 128 | \function{textdomain()} takes. Optional \var{localedir} is as in |
| 129 | \function{bindtextdomain()} Optional \var{languages} is a list of |
| 130 | strings, where each string is a language code. |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 131 | |
| 132 | If \var{localedir} is not given, then the default system locale |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 133 | directory is used.\footnote{See the footnote for |
| 134 | \function{bindtextdomain()} above.} If \var{languages} is not given, |
| 135 | then the following environment variables are searched: \envvar{LANGUAGE}, |
| 136 | \envvar{LC_ALL}, \envvar{LC_MESSAGES}, and \envvar{LANG}. The first one |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 137 | returning a non-empty value is used for the \var{languages} variable. |
Barry Warsaw | 91b81c4 | 2001-10-18 19:41:48 +0000 | [diff] [blame] | 138 | The environment variables should contain a colon separated list of |
| 139 | languages, which will be split on the colon to produce the expected |
| 140 | list of language code strings. |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 141 | |
| 142 | \function{find()} then expands and normalizes the languages, and then |
| 143 | iterates through them, searching for an existing file built of these |
| 144 | components: |
| 145 | |
| 146 | \file{\var{localedir}/\var{language}/LC_MESSAGES/\var{domain}.mo} |
| 147 | |
| 148 | The first such file name that exists is returned by \function{find()}. |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 149 | If no such file is found, then \code{None} is returned. If \var{all} |
| 150 | is given, it returns a list of all file names, in the order in which |
| 151 | they appear in the languages list or the environment variables. |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 152 | \end{funcdesc} |
| 153 | |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 154 | \begin{funcdesc}{translation}{domain\optional{, localedir\optional{, |
Martin v. Löwis | 1be6419 | 2002-01-11 06:33:28 +0000 | [diff] [blame] | 155 | languages\optional{, |
| 156 | class_,\optional{fallback}}}}} |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 157 | Return a \class{Translations} instance based on the \var{domain}, |
| 158 | \var{localedir}, and \var{languages}, which are first passed to |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 159 | \function{find()} to get a list of the |
| 160 | associated \file{.mo} file paths. Instances with |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 161 | identical \file{.mo} file names are cached. The actual class instantiated |
| 162 | is either \var{class_} if provided, otherwise |
| 163 | \class{GNUTranslations}. The class's constructor must take a single |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 164 | file object argument. |
| 165 | |
| 166 | If multiple files are found, later files are used as fallbacks for |
| 167 | earlier ones. To allow setting the fallback, \function{copy.copy} |
| 168 | is used to clone each translation object from the cache; the actual |
| 169 | instance data is still shared with the cache. |
| 170 | |
| 171 | If no \file{.mo} file is found, this function raises |
| 172 | \exception{IOError} if \var{fallback} is false (which is the default), |
| 173 | and returns a \class{NullTranslations} instance if \var{fallback} is |
| 174 | true. |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 175 | \end{funcdesc} |
| 176 | |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 177 | \begin{funcdesc}{install}{domain\optional{, localedir\optional{, unicode}}} |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 178 | This installs the function \function{_} in Python's builtin namespace, |
| 179 | based on \var{domain}, and \var{localedir} which are passed to the |
| 180 | function \function{translation()}. The \var{unicode} flag is passed to |
| 181 | the resulting translation object's \method{install} method. |
| 182 | |
| 183 | As seen below, you usually mark the strings in your application that are |
Fred Drake | 91f2f26 | 2001-07-06 19:28:48 +0000 | [diff] [blame] | 184 | candidates for translation, by wrapping them in a call to the |
| 185 | \function{_()} function, like this: |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 186 | |
| 187 | \begin{verbatim} |
| 188 | print _('This string will be translated.') |
| 189 | \end{verbatim} |
| 190 | |
| 191 | For convenience, you want the \function{_()} function to be installed in |
| 192 | Python's builtin namespace, so it is easily accessible in all modules |
| 193 | of your application. |
| 194 | \end{funcdesc} |
| 195 | |
| 196 | \subsubsection{The \class{NullTranslations} class} |
| 197 | Translation classes are what actually implement the translation of |
| 198 | original source file message strings to translated message strings. |
| 199 | The base class used by all translation classes is |
| 200 | \class{NullTranslations}; this provides the basic interface you can use |
| 201 | to write your own specialized translation classes. Here are the |
| 202 | methods of \class{NullTranslations}: |
| 203 | |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 204 | \begin{methoddesc}[NullTranslations]{__init__}{\optional{fp}} |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 205 | Takes an optional file object \var{fp}, which is ignored by the base |
| 206 | class. Initializes ``protected'' instance variables \var{_info} and |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 207 | \var{_charset} which are set by derived classes, as well as \var{_fallback}, |
| 208 | which is set through \method{add_fallback}. It then calls |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 209 | \code{self._parse(fp)} if \var{fp} is not \code{None}. |
| 210 | \end{methoddesc} |
| 211 | |
| 212 | \begin{methoddesc}[NullTranslations]{_parse}{fp} |
| 213 | No-op'd in the base class, this method takes file object \var{fp}, and |
| 214 | reads the data from the file, initializing its message catalog. If |
| 215 | you have an unsupported message catalog file format, you should |
| 216 | override this method to parse your format. |
| 217 | \end{methoddesc} |
| 218 | |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 219 | \begin{methoddesc}{NullTranslations}{add_fallback}{fallback} |
| 220 | Add \var{fallback} as the fallback object for the current translation |
| 221 | object. A translation object should consult the fallback if it cannot |
| 222 | provide a translation for a given message. |
| 223 | \end{methoddesc} |
| 224 | |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 225 | \begin{methoddesc}[NullTranslations]{gettext}{message} |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 226 | If a fallback has been set, forward \method{gettext} to the fallback. |
| 227 | Otherwise, return the translated message. Overridden in derived classes. |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 228 | \end{methoddesc} |
| 229 | |
| 230 | \begin{methoddesc}[NullTranslations]{ugettext}{message} |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 231 | If a fallback has been set, forward \method{ugettext} to the fallback. |
| 232 | Otherwise, return the translated message as a Unicode string. |
| 233 | Overridden in derived classes. |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 234 | \end{methoddesc} |
| 235 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 236 | \begin{methoddesc}[NullTranslations]{ngettext}{singular, plural, n} |
| 237 | If a fallback has been set, forward \method{ngettext} to the fallback. |
| 238 | Otherwise, return the translated message. Overridden in derived classes. |
| 239 | |
| 240 | \versionadded{2.3} |
| 241 | \end{methoddesc} |
| 242 | |
| 243 | \begin{methoddesc}[NullTranslations]{ungettext}{singular, plural, n} |
| 244 | If a fallback has been set, forward \method{ungettext} to the fallback. |
| 245 | Otherwise, return the translated message as a Unicode string. |
| 246 | Overridden in derived classes. |
| 247 | |
| 248 | \versionadded{2.3} |
| 249 | \end{methoddesc} |
| 250 | |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 251 | \begin{methoddesc}[NullTranslations]{info}{} |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 252 | Return the ``protected'' \member{_info} variable. |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 253 | \end{methoddesc} |
| 254 | |
| 255 | \begin{methoddesc}[NullTranslations]{charset}{} |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 256 | Return the ``protected'' \member{_charset} variable. |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 257 | \end{methoddesc} |
| 258 | |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 259 | \begin{methoddesc}[NullTranslations]{install}{\optional{unicode}} |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 260 | If the \var{unicode} flag is false, this method installs |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 261 | \method{self.gettext()} into the built-in namespace, binding it to |
| 262 | \samp{_}. If \var{unicode} is true, it binds \method{self.ugettext()} |
| 263 | instead. By default, \var{unicode} is false. |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 264 | |
| 265 | Note that this is only one way, albeit the most convenient way, to |
| 266 | make the \function{_} function available to your application. Because it |
| 267 | affects the entire application globally, and specifically the built-in |
| 268 | namespace, localized modules should never install \function{_}. |
| 269 | Instead, they should use this code to make \function{_} available to |
| 270 | their module: |
| 271 | |
| 272 | \begin{verbatim} |
| 273 | import gettext |
| 274 | t = gettext.translation('mymodule', ...) |
| 275 | _ = t.gettext |
| 276 | \end{verbatim} |
| 277 | |
| 278 | This puts \function{_} only in the module's global namespace and so |
| 279 | only affects calls within this module. |
| 280 | \end{methoddesc} |
| 281 | |
| 282 | \subsubsection{The \class{GNUTranslations} class} |
| 283 | |
| 284 | The \module{gettext} module provides one additional class derived from |
| 285 | \class{NullTranslations}: \class{GNUTranslations}. This class |
| 286 | overrides \method{_parse()} to enable reading GNU \program{gettext} |
| 287 | format \file{.mo} files in both big-endian and little-endian format. |
| 288 | |
| 289 | It also parses optional meta-data out of the translation catalog. It |
| 290 | is convention with GNU \program{gettext} to include meta-data as the |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 291 | translation for the empty string. This meta-data is in \rfc{822}-style |
| 292 | \code{key: value} pairs. If the key \code{Content-Type} is found, |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 293 | then the \code{charset} property is used to initialize the |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 294 | ``protected'' \member{_charset} instance variable. The entire set of |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 295 | key/value pairs are placed into a dictionary and set as the |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 296 | ``protected'' \member{_info} instance variable. |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 297 | |
| 298 | If the \file{.mo} file's magic number is invalid, or if other problems |
| 299 | occur while reading the file, instantiating a \class{GNUTranslations} class |
| 300 | can raise \exception{IOError}. |
| 301 | |
| 302 | The other usefully overridden method is \method{ugettext()}, which |
| 303 | returns a Unicode string by passing both the translated message string |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 304 | and the value of the ``protected'' \member{_charset} variable to the |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 305 | builtin \function{unicode()} function. |
| 306 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 307 | To facilitate plural forms, the methods \method{ngettext} and |
| 308 | \method{ungettext} are overridden as well. |
| 309 | |
Fred Drake | d0726c3 | 2000-09-07 18:55:08 +0000 | [diff] [blame] | 310 | \subsubsection{Solaris message catalog support} |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 311 | |
| 312 | The Solaris operating system defines its own binary |
| 313 | \file{.mo} file format, but since no documentation can be found on |
| 314 | this format, it is not supported at this time. |
| 315 | |
| 316 | \subsubsection{The Catalog constructor} |
| 317 | |
Fred Drake | d0726c3 | 2000-09-07 18:55:08 +0000 | [diff] [blame] | 318 | GNOME\index{GNOME} uses a version of the \module{gettext} module by |
| 319 | James Henstridge, but this version has a slightly different API. Its |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 320 | documented usage was: |
| 321 | |
| 322 | \begin{verbatim} |
| 323 | import gettext |
| 324 | cat = gettext.Catalog(domain, localedir) |
| 325 | _ = cat.gettext |
| 326 | print _('hello world') |
| 327 | \end{verbatim} |
| 328 | |
| 329 | For compatibility with this older module, the function |
| 330 | \function{Catalog()} is an alias for the the \function{translation()} |
| 331 | function described above. |
| 332 | |
| 333 | One difference between this module and Henstridge's: his catalog |
| 334 | objects supported access through a mapping API, but this appears to be |
| 335 | unused and so is not currently supported. |
| 336 | |
| 337 | \subsection{Internationalizing your programs and modules} |
| 338 | Internationalization (I18N) refers to the operation by which a program |
| 339 | is made aware of multiple languages. Localization (L10N) refers to |
| 340 | the adaptation of your program, once internationalized, to the local |
| 341 | language and cultural habits. In order to provide multilingual |
| 342 | messages for your Python programs, you need to take the following |
| 343 | steps: |
| 344 | |
| 345 | \begin{enumerate} |
| 346 | \item prepare your program or module by specially marking |
| 347 | translatable strings |
| 348 | \item run a suite of tools over your marked files to generate raw |
| 349 | messages catalogs |
| 350 | \item create language specific translations of the message catalogs |
| 351 | \item use the \module{gettext} module so that message strings are |
| 352 | properly translated |
| 353 | \end{enumerate} |
| 354 | |
| 355 | In order to prepare your code for I18N, you need to look at all the |
| 356 | strings in your files. Any string that needs to be translated |
Fred Drake | 91f2f26 | 2001-07-06 19:28:48 +0000 | [diff] [blame] | 357 | should be marked by wrapping it in \code{_('...')} --- that is, a call |
| 358 | to the function \function{_()}. For example: |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 359 | |
| 360 | \begin{verbatim} |
| 361 | filename = 'mylog.txt' |
| 362 | message = _('writing a log message') |
| 363 | fp = open(filename, 'w') |
| 364 | fp.write(message) |
| 365 | fp.close() |
| 366 | \end{verbatim} |
| 367 | |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 368 | In this example, the string \code{'writing a log message'} is marked as |
| 369 | a candidate for translation, while the strings \code{'mylog.txt'} and |
| 370 | \code{'w'} are not. |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 371 | |
Barry Warsaw | b416290 | 2001-01-31 21:21:45 +0000 | [diff] [blame] | 372 | The Python distribution comes with two tools which help you generate |
| 373 | the message catalogs once you've prepared your source code. These may |
| 374 | or may not be available from a binary distribution, but they can be |
| 375 | found in a source distribution, in the \file{Tools/i18n} directory. |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 376 | |
Barry Warsaw | b416290 | 2001-01-31 21:21:45 +0000 | [diff] [blame] | 377 | The \program{pygettext}\footnote{Fran\c cois Pinard has |
| 378 | written a program called |
Barry Warsaw | ddef888 | 2000-09-13 12:04:47 +0000 | [diff] [blame] | 379 | \program{xpot} which does a similar job. It is available as part of |
| 380 | his \program{po-utils} package at |
Barry Warsaw | b416290 | 2001-01-31 21:21:45 +0000 | [diff] [blame] | 381 | \url{http://www.iro.umontreal.ca/contrib/po-utils/HTML}.} program |
| 382 | scans all your Python source code looking for the strings you |
| 383 | previously marked as translatable. It is similar to the GNU |
| 384 | \program{gettext} program except that it understands all the |
| 385 | intricacies of Python source code, but knows nothing about C or C++ |
| 386 | source code. You don't need GNU \code{gettext} unless you're also |
Fred Drake | 91f2f26 | 2001-07-06 19:28:48 +0000 | [diff] [blame] | 387 | going to be translating C code (such as C extension modules). |
Barry Warsaw | b416290 | 2001-01-31 21:21:45 +0000 | [diff] [blame] | 388 | |
| 389 | \program{pygettext} generates textual Uniforum-style human readable |
| 390 | message catalog \file{.pot} files, essentially structured human |
| 391 | readable files which contain every marked string in the source code, |
| 392 | along with a placeholder for the translation strings. |
| 393 | \program{pygettext} is a command line script that supports a similar |
| 394 | command line interface as \program{xgettext}; for details on its use, |
| 395 | run: |
| 396 | |
| 397 | \begin{verbatim} |
| 398 | pygettext.py --help |
| 399 | \end{verbatim} |
| 400 | |
| 401 | Copies of these \file{.pot} files are then handed over to the |
| 402 | individual human translators who write language-specific versions for |
| 403 | every supported natural language. They send you back the filled in |
| 404 | language-specific versions as a \file{.po} file. Using the |
| 405 | \program{msgfmt.py}\footnote{\program{msgfmt.py} is binary |
| 406 | compatible with GNU \program{msgfmt} except that it provides a |
| 407 | simpler, all-Python implementation. With this and |
| 408 | \program{pygettext.py}, you generally won't need to install the GNU |
| 409 | \program{gettext} package to internationalize your Python |
| 410 | applications.} program (in the \file{Tools/i18n} directory), you take the |
| 411 | \file{.po} files from your translators and generate the |
| 412 | machine-readable \file{.mo} binary catalog files. The \file{.mo} |
| 413 | files are what the \module{gettext} module uses for the actual |
| 414 | translation processing during run-time. |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 415 | |
| 416 | How you use the \module{gettext} module in your code depends on |
| 417 | whether you are internationalizing your entire application or a single |
| 418 | module. |
| 419 | |
| 420 | \subsubsection{Localizing your module} |
| 421 | |
| 422 | If you are localizing your module, you must take care not to make |
| 423 | global changes, e.g. to the built-in namespace. You should not use |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 424 | the GNU \code{gettext} API but instead the class-based API. |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 425 | |
| 426 | Let's say your module is called ``spam'' and the module's various |
| 427 | natural language translation \file{.mo} files reside in |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 428 | \file{/usr/share/locale} in GNU \program{gettext} format. Here's what |
| 429 | you would put at the top of your module: |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 430 | |
| 431 | \begin{verbatim} |
| 432 | import gettext |
| 433 | t = gettext.translation('spam', '/usr/share/locale') |
| 434 | _ = t.gettext |
| 435 | \end{verbatim} |
| 436 | |
| 437 | If your translators were providing you with Unicode strings in their |
| 438 | \file{.po} files, you'd instead do: |
| 439 | |
| 440 | \begin{verbatim} |
| 441 | import gettext |
| 442 | t = gettext.translation('spam', '/usr/share/locale') |
| 443 | _ = t.ugettext |
| 444 | \end{verbatim} |
| 445 | |
| 446 | \subsubsection{Localizing your application} |
| 447 | |
| 448 | If you are localizing your application, you can install the \function{_()} |
| 449 | function globally into the built-in namespace, usually in the main driver file |
| 450 | of your application. This will let all your application-specific |
| 451 | files just use \code{_('...')} without having to explicitly install it in |
| 452 | each file. |
| 453 | |
| 454 | In the simple case then, you need only add the following bit of code |
| 455 | to the main driver file of your application: |
| 456 | |
| 457 | \begin{verbatim} |
| 458 | import gettext |
| 459 | gettext.install('myapplication') |
| 460 | \end{verbatim} |
| 461 | |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 462 | If you need to set the locale directory or the \var{unicode} flag, |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 463 | you can pass these into the \function{install()} function: |
| 464 | |
| 465 | \begin{verbatim} |
| 466 | import gettext |
| 467 | gettext.install('myapplication', '/usr/share/locale', unicode=1) |
| 468 | \end{verbatim} |
| 469 | |
| 470 | \subsubsection{Changing languages on the fly} |
| 471 | |
| 472 | If your program needs to support many languages at the same time, you |
| 473 | may want to create multiple translation instances and then switch |
| 474 | between them explicitly, like so: |
| 475 | |
| 476 | \begin{verbatim} |
| 477 | import gettext |
| 478 | |
| 479 | lang1 = gettext.translation(languages=['en']) |
| 480 | lang2 = gettext.translation(languages=['fr']) |
| 481 | lang3 = gettext.translation(languages=['de']) |
| 482 | |
| 483 | # start by using language1 |
| 484 | lang1.install() |
| 485 | |
| 486 | # ... time goes by, user selects language 2 |
| 487 | lang2.install() |
| 488 | |
| 489 | # ... more time goes by, user selects language 3 |
| 490 | lang3.install() |
| 491 | \end{verbatim} |
| 492 | |
| 493 | \subsubsection{Deferred translations} |
| 494 | |
Neal Norwitz | 563d12d | 2002-06-24 02:22:39 +0000 | [diff] [blame] | 495 | In most coding situations, strings are translated where they are coded. |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 496 | Occasionally however, you need to mark strings for translation, but |
| 497 | defer actual translation until later. A classic example is: |
| 498 | |
| 499 | \begin{verbatim} |
| 500 | animals = ['mollusk', |
| 501 | 'albatross', |
| 502 | 'rat', |
| 503 | 'penguin', |
| 504 | 'python', |
| 505 | ] |
| 506 | # ... |
| 507 | for a in animals: |
| 508 | print a |
| 509 | \end{verbatim} |
| 510 | |
| 511 | Here, you want to mark the strings in the \code{animals} list as being |
| 512 | translatable, but you don't actually want to translate them until they |
| 513 | are printed. |
| 514 | |
| 515 | Here is one way you can handle this situation: |
| 516 | |
| 517 | \begin{verbatim} |
| 518 | def _(message): return message |
| 519 | |
| 520 | animals = [_('mollusk'), |
| 521 | _('albatross'), |
| 522 | _('rat'), |
| 523 | _('penguin'), |
| 524 | _('python'), |
| 525 | ] |
| 526 | |
| 527 | del _ |
| 528 | |
| 529 | # ... |
| 530 | for a in animals: |
| 531 | print _(a) |
| 532 | \end{verbatim} |
| 533 | |
| 534 | This works because the dummy definition of \function{_()} simply returns |
| 535 | the string unchanged. And this dummy definition will temporarily |
| 536 | override any definition of \function{_()} in the built-in namespace |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 537 | (until the \keyword{del} command). |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 538 | Take care, though if you have a previous definition of \function{_} in |
| 539 | the local namespace. |
| 540 | |
| 541 | Note that the second use of \function{_()} will not identify ``a'' as |
| 542 | being translatable to the \program{pygettext} program, since it is not |
| 543 | a string. |
| 544 | |
| 545 | Another way to handle this is with the following example: |
| 546 | |
| 547 | \begin{verbatim} |
| 548 | def N_(message): return message |
| 549 | |
| 550 | animals = [N_('mollusk'), |
| 551 | N_('albatross'), |
| 552 | N_('rat'), |
| 553 | N_('penguin'), |
| 554 | N_('python'), |
| 555 | ] |
| 556 | |
| 557 | # ... |
| 558 | for a in animals: |
| 559 | print _(a) |
| 560 | \end{verbatim} |
| 561 | |
| 562 | In this case, you are marking translatable strings with the function |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 563 | \function{N_()},\footnote{The choice of \function{N_()} here is totally |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 564 | arbitrary; it could have just as easily been |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 565 | \function{MarkThisStringForTranslation()}. |
| 566 | } which won't conflict with any definition of |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 567 | \function{_()}. However, you will need to teach your message extraction |
| 568 | program to look for translatable strings marked with \function{N_()}. |
| 569 | \program{pygettext} and \program{xpot} both support this through the |
| 570 | use of command line switches. |
| 571 | |
| 572 | \subsection{Acknowledgements} |
| 573 | |
| 574 | The following people contributed code, feedback, design suggestions, |
| 575 | previous implementations, and valuable experience to the creation of |
| 576 | this module: |
| 577 | |
| 578 | \begin{itemize} |
| 579 | \item Peter Funk |
| 580 | \item James Henstridge |
Fred Drake | 74f5a56 | 2002-11-22 14:28:53 +0000 | [diff] [blame] | 581 | \item Juan David Ib\'a\~nez Palomar |
Fred Drake | d576e9d | 2000-08-30 04:19:20 +0000 | [diff] [blame] | 582 | \item Marc-Andr\'e Lemburg |
Barry Warsaw | 0691a6b | 2000-08-30 03:27:10 +0000 | [diff] [blame] | 583 | \item Martin von L\"owis |
| 584 | \item Fran\c cois Pinard |
| 585 | \item Barry Warsaw |
| 586 | \end{itemize} |