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.

class TranslationStringFactory[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.

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 passed may be translation strings or unicode strings. 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. Note that if the objects passed are translation strings, their domains and mappings are ignored. The domain and mapping arguments must be used instead. If the domain is not supplied, a default domain is used (usually messages).


num = 1
translated = localizer.pluralize('Add ${num} item',
                                 'Add ${num} items',
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.


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


translated = localizer.translate('Add ${item}', domain='mypackage',

Retrieve a pyramid.i18n.Localizer object corresponding to the current request’s locale name.


Negotiate and return the locale name associated with the current request (never cached).


Return the locale name associated with the current request (possibly cached).


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).
  • 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.)

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