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.msgidmust be the message identifier for the translation string. It must be aunicodeobject or astrobject encoded in the default system encoding.Optional keyword arguments to this object’s constructor include
domain,default, andmapping.domainrepresents the translation domain. By default, the translation domain isNone, indicating that this translation string is associated with the default translation domain (usuallymessages).defaultrepresents an explicit default text for this translation string. Default text appears when the translation string cannot be translated. Usually, themsgidof 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. Ifdefaultis provided, it must be aunicodeobject or astrobject encoded in the default system encoding (usually means ASCII). IfdefaultisNone(its default value), themsgidvalue used by this translation string will be assumed to be the value ofdefault.mapping, if supplied, must be a dictionary-like object which represents the replacement values for any translation string replacement marker instances found within themsgid(ordefault) value of this translation string.contextrepresents the translation context. By default, the translation context isNone.After a translation string is constructed, it behaves like most other
unicodeobjects; itsmsgidvalue will be displayed when it is treated like aunicodeobject. Only when itsugettextmethod is called will it be translated.Its default value is available as the
defaultattribute of the object, its translation domain is available as thedomainattribute, and themappingis available as themappingattribute. 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
domainvalue. A single argument is passed to this class’ constructor:domain. This value will be used as thedomainvalues oftranslationstring.TranslationStringobjects generated by the__call__of this class. Themsgid,mapping, anddefaultvalues provided to the__call__method of an instance of this class have the meaning as described by the constructor of thetranslationstring.TranslationString
-
class
Localizer(locale_name, translations)[source]¶ - An object providing translation and pluralizations related to
- the current request’s locale name. A
pyramid.i18n.Localizerobject is created using thepyramid.i18n.get_localizer()function.
-
locale_name¶ The locale name for this localizer (e.g.
enoren_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
nvalue representing the number that appears in the message using gettext plural forms support. Thesingularandpluralobjects passed may be translation strings or unicode strings.nrepresents the number of elements.domainis the translation domain to use to do the pluralization, andmappingis 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 thedomainis not supplied, a default domain is used (usuallymessages).Example:
num = 1 translated = localizer.pluralize('Add ${num} item', 'Add ${num} items', 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
translatemethod accepts three arguments:tstring(required),domain(optional) andmapping(optional). When called, it will translate thetstringtranslation string to aunicodeobject using the current locale. If the current locale could not be determined, the result of interpolation of the default value is returned. The optionaldomainargument can be used to specify or override the domain of thetstring(useful whentstringis a normal string rather than a translation string). The optionalmappingargument can specify or override thetstringinterpolation mapping, useful when thetstringargument 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]¶ Retrieve a
pyramid.i18n.Localizerobject corresponding to the current request’s locale name.
-
negotiate_locale_name(request)[source]¶ Negotiate and return the locale name associated with the current request (never cached).
-
get_locale_name(request)[source]¶ Return the locale name associated with the current request (possibly cached).
-
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). - Then it looks for the
request.params['_LOCALE_']value. - Then it looks for the
request.cookies['_LOCALE_']value. - Finally, the negotiator returns
Noneif the locale could not be determined via any of the previous checks (when a locale negotiator returnsNone, it signifies that the default locale name should be used.)
- First, the negotiator looks for the
See Internationalization and Localization for more information about using Pyramid internationalization and localization services within an application.