当前位置: 首页>>代码示例>>Python>>正文


Python trans_real.DjangoTranslation方法代码示例

本文整理汇总了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) 
开发者ID:edx,项目名称:xblock-utils,代码行数:23,代码来源:i18n.py

示例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) 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:13,代码来源:i18n.py

示例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] 
开发者ID:DMOJ,项目名称:online-judge,代码行数:7,代码来源:user_translations.py

示例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 
开发者ID:Yeah-Kun,项目名称:python,代码行数:48,代码来源:i18n.py


注:本文中的django.utils.translation.trans_real.DjangoTranslation方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。