def ActivateTranslation(session, config, language, localedir=None): global ACTIVE_TRANSLATION, RECENTLY_TRANSLATED if not language: language = os.getenv('LANG', None) if not localedir: import mailpile.config.paths localedir = mailpile.config.paths.DEFAULT_LOCALE_DIRECTORY() trans = None if (not language) or language[:5].lower() in ('en', 'en_us', 'c'): trans = NullTranslations() elif language: try: trans = translation("mailpile", localedir, [language], codeset="utf-8") except IOError: if session: session.ui.debug('Failed to load language %s' % language) if not trans: trans = translation("mailpile", localedir, codeset='utf-8', fallback=True) if session: session.ui.debug('Failed to configure i18n (%s). ' 'Using fallback.' % language) if trans: with RECENTLY_TRANSLATED_LOCK: RECENTLY_TRANSLATED = [] ACTIVE_TRANSLATION = trans trans.org_gettext = trans.gettext trans.org_ngettext = trans.ngettext trans.gettext = lambda t, g: gettext(g) trans.ngettext = lambda t, s1, s2, n: ngettext(s1, s2, n) trans.set_output_charset("utf-8") if hasattr(config, 'jinja_env'): config.jinja_env.install_gettext_translations(trans, newstyle=True) if session and language and not isinstance(trans, NullTranslations): session.ui.debug(gettext('Loaded language %s') % language) return trans
def init(locale_dirs, language, catalog='sphinx', namespace='general'): # type: (List[unicode], unicode, unicode, unicode) -> Tuple[NullTranslations, bool] """Look for message catalogs in `locale_dirs` and *ensure* that there is at least a NullTranslations catalog set in `translators`. If called multiple times or if several ``.mo`` files are found, their contents are merged together (thus making ``init`` reentrant). """ global translators translator = translators.get((namespace, catalog)) # ignore previously failed attempts to find message catalogs if translator.__class__ is NullTranslations: translator = None # the None entry is the system's default locale path has_translation = True if language and '_' in language: # for language having country code (like "de_AT") languages = [language, language.split('_')[0]] else: languages = [language] # loading for dir_ in locale_dirs: try: trans = gettext.translation( catalog, localedir=dir_, # type: ignore languages=languages) if translator is None: translator = trans else: translator.add_fallback(trans) except Exception: # Language couldn't be found in the specified path pass # guarantee translators[(namespace, catalog)] exists if translator is None: translator = NullTranslations() has_translation = False translators[(namespace, catalog)] = translator if hasattr(translator, 'ugettext'): translator.gettext = translator.ugettext # type: ignore return translator, has_translation
def init(locale_dirs, language, catalog='sphinx', namespace='general'): # type: (List[unicode], unicode, unicode, unicode) -> Tuple[NullTranslations, bool] """Look for message catalogs in `locale_dirs` and *ensure* that there is at least a NullTranslations catalog set in `translators`. If called multiple times or if several ``.mo`` files are found, their contents are merged together (thus making ``init`` reentrable). """ global translators translator = translators.get((namespace, catalog)) # ignore previously failed attempts to find message catalogs if translator.__class__ is NullTranslations: translator = None # the None entry is the system's default locale path has_translation = True if language and '_' in language: # for language having country code (like "de_AT") languages = [language, language.split('_')[0]] else: languages = [language] # loading for dir_ in locale_dirs: try: trans = gettext.translation(catalog, localedir=dir_, # type: ignore languages=languages) if translator is None: translator = trans else: translator.add_fallback(trans) except Exception: # Language couldn't be found in the specified path pass # guarantee translators[(namespace, catalog)] exists if translator is None: translator = NullTranslations() has_translation = False translators[(namespace, catalog)] = translator if hasattr(translator, 'ugettext'): translator.gettext = translator.ugettext # type: ignore return translator, has_translation
def gettext(self, msg): translation = self.data.get(msg) if translation is not None: return translation return NullTranslations.gettext(self, msg)