Python
------

RPMs
     python

Ubuntu packages
     python

File extension
     py

String syntax
     'abc', u'abc', r'abc', ur'abc',

     "abc", u"abc", r"abc", ur"abc",

     '''abc''', u'''abc''', r'''abc''', ur'''abc''',

     """abc""", u"""abc""", r"""abc""", ur"""abc"""

gettext shorthand
     _('abc') etc.

gettext/ngettext functions
     gettext.gettext, gettext.dgettext, gettext.ngettext, gettext.dngettext, also ugettext, ungettext

textdomain
     gettext.textdomain function, or gettext.install(DOMAIN) function

bindtextdomain
     gettext.bindtextdomain function, or gettext.install(DOMAIN,LOCALEDIR) function

setlocale
     not used by the gettext emulation

Prerequisite
     import gettext

Use or emulate GNU gettext
     emulate

Extractor
     xgettext

Formatting with positions
     '...%(ident)d...' % { 'ident': value }

     '...{ident}...'.format(ident=value) (see PEP 3101)

Portability
     fully portable

po-mode marking
     ---

An example is available in the examples directory: hello-python.

A note about format strings: Python supports format strings with unnamed arguments, such as '...%d...', and format strings with named arguments, such as '...%(ident)d...'.  The latter are preferable for internationalized programs, for two reasons:

  * When a format string takes more than one argument, the translator can provide a translation that uses the arguments in a different order, if the format string uses named arguments.  For example, the translator can reformulate

          "'%(volume)s' has only %(freespace)d bytes free."

     to

          "Only %(freespace)d bytes free on '%(volume)s'."

     Additionally, the identifiers also provide some context to the translator.

  * In the context of plural forms, the format string used for the singular form does not use the numeric argument in many languages.  Even in English, one prefers to write "one hour" instead of "1 hour".  Omitting individual arguments from format strings like this is only possible with the named argument syntax.  (With unnamed arguments, Python -- unlike C -- verifies that the format string uses all supplied arguments.)

A note about f-strings (PEP 498): xgettext

  * syntactically recognizes f-strings,

  * is able to extract f-strings that contain no sub-expressions.

However, xgettext does not extract f-strings marked for translation that contain sub-expressions.  This will not work as expected:

     _(f"The file {file[i]} does not exist.")

because the translator is generally not a programmer and should thus not be confronted with expressions from the programming language.

Related software
----------------

An internationalization system based on GNU gettext and PO files is Babel (https://babel.pocoo.org/).