Finish up the dynamic translations

This commit is contained in:
blitzmann
2020-07-24 21:59:38 -04:00
parent ac1e6fe5b7
commit 1fe83ddcdf
5 changed files with 29 additions and 38 deletions

View File

@@ -53,6 +53,8 @@ LOGLEVEL_MAP = {
"debug": DEBUG,
}
CATALOG = 'lang'
slotColourMap = {
FittingSlot.LOW: wx.Colour(250, 235, 204), # yellow = low slots
FittingSlot.MED: wx.Colour(188, 215, 241), # blue = mid slots
@@ -192,7 +194,7 @@ def defPaths(customSavePath=None):
# set langauge, taking the passed argument or falling back to what's saved in the settings
localeSettings = LocaleSettings.getInstance()
language = language if language in localeSettings.supported_langauges else localeSettings.get('locale')
language = language or localeSettings.get('locale')
# sets the lang for eos, using the mapped langauge.
eos.config.set_lang(localeSettings.get_eos_locale())

View File

@@ -1,7 +1,7 @@
import wx
import config
import os
import sys
from logbook import Logger
pyfalog = Logger(__name__)
from service.settings import LocaleSettings
@@ -54,27 +54,24 @@ class PyfaApp(wx.App):
"""
# Language domain.
langDomain = "lang"
# Languages you want to support.
supLang = LocaleSettings.supported_langauges
langDomain = config.CATALOG
# If an unsupported language is requested default to English.
if lang in supLang:
selLang = supLang[lang].wxLocale
else:
selLang = wx.LANGUAGE_ENGLISH_US
if self.locale:
assert sys.getrefcount(self.locale) <= 2
del self.locale
# Create a locale object for this language.
pyfalog.debug("Setting language to: " + lang)
self.locale = wx.Locale(selLang)
if self.locale.IsOk():
success = self.locale.AddCatalog(langDomain)
if not success:
print("Langauage catalog not successfully loaded")
langInfo = wx.Locale.FindLanguageInfo(lang)
if langInfo is not None:
pyfalog.debug("Setting language to: " + lang)
self.locale = wx.Locale(langInfo.Language)
if self.locale.IsOk():
success = self.locale.AddCatalog(langDomain)
if not success:
print("Langauage catalog not successfully loaded")
else:
self.locale = None
pyfalog.debug("Cannot find langauge: " + lang)
self.locale = wx.Locale(wx.Locale.FindLanguageInfo(LocaleSettings.defaults['locale']).Language)

View File

@@ -102,12 +102,13 @@ class PFGeneralPref(PreferenceView):
self.stLangLabel.Wrap(-1)
langSizer.Add(self.stLangLabel, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
self.langChoices = sorted([wx.Locale.FindLanguageInfo(x) for x in wx.Translations.Get().GetAvailableTranslations('lang')], key=lambda x: x.Description)
self.langChoices = sorted([v for x, v in LocaleSettings.supported_langauges().items()], key=lambda x: x.Description)
self.chLang = wx.Choice(panel, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, [x.Description for x in self.langChoices], 0)
self.chLang.Bind(wx.EVT_CHOICE, self.onLangSelection)
self.chLang.SetStringSelection(self.localeSettings.get('locale'))
selectedIndex = self.langChoices.index(next((x for x in self.langChoices if x.CanonicalName == self.localeSettings.get('locale')), None))
self.chLang.SetSelection(selectedIndex)
langSizer.Add(self.chLang, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
langBox.Add(langSizer)

View File

@@ -1,5 +1,6 @@
import os, glob
import msgfmt
import config
import sys
@@ -8,6 +9,6 @@ locale_path = os.path.abspath(os.path.join(script_dir, '..', 'locale'))
for name in glob.iglob(locale_path + '/**'):
if not os.path.isfile(name):
path = os.path.join(locale_path, name, 'LC_MESSAGES', 'lang')
path = os.path.join(locale_path, name, 'LC_MESSAGES', config.CATALOG)
sys.argv[1:] = [path + '.po']
msgfmt.main()

View File

@@ -539,24 +539,18 @@ class GraphSettings:
Locale = namedtuple('Locale', ['wxLocale', 'eosLang'])
class LocaleSettings:
_instance = None
DEFAULT = "en"
supported_langauges = {
"en": Locale(wx.LANGUAGE_ENGLISH_US, 'en'),
"fr": Locale(wx.LANGUAGE_FRENCH, 'fr'),
"ja": Locale(wx.LANGUAGE_JAPANESE, 'ja'),
"ko": Locale(wx.LANGUAGE_KOREAN, 'ko'),
"ru": Locale(wx.LANGUAGE_RUSSIAN, 'ru'),
"zh": Locale(wx.LANGUAGE_CHINESE_SIMPLIFIED, 'zh'),
# Non game client langauges
"it": Locale(wx.LANGUAGE_ITALIAN, None),
}
DEFAULT = "en_US"
defaults = {
'locale': DEFAULT,
'eos_locale': 'Auto' # flag for "Default" which is the same as the locale or, if not available, English
}
@classmethod
def supported_langauges(cls):
"""Requires the application to be initialized, otherwise wx.Translation isn't set."""
return {x: wx.Locale.FindLanguageInfo(x) for x in wx.Translations.Get().GetAvailableTranslations(config.CATALOG)}
@classmethod
def getInstance(cls):
if cls._instance is None:
@@ -564,22 +558,18 @@ class LocaleSettings:
return cls._instance
def __init__(self):
self.settings = SettingsProvider.getInstance().getSettings('localeSettings', self.defaults)
def get(self, key):
"""gets the raw value fo the setting"""
return self.settings[key]
def get_eos_locale(self):
"""gets the effective value of the setting"""
val = self.settings['eos_locale']
return 'en'
return val if val != self.defaults['eos_locale'] else self.supported_langauges.get(self.settings['locale'], 'en').eosLang
return val if val != self.defaults['eos_locale'] else self.settings['locale'].split("_")[0]
def set(self, key, value):
if key == 'locale' and value not in self.supported_langauges:
if key == 'locale' and value not in self.supported_langauges():
self.settings[key] = self.DEFAULT
self.settings[key] = value