Fork me on GitHub

pylons.i18n.translation – Translation/Localization functions

Translation/Localization functions.

Provides gettext translation functions via an app’s pylons.translator and get/set_lang for changing the language translated to.

Module Contents

exception pylons.i18n.translation.LanguageError

Exception raised when a problem occurs with changing languages

class pylons.i18n.translation.LazyString(func, *args, **kwargs)

Has a number of lazily evaluated functions replicating a string. Just override the eval() method to produce the actual value.

This method copied from TurboGears.

pylons.i18n.translation.lazify(func)

Decorator to return a lazy-evaluated version of the original

pylons.i18n.translation.gettext_noop(value)

Mark a string for translation without translating it. Returns value.

Used for global strings, e.g.:

foo = N_('Hello')

class Bar:
    def __init__(self):
        self.local_foo = _(foo)

h.set_lang('fr')
assert Bar().local_foo == 'Bonjour'
h.set_lang('es')
assert Bar().local_foo == 'Hola'
assert foo == 'Hello'
pylons.i18n.translation.gettext(value)

Mark a string for translation. Returns the localized string of value.

Mark a string to be localized as follows:

gettext('This should be in lots of languages')
pylons.i18n.translation.ugettext(value)

Mark a string for translation. Returns the localized unicode string of value.

Mark a string to be localized as follows:

_('This should be in lots of languages')
pylons.i18n.translation.ngettext(singular, plural, n)

Mark a string for translation. Returns the localized string of the pluralized value.

This does a plural-forms lookup of a message id. singular is used as the message id for purposes of lookup in the catalog, while n is used to determine which plural form to use. The returned message is a string.

Mark a string to be localized as follows:

ngettext('There is %(num)d file here', 'There are %(num)d files here',
         n) % {'num': n}
pylons.i18n.translation.ungettext(singular, plural, n)

Mark a string for translation. Returns the localized unicode string of the pluralized value.

This does a plural-forms lookup of a message id. singular is used as the message id for purposes of lookup in the catalog, while n is used to determine which plural form to use. The returned message is a Unicode string.

Mark a string to be localized as follows:

ungettext('There is %(num)d file here', 'There are %(num)d files here',
          n) % {'num': n}
pylons.i18n.translation.set_lang(lang, set_environ=True, **kwargs)

Set the current language used for translations.

lang should be a string or a list of strings. If a list of strings, the first language is set as the main and the subsequent languages are added as fallbacks.

pylons.i18n.translation.get_lang()

Return the current i18n language used

pylons.i18n.translation.add_fallback(lang, **kwargs)

Add a fallback language from which words not matched in other languages will be translated to.

This fallback will be associated with the currently selected language – that is, resetting the language via set_lang() resets the current fallbacks.

This function can be called multiple times to add multiple fallbacks.