log = logging.getLogger("i18n") # currently active translation object _trans = None # type: Optional[gettext_module.NullTranslations] def gettext(message): # type: (Text) -> Text if not _trans: return message return _trans.ugettext(message) gettext_lazy = make_lazy_gettext(lambda: gettext) def ngettext(message1, message2, count): # type: (Text, Text, int) -> Text return _trans.ungettext(message1, message2, count) LANGCACHE = {} # type: Dict[str, str] def reset_language(): """ Reset global state to initial. """ global _trans
# KeyError can happen with a settings file written to by more than one UH # installation (one that has compiled language files and one that hasn't) # ValueError can be raised by gettext if for instance the plural forms are # corrupted. log.warning("Configured language %s could not be loaded.", language) log.warning("Error: %s", err) log.warning("Continuing with English as fallback.") horizons.globals.fife.set_uh_setting('Language', LANGUAGENAMES['']) return change_language() # recurse else: # default locale if platform.system() == "Windows": # win doesn't set the language variable by default os.environ['LANGUAGE'] = locale.getdefaultlocale()[0] gettext.install('unknown-horizons', 'content/lang', unicode=True, names=['ngettext',]) # expose the plural-aware translate function as builtin N_ (gettext does the same to _) import __builtin__ __builtin__.__dict__['N_'] = __builtin__.__dict__['ngettext'] # update fonts new_locale = language or horizons.globals.fife.get_locale() fontdef = get_fontdef_for_locale(new_locale) horizons.globals.fife.pychan.loadFonts(fontdef) # dynamically reset all translations of active widgets update_all_translations() LanguageChanged.broadcast(None) _lazy = make_lazy_gettext(lambda: lambda s: _(s))
log.warning("Continuing with English as fallback.") horizons.globals.fife.set_uh_setting('Language', LANGUAGENAMES['']) return change_language() # recurse else: # default locale if platform.system( ) == "Windows": # win doesn't set the language variable by default os.environ['LANGUAGE'] = locale.getdefaultlocale()[0] gettext.install('unknown-horizons', 'content/lang', unicode=True, names=[ 'ngettext', ]) # expose the plural-aware translate function as builtin N_ (gettext does the same to _) import __builtin__ __builtin__.__dict__['N_'] = __builtin__.__dict__['ngettext'] # update fonts new_locale = language or horizons.globals.fife.get_locale() fontdef = get_fontdef_for_locale(new_locale) horizons.globals.fife.pychan.loadFonts(fontdef) # dynamically reset all translations of active widgets update_all_translations() LanguageChanged.broadcast(None) _lazy = make_lazy_gettext(lambda: lambda s: _(s))