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
.msgid
must be the message identifier for the translation string. It must be aunicode
object or astr
object encoded in the default system encoding.Optional keyword arguments to this object's constructor include
domain
,default
, andmapping
.domain
represents the translation domain. By default, the translation domain isNone
, indicating that this translation string is associated with the default translation domain (usuallymessages
).default
represents an explicit default text for this translation string. Default text appears when the translation string cannot be translated. Usually, themsgid
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. Ifdefault
is provided, it must be aunicode
object or astr
object encoded in the default system encoding (usually means ASCII). Ifdefault
isNone
(its default value), themsgid
value 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.context
represents the translation context. By default, the translation context isNone
.After a translation string is constructed, it behaves like most other
unicode
objects; itsmsgid
value will be displayed when it is treated like aunicode
object. Only when itsugettext
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 thedomain
attribute, and themapping
is available as themapping
attribute. 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
domain
value. A single argument is passed to this class' constructor:domain
. This value will be used as thedomain
values oftranslationstring.TranslationString
objects generated by the__call__
of this class. Themsgid
,mapping
, anddefault
values 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.Localizer
object is created using thepyramid.i18n.get_localizer()
function.- locale_name¶
The locale name for this localizer (e.g.
en
oren_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
n
value representing the number that appears in the message using gettext plural forms support. Thesingular
andplural
objects should be 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, andmapping
is the interpolation mapping that should be used on the result. If thedomain
is 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
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)[ソース]¶
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) andmapping
(optional). When called, it will translate thetstring
translation string using the current locale. If the current locale could not be determined, the result of interpolation of the default value is returned. The optionaldomain
argument can be used to specify or override the domain of thetstring
(useful whentstring
is a normal string rather than a translation string). The optionalmapping
argument can specify or override thetstring
interpolation mapping, useful when thetstring
argument 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.localizer
attribute directly instead. Retrieve apyramid.i18n.Localizer
object 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_name
directly 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
None
if 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.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.