def lookup_translation(): ctx = _request_ctx_stack.top if ctx is None: return None translations = getattr(ctx, 'pycroft_translations', None) if translations is None: translations = Translations() for module in (pycroft, web): os.path.dirname(module.__file__) dirname = os.path.join(ctx.app.root_path, 'translations') translations.merge(Translations.load(dirname, [get_locale()])) ctx.pycroft_translations = translations return translations
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. """ locale_dirs = self._locale_dirs.get(domain, None) if locale_dirs: if isinstance(locale_dirs, basestring): locale_dirs = (locale_dirs, ) translation_list = self._load_translations(domain, locale_dirs, fallback) if (not fallback) and len(translation_list) == 0: msg = 'No %r translations found for %r in %r.' raise LanguageError(msg % (domain, self._languages, locale_dirs)) translations = Translations(domain=domain) for translation in translation_list: translations.merge(translation) elif fallback: translations = NullTranslations() else: raise DomainError('No localedir specified for domain %r' % domain) self._domains[domain] = translations return translations