pyramid.i18n¶
- class TranslationString(msgid, domain=None, default=None, mapping=None, context=None)[ソース]
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.
- TranslationStringFactory(factory_domain)[ソース]¶
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)[ソース]¶
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)[ソース]¶
Return a 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 should be strings. There is no reason to use translation string objects as arguments as all metadata is ignored.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. 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})
If using the gettext plural support, which is required for languages that have pluralisation rules other than n != 1, the
singularargument 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)[ソース]¶
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 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.i18n 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)[ソース]¶
バージョン 1.5 で非推奨: Use the
pyramid.request.Request.localizerattribute directly instead. Retrieve apyramid.i18n.Localizerobject corresponding to the current request's locale name.
- negotiate_locale_name(request)[ソース]¶
Negotiate and return the locale name associated with the current request.
- get_locale_name(request)[ソース]¶
バージョン 1.5 で非推奨: Use
pyramid.request.Request.locale_namedirectly instead. Return the locale name associated with the current request.
- default_locale_negotiator(request)[ソース]¶
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 notNone, 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
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.)
- make_localizer(current_locale_name, translation_directories)[ソース]¶
Create a
pyramid.i18n.Localizerobject 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.