本文整理汇总了Python中django.utils.translation.trans_real.DjangoTranslation方法的典型用法代码示例。如果您正苦于以下问题:Python trans_real.DjangoTranslation方法的具体用法?Python trans_real.DjangoTranslation怎么用?Python trans_real.DjangoTranslation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.utils.translation.trans_real
的用法示例。
在下文中一共展示了trans_real.DjangoTranslation方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: merge_translation
# 需要导入模块: from django.utils.translation import trans_real [as 别名]
# 或者: from django.utils.translation.trans_real import DjangoTranslation [as 别名]
def merge_translation(self, context):
"""
Context wrapper which modifies the given language's translation catalog using the i18n service, if found.
"""
language = get_language()
i18n_service = context.get('_i18n_service', None)
if i18n_service:
# Cache the original translation object to reduce overhead
if language not in self._translations:
self._translations[language] = trans_real.DjangoTranslation(language)
translation = trans_real.translation(language)
translation.merge(i18n_service)
yield
# Revert to original translation object
if language in self._translations:
trans_real._translations[language] = self._translations[language] # pylint: disable=protected-access
# Re-activate the current language to reset translation caches
trans_real.activate(language)
示例2: get
# 需要导入模块: from django.utils.translation import trans_real [as 别名]
# 或者: from django.utils.translation.trans_real import DjangoTranslation [as 别名]
def get(self, request, *args, **kwargs):
locale = get_language()
domain = kwargs.get('domain', self.domain)
# If packages are not provided, default to all installed packages, as
# DjangoTranslation without localedirs harvests them all.
packages = kwargs.get('packages', '')
packages = packages.split('+') if packages else self.packages
paths = self.get_paths(packages) if packages else None
self.translation = DjangoTranslation(locale, domain=domain, localedirs=paths)
context = self.get_context_data(**kwargs)
return self.render_to_response(context)
示例3: translation
# 需要导入模块: from django.utils.translation import trans_real [as 别名]
# 或者: from django.utils.translation.trans_real import DjangoTranslation [as 别名]
def translation(language):
global _translations
if language not in _translations:
_translations[language] = DjangoTranslation(language, domain='dmoj-user')
return _translations[language]
示例4: get_javascript_catalog
# 需要导入模块: from django.utils.translation import trans_real [as 别名]
# 或者: from django.utils.translation.trans_real import DjangoTranslation [as 别名]
def get_javascript_catalog(locale, domain, packages):
app_configs = apps.get_app_configs()
allowable_packages = set(app_config.name for app_config in app_configs)
allowable_packages.update(DEFAULT_PACKAGES)
packages = [p for p in packages if p in allowable_packages]
paths = []
# paths of requested packages
for package in packages:
p = importlib.import_module(package)
path = os.path.join(os.path.dirname(upath(p.__file__)), 'locale')
paths.append(path)
trans = DjangoTranslation(locale, domain=domain, localedirs=paths)
trans_cat = trans._catalog
plural = None
if '' in trans_cat:
for line in trans_cat[''].split('\n'):
if line.startswith('Plural-Forms:'):
plural = line.split(':', 1)[1].strip()
if plural is not None:
# this should actually be a compiled function of a typical plural-form:
# Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 :
# n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;
plural = [el.strip() for el in plural.split(';') if el.strip().startswith('plural=')][0].split('=', 1)[1]
pdict = {}
maxcnts = {}
catalog = {}
trans_fallback_cat = trans._fallback._catalog if trans._fallback else {}
for key, value in itertools.chain(six.iteritems(trans_cat), six.iteritems(trans_fallback_cat)):
if key == '' or key in catalog:
continue
if isinstance(key, six.string_types):
catalog[key] = value
elif isinstance(key, tuple):
msgid = key[0]
cnt = key[1]
maxcnts[msgid] = max(cnt, maxcnts.get(msgid, 0))
pdict.setdefault(msgid, {})[cnt] = value
else:
raise TypeError(key)
for k, v in pdict.items():
catalog[k] = [v.get(i, '') for i in range(maxcnts[k] + 1)]
return catalog, plural