當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。