Exemplo n.º 1
0
    def _load_domain(self, domain, fallback=True):
        """Load the given domain from one of the pre-configured locale dirs.

        Returns a :class:`gettext.NullTranslations` instance if no
        translations could be found for a non-critical domain. This
        allows untranslated plugins to be rendered in English instead
        of an error being raised.

        :param domain: A domain name.
        :param fallback: An optional flag that, when True, returns a
            :class:`gettext.NullTranslations` instance if no translations
            file could be found for the given language(s).
        :rtype: :class:`gettext.GNUTranslations`
        :returns: The native python translator instance for this domain.
        :raises DomainError: If no locale dir has been configured for this
            domain and the fallback is off.
        :raises LanguageError: If no translations could be found for this
            domain in this locale and the fallback is off.
        """
        localedir = self._locale_dirs.get(domain, None)
        if localedir:
            try:
                t = gettext_translation(domain, localedir, self._languages,
                                        fallback=fallback)
            except IOError:
                # This only occurs when fallback is false.
                msg = 'No %r translations found for %r at %r.' % \
                    (domain, self._languages, localedir)
                raise LanguageError(msg)
        elif fallback:
            t = NullTranslations()
        else:
            raise DomainError('No localedir specified for domain %r' % domain)
        self._domains[domain] = t
        return t
Exemplo n.º 2
0
    def _load_translations(self):
        from gettext import translation as gettext_translation

        i18n_kwargs = {
            "domain":
            self.config.get("pygrim:i18n:lang_domain"),
            "localedir":
            path.join(self.config.get("uwsgi:chdir"),
                      self.config.get("pygrim:i18n:locale_path"))
        }
        translations = {}
        for lang in self.config.get("pygrim:i18n:locales"):
            i18n_kwargs["languages"] = (lang, )
            try:
                translation = gettext_translation(**i18n_kwargs)
            except IOError:
                msg = "No translation file found for language %r in domain %r"
                log.error(msg, lang, i18n_kwargs["domain"])
                raise RuntimeError(msg % (lang, i18n_kwargs["domain"]))
            else:
                translations[lang] = translation

        try:
            default_locale = self.config.get("pygrim:i18n:default_locale")
        except KeyError:
            raise
        else:
            if default_locale not in translations:
                msg = "Default locale %r is not enabled. Known locales: %r"
                log.error(msg, default_locale, translations.keys())
                raise RuntimeError(msg % (default_locale, translations.keys()))

        log.debug("Loaded translations: %r", translations.keys())
        log.debug("Default translation: %r", default_locale)
        return translations
Exemplo n.º 3
0
 def _load_translations(self, domain, locale_dirs, fallback):
     for locale_dir in locale_dirs:
         try:
             yield gettext_translation(domain, locale_dir, self._languages, fallback=fallback)
         except IOError:
             # This only occurs when fallback is false and no translation was
             # found in <locale_dir>.
             pass
Exemplo n.º 4
0
 def _load_translations(self, domain, locale_dirs, fallback):
     for locale_dir in locale_dirs:
         try:
             yield gettext_translation(domain, locale_dir, self._languages, fallback=fallback)
         except IOError:
             # This only occurs when fallback is false and no translation was
             # found in <locale_dir>.
             pass
Exemplo n.º 5
0
    def _load_domain(self, domain, fallback=True):
        """Load the given domain from one of the pre-configured locale dirs.

        Returns a :class:`gettext.NullTranslations` instance if no
        translations could be found for a non-critical domain. This
        allows untranslated plugins to be rendered in English instead
        of an error being raised.

        :param domain: A domain name.
        :param fallback: An optional flag that, when True, returns a
            :class:`gettext.NullTranslations` instance if no translations
            file could be found for the given language(s).
        :rtype: :class:`gettext.GNUTranslations`
        :returns: The native python translator instance for this domain.
        :raises DomainError: If no locale dir has been configured for this
            domain and the fallback is off.
        :raises LanguageError: If no translations could be found for this
            domain in this locale and the fallback is off.
        """
        localedir = self._locale_dirs.get(domain, None)
        if localedir:
            try:
                t = gettext_translation(domain,
                                        localedir,
                                        self._languages,
                                        fallback=fallback)
            except IOError:
                # This only occurs when fallback is false.
                msg = 'No %r translations found for %r at %r.' % \
                    (domain, self._languages, localedir)
                raise LanguageError(msg)
        elif fallback:
            t = NullTranslations()
        else:
            raise DomainError('No localedir specified for domain %r' % domain)
        self._domains[domain] = t
        return t