本文整理汇总了Python中gettext.translation方法的典型用法代码示例。如果您正苦于以下问题:Python gettext.translation方法的具体用法?Python gettext.translation怎么用?Python gettext.translation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gettext
的用法示例。
在下文中一共展示了gettext.translation方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __new__
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import translation [as 别名]
def __new__(cls, msgid, msgtext=None, params=None,
domain='oslo', has_contextual_form=False,
has_plural_form=False, *args):
"""Create a new Message object.
In order for translation to work gettext requires a message ID, this
msgid will be used as the base unicode text. It is also possible
for the msgid and the base unicode text to be different by passing
the msgtext parameter.
"""
# If the base msgtext is not given, we use the default translation
# of the msgid (which is in English) just in case the system locale is
# not English, so that the base text will be in that locale by default.
if not msgtext:
msgtext = Message._translate_msgid(msgid, domain)
# We want to initialize the parent unicode with the actual object that
# would have been plain unicode if 'Message' was not enabled.
msg = super(Message, cls).__new__(cls, msgtext)
msg.msgid = msgid
msg.domain = domain
msg.params = params
msg.has_contextual_form = has_contextual_form
msg.has_plural_form = has_plural_form
return msg
示例2: _sanitize_mod_params
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import translation [as 别名]
def _sanitize_mod_params(self, other):
"""Sanitize the object being modded with this Message.
- Add support for modding 'None' so translation supports it
- Trim the modded object, which can be a large dictionary, to only
those keys that would actually be used in a translation
- Snapshot the object being modded, in case the message is
translated, it will be used as it was when the Message was created
"""
if other is None:
params = (other,)
elif isinstance(other, dict):
# Merge the dictionaries
# Copy each item in case one does not support deep copy.
params = {}
if isinstance(self.params, dict):
params.update((key, self._copy_param(val))
for key, val in self.params.items())
params.update((key, self._copy_param(val))
for key, val in other.items())
else:
params = self._copy_param(other)
return params
示例3: translator
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import translation [as 别名]
def translator(locales_map):
"""Build mock translator for the given locales.
Returns a mock gettext.translation function that uses
individual TestTranslations to translate in the given locales.
:param locales_map: A map from locale name to a translations map.
{
'es': {'Hi': 'Hola', 'Bye': 'Adios'},
'zh': {'Hi': 'Ni Hao', 'Bye': 'Zaijian'}
}
"""
def _translation(domain, localedir=None,
languages=None, fallback=None):
if languages:
language = languages[0]
if language in locales_map:
return FakeTranslations(locales_map[language])
return gettext.NullTranslations()
return _translation
示例4: get_translations
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import translation [as 别名]
def get_translations(languages=None, getter=get_builtin_gnu_translations):
"""
Get a WTForms translation object which wraps a low-level translations object.
:param languages:
A sequence of languages to try, in order.
:param getter:
A single-argument callable which returns a low-level translations object.
"""
translations = getter(languages)
if hasattr(translations, 'ugettext'):
return DefaultTranslations(translations)
else:
# Python 3 has no ugettext/ungettext, so just return the translations object.
return translations
示例5: resource_exists
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import translation [as 别名]
def resource_exists(name):
"""Return true if the given resource exists"""
try:
open_resource(name).close()
return True
except IOError:
return False
# Enable this when we get some translations?
# We want an i18n API that is useful to programs using Python's gettext
# module, as well as the Zope3 i18n package. Perhaps we should just provide
# the POT file and translations, and leave it up to callers to make use
# of them.
#
# t = gettext.translation(
# 'pytz', os.path.join(os.path.dirname(__file__), 'locales'),
# fallback=True
# )
# def _(timezone_name):
# """Translate a timezone name using the current locale, returning Unicode"""
# return t.ugettext(timezone_name)
示例6: _new_gnu_trans
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import translation [as 别名]
def _new_gnu_trans(self, localedir, use_null_fallback=True):
"""
Returns a mergeable gettext.GNUTranslations instance.
A convenience wrapper. By default gettext uses 'fallback=False'.
Using param `use_null_fallback` to avoid confusion with any other
references to 'fallback'.
"""
translation = gettext_module.translation(
domain='django',
localedir=localedir,
languages=[self.__locale],
codeset='utf-8',
fallback=use_null_fallback)
if not hasattr(translation, '_catalog'):
# provides merge support for NullTranslations()
translation._catalog = {}
translation._info = {}
translation.plural = lambda n: int(n != 1)
return translation
示例7: test_the_alternative_interface
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import translation [as 别名]
def test_the_alternative_interface(self):
eq = self.assertEqual
# test the alternative interface
with open(self.mofile, 'rb') as fp:
t = gettext.GNUTranslations(fp)
# Install the translation object
t.install()
eq(_('nudge nudge'), 'wink wink')
# Try unicode return type
t.install(unicode=True)
eq(_('mullusk'), 'bacon')
# Test installation of other methods
import __builtin__
t.install(unicode=True, names=["gettext", "lgettext"])
eq(_, t.ugettext)
eq(__builtin__.gettext, t.ugettext)
eq(lgettext, t.lgettext)
del __builtin__.gettext
del __builtin__.lgettext
示例8: test_cache
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import translation [as 别名]
def test_cache(self):
self.localedir = os.curdir
self.mofile = MOFILE
self.assertEqual(len(gettext._translations), 0)
t = gettext.translation('gettext', self.localedir)
self.assertEqual(len(gettext._translations), 1)
t = gettext.translation('gettext', self.localedir,
class_=DummyGNUTranslations)
self.assertEqual(len(gettext._translations), 2)
self.assertEqual(t.__class__, DummyGNUTranslations)
# Calling it again doesn't add to the cache
t = gettext.translation('gettext', self.localedir,
class_=DummyGNUTranslations)
self.assertEqual(len(gettext._translations), 2)
self.assertEqual(t.__class__, DummyGNUTranslations)
示例9: resource_exists
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import translation [as 别名]
def resource_exists(name):
"""Return true if the given resource exists"""
try:
open_resource(name).close()
return True
except IOError:
return False
# Enable this when we get some translations?
# We want an i18n API that is useful to programs using Python's gettext
# module, as well as the Zope3 i18n package. Perhaps we should just provide
# the POT file and translations, and leave it up to callers to make use
# of them.
#
# t = gettext.translation(
# 'pytz', os.path.join(os.path.dirname(__file__), 'locales'),
# fallback=True
# )
# def _(timezone_name):
# """Translate a timezone name using the current locale, returning Unicode"""
# return t.ugettext(timezone_name)
示例10: findtranslation
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import translation [as 别名]
def findtranslation(self):
"Find the translation for the document language."
self.langcodes = None
if not DocumentParameters.language:
Trace.error('No language in document')
return
if not DocumentParameters.language in TranslationConfig.languages:
Trace.error('Unknown language ' + DocumentParameters.language)
return
if TranslationConfig.languages[DocumentParameters.language] == 'en':
return
langcodes = [TranslationConfig.languages[DocumentParameters.language]]
try:
self.translation = gettext.translation('elyxer', None, langcodes)
except IOError:
Trace.error('No translation for ' + str(langcodes))
示例11: findtranslation
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import translation [as 别名]
def findtranslation(self):
"Find the translation for the document language."
self.langcodes = None
if not DocumentParameters.language:
Trace.error('No language in document')
return
if not DocumentParameters.language in TranslationConfig.languages:
Trace.error('Unknown language ' + DocumentParameters.language)
return
if TranslationConfig.languages[DocumentParameters.language] == 'en':
return
langcodes = [TranslationConfig.languages[DocumentParameters.language]]
try:
self.translation = gettext.translation('elyxer', None, langcodes)
except IOError:
Trace.error('No translation for ' + unicode(langcodes))
示例12: __resource_exists
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import translation [as 别名]
def __resource_exists(name):
"""Return true if the given resource exists"""
try:
open_resource(name).close()
return True
except IOError:
return False
# Enable this when we get some translations?
# We want an i18n API that is useful to programs using Python's gettext
# module, as well as the Zope3 i18n package. Perhaps we should just provide
# the POT file and translations, and leave it up to callers to make use
# of them.
#
# t = gettext.translation(
# 'pytz', os.path.join(os.path.dirname(__file__), 'locales'),
# fallback=True
# )
# def _(timezone_name):
# """Translate a timezone name using the current locale, returning Unicode"""
# return t.ugettext(timezone_name)
示例13: gettext
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import translation [as 别名]
def gettext(message):
"""
Translate the 'message' string. It uses the current thread to find the
translation object to use. If no current translation is activated, the
message will be run through the default translation object.
"""
global _default
eol_message = message.replace('\r\n', '\n').replace('\r', '\n')
if len(eol_message) == 0:
# Return an empty value of the corresponding type if an empty message
# is given, instead of metadata, which is the default gettext behavior.
result = type(message)("")
else:
_default = _default or translation(settings.LANGUAGE_CODE)
translation_object = getattr(_active, "value", _default)
result = translation_object.gettext(eol_message)
if isinstance(message, SafeData):
return mark_safe(result)
return result
示例14: resource_exists
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import translation [as 别名]
def resource_exists(name):
return loader.resource_exists(name)
# Enable this when we get some translations?
# We want an i18n API that is useful to programs using Python's gettext
# module, as well as the Zope3 i18n package. Perhaps we should just provide
# the POT file and translations, and leave it up to callers to make use
# of them.
#
# t = gettext.translation(
# 'pytz', os.path.join(os.path.dirname(__file__), 'locales'),
# fallback=True
# )
# def _(timezone_name):
# """Translate a timezone name using the current locale, returning Unicode"""
# return t.ugettext(timezone_name)
示例15: __init__
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import translation [as 别名]
def __init__(self, domain, localedir=None):
"""Establish a set of translation functions for the domain.
:param domain: Name of translation domain,
specifying a message catalog.
:type domain: str
:param localedir: Directory with translation catalogs.
:type localedir: str
"""
self.domain = domain
if localedir is None:
variable_name = _locale.get_locale_dir_variable_name(domain)
localedir = os.environ.get(variable_name)
self.localedir = localedir