def get_translations(self): ctx = _get_current_context() if ctx is None: return support.NullTranslations() cache = self.get_translations_cache(ctx) locale = get_locale() try: return cache[str(locale), self.domain] except KeyError: translations = support.Translations() for dirname in self.translation_directories: catalog = support.Translations.load( dirname, [locale], self.domain ) translations.merge(catalog) # FIXME: Workaround for merge() being really, really stupid. It # does not copy _info, plural(), or any other instance variables # populated by GNUTranslations. We probably want to stop using # `support.Translations.merge` entirely. if hasattr(catalog, 'plural'): translations.plural = catalog.plural cache[str(locale), self.domain] = translations return translations
def get_translations(): """Returns the correct gettext translations that should be used for this request. This will never fail and return a dummy translation object if used outside of the request or if a translation cannot be found. """ ctx = _get_current_context() if ctx is None: return support.NullTranslations() translations = getattr(ctx, 'babel_translations', None) if translations is None: translations = support.Translations() babel = current_app.extensions['babel'] for dirname in babel.translation_directories: catalog = support.Translations.load(dirname, [get_locale()]) translations.merge(catalog) # FIXME: Workaround for merge() being really, really stupid. It # does not copy _info, plural(), or any other instance variables # populated by GNUTranslations. We probably want to stop using # `support.Translations.merge` entirely. if hasattr(catalog, 'plural'): translations.plural = catalog.plural ctx.babel_translations = translations return translations
def get_translations(request=None): """Returns the correct gettext translations that should be used for this request. This will never fail and return a dummy translation object if used outside of the request or if a translation cannot be found. """ if request is None: return support.NullTranslations() request_ = get_request_container(request) translations = request_.get("babel_translations", None) if translations is None: app = request.app locale = get_locale(request) if locale in app.ctx.babel_translations: request_["babel_translations"] = app.ctx.babel_translations[locale] return app.ctx.babel_translations[locale] translations = support.Translations() babel = app.ctx.babel_instance for dirname in babel.translation_directories: catalog = support.Translations.load(dirname, [locale]) translations.merge(catalog) # FIXME: Workaround for merge() being really, really stupid. It # does not copy _info, plural(), or any other instance variables # populated by GNUTranslations. We probably want to stop using # `support.Translations.merge` entirely. if hasattr(catalog, "plural"): translations.plural = catalog.plural request_["babel_translations"] = translations app.ctx.babel_translations[locale] = translations return translations
def get_translations(self): """Returns the correct gettext translations that should be used for this request. This will never fail and return a dummy translation object if used outside of the request or if a translation cannot be found. """ state = get_state(silent=True) if state is None: return support.NullTranslations() locale = get_locale() cache = self.get_translations_cache() translations = cache.get(str(locale)) if translations is None: dirname = self.get_translations_path(state.app) translations = support.Translations.load( dirname, locale, domain=self.domain ) self.cache[str(locale)] = translations return translations
def get_gettext_translations(self, locale): if locale in self._gettext_translations: return self._gettext_translations[locale] path, fp = self.find_mo_file(locale) if fp: trans = support.Translations(fp, domain='messages') else: trans = support.NullTranslations() self._gettext_translations[locale] = trans return trans
def get_translations(domain: str = None, locale: Locale = None) -> support.Translations: """Load and cache translations.""" if BABEL is None: raise RuntimeError('BabelMiddleware is not inited.') locale = locale or current_locale.get() if not locale: return support.NullTranslations() domain = domain or BABEL.domain if (domain, locale.language) not in BABEL.translations: translations = None for path in reversed(BABEL.locales_dirs): trans = support.Translations.load(path, locales=locale, domain=domain) if translations: translations._catalog.update(trans._catalog) else: translations = trans BABEL.translations[(domain, locale.language)] = translations return BABEL.translations[(domain, locale.language)]
def get_translations(self): """Returns the correct gettext translations that should be used for this request. This will never fail and return a dummy translation object if used outside of the request or if a translation cannot be found. """ ctx = _get_current_context() if ctx is None: return support.NullTranslations() locale = get_locale() # TODO: Looks like this will repeatedly hit disk when an # unsupported locale is requested. It shouldn't. translations = self.cache.get(str(locale)) if translations is None: dirname = self.get_translations_path() translations = support.Translations.load(dirname, locale, domain=self.domain) self.cache[str(locale)] = translations return translations
def setUp(self): fp = BytesIO() write_mo(fp, Catalog(locale='de')) fp.seek(0) self.translations = support.Translations(fp=fp) self.null_translations = support.NullTranslations(fp=fp)
def gettext(string): return context.get('translations', default=support.NullTranslations()).gettext(string)