def change_language(language=None): """Load/change the language of Unknown Horizons. Called on startup and when changing the language in the settings menu. """ if language: # non-default try: # NOTE about gettext fallback mechanism: # English is not shipped as .mo file, thus if English is # selected we use NullTranslations to get English output. fallback = (language == 'en') trans = gettext.translation('unknown-horizons', find_available_languages()[language], languages=[language], fallback=fallback) trans.install(unicode=True, names=[ 'ngettext', ]) except (IOError, KeyError, ValueError) as err: # 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)
def change_language(language=None): """Load/change the language of Unknown Horizons. Called on startup and when changing the language in the settings menu. """ if language: # non-default try: # NOTE about gettext fallback mechanism: # English is not shipped as .mo file, thus if English is # selected we use NullTranslations to get English output. fallback = (language == 'en') trans = gettext.translation('unknown-horizons', find_available_languages()[language], languages=[language], fallback=fallback) trans.install(unicode=True, names=['ngettext',]) except (IOError, KeyError, ValueError) as err: # 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)
def update_languages(self, data=None): """ Load/Change language of Unknown Horizons. Called on startup and when changing the language. data is used when changing the language in the settings menu. """ if data is None: data = self._setting.get(UH_MODULE, "Language") # get language key symbol = LANGUAGENAMES.get_by_value(data) if symbol != '': # non-default try: # NOTE about gettext fallback mechanism: # English is not shipped as .mo file, thus if English is # selected we use NullTranslations to get English output. fallback = (symbol == 'en') trans = gettext.translation('unknown-horizons', find_available_languages()[symbol], \ languages=[symbol], fallback=fallback) trans.install(unicode=True, names=['ngettext',]) except IOError: #xgettext:python-format print _("Configured language {lang} could not be loaded").format(lang=symbol) self._setting.set(UH_MODULE, "Language", LANGUAGENAMES['']) return self.update_languages() # 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',]) # update fonts fontdef = get_fontdef_for_locale(symbol) self.engine.pychan.loadFonts(fontdef) # dynamically reset all translations of active widgets update_all_translations()
def change_language(language=None): """Load/change the language of Unknown Horizons. Called on startup and when changing the language in the settings menu. """ if language: # non-default try: # NOTE about gettext fallback mechanism: # English is not shipped as .mo file, thus if English is # selected we use NullTranslations to get English output. fallback = language == "en" trans = gettext.translation( "unknown-horizons", find_available_languages()[language], languages=[language], fallback=fallback ) trans.install(unicode=True, names=["ngettext"]) except IOError: # xgettext:python-format print "Configured language {lang} could not be loaded.".format(lang=language) 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 fontdef = get_fontdef_for_locale(language or horizons.globals.fife.get_locale()) horizons.globals.fife.pychan.loadFonts(fontdef) # dynamically reset all translations of active widgets update_all_translations()