# pyramid.i18n¶

class TranslationString[source]

The constructor for a translation string. A translation string is a Unicode-like object that has some extra metadata.

This constructor accepts one required argument named msgid. msgid must be the message identifier for the translation string. It must be a unicode object or a str object encoded in the default system encoding.

Optional keyword arguments to this object's constructor include domain, default, and mapping.

domain represents the translation domain. By default, the translation domain is None, indicating that this translation string is associated with the default translation domain (usually messages).

default represents an explicit default text for this translation string. Default text appears when the translation string cannot be translated. Usually, the msgid of a translation string serves double duty as its default text. However, using this option you can provide a different default text for this translation string. This feature is useful when the default of a translation string is too complicated or too long to be used as a message identifier. If default is provided, it must be a unicode object or a str object encoded in the default system encoding (usually means ASCII). If default is None (its default value), the msgid value used by this translation string will be assumed to be the value of default.

mapping, if supplied, must be a dictionary-like object which represents the replacement values for any translation string replacement marker instances found within the msgid (or default) value of this translation string.

context represents the translation context. By default, the translation context is None.

After a translation string is constructed, it behaves like most other unicode objects; its msgid value will be displayed when it is treated like a unicode object. Only when its ugettext method is called will it be translated.

Its default value is available as the default attribute of the object, its translation domain is available as the domain attribute, and the mapping is available as the mapping attribute. The object otherwise behaves much like a Unicode string.

TranslationStringFactory(factory_domain)[source]

Create a factory which will generate translation strings without requiring that each call to the factory be passed a domain value. A single argument is passed to this class' constructor: domain. This value will be used as the domain values of translationstring.TranslationString objects generated by the __call__ of this class. The msgid, mapping, and default values provided to the __call__ method of an instance of this class have the meaning as described by the constructor of the translationstring.TranslationString

class Localizer(locale_name, translations)[source]

An object providing translation and pluralizations related to the current request's locale name. A pyramid.i18n.Localizer object is created using the pyramid.i18n.get_localizer() function.

locale_name

The locale name for this localizer (e.g. en or en_US).

pluralize(singular, plural, n, domain=None, mapping=None)[source]

Return a Unicode string translation by using two message identifier objects as a singular/plural pair and an n value representing the number that appears in the message using gettext plural forms support. The singular and plural objects should be unicode strings. There is no reason to use translation string objects as arguments as all metadata is ignored.

n represents the number of elements. domain is the translation domain to use to do the pluralization, and mapping is the interpolation mapping that should be used on the result. If the domain is not supplied, a default domain is used (usually messages).

Example:

num = 1
translated = localizer.pluralize('Add ${num} item', 'Add${num} items',
num,
mapping={'num':num})


If using the gettext plural support, which is required for languages that have pluralisation rules other than n != 1, the singular argument must be the message_id defined in the translation file. The plural argument is not used in this case.

Example:

num = 1
translated = localizer.pluralize('item_plural',
'',
num,
mapping={'num':num})

translate(tstring, domain=None, mapping=None)[source]

Translate a translation string to the current language and interpolate any replacement markers in the result. The translate method accepts three arguments: tstring (required), domain (optional) and mapping (optional). When called, it will translate the tstring translation string to a unicode object using the current locale. If the current locale could not be determined, the result of interpolation of the default value is returned. The optional domain argument can be used to specify or override the domain of the tstring (useful when tstring is a normal string rather than a translation string). The optional mapping argument can specify or override the tstring interpolation mapping, useful when the tstring argument is a simple string instead of a translation string.

Example:

from pyramid.18n import TranslationString
ts = TranslationString('Add ${item}', domain='mypackage', mapping={'item':'Item'}) translated = localizer.translate(ts)  Example: translated = localizer.translate('Add${item}', domain='mypackage',
mapping={'item':'Item'})

get_localizer(request)[source]

Deprecated since version 1.5: Use the pyramid.request.Request.localizer attribute directly instead. Retrieve a pyramid.i18n.Localizer object corresponding to the current request's locale name.

negotiate_locale_name(request)[source]

Negotiate and return the locale name associated with the current request.

get_locale_name(request)[source]

Deprecated since version 1.5: Use pyramid.request.Request.locale_name directly instead. Return the locale name associated with the current request.

default_locale_negotiator(request)[source]

The default locale negotiator. Returns a locale name or None.

• First, the negotiator looks for the _LOCALE_ attribute of the request object (possibly set by a view or a listener for an event). If the attribute exists and it is not None, its value will be used.
• Then it looks for the request.params['_LOCALE_'] value.
• Then it looks for the request.cookies['_LOCALE_'] value.
• Finally, the negotiator returns None if the locale could not be determined via any of the previous checks (when a locale negotiator returns None, it signifies that the default locale name should be used.)
make_localizer(current_locale_name, translation_directories)[source]

Create a pyramid.i18n.Localizer object corresponding to the provided locale name from the translations found in the list of translation directories.

See Internationalization and Localization for more information about using Pyramid internationalization and localization services within an application.