Skip to content

Commit

Permalink
Finish up the dynamic translations
Browse files Browse the repository at this point in the history
  • Loading branch information
blitzmann committed Jul 25, 2020
1 parent ac1e6fe commit 1fe83dd
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 38 deletions.
4 changes: 3 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
"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
Expand Down Expand Up @@ -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())
Expand Down
29 changes: 13 additions & 16 deletions gui/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import wx
import config
import os

import sys
from logbook import Logger
pyfalog = Logger(__name__)
from service.settings import LocaleSettings
Expand Down Expand Up @@ -54,27 +54,24 @@ def UpdateLanguage(self, lang=None):
"""

# 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)
5 changes: 3 additions & 2 deletions gui/builtinPreferenceViews/pyfaGeneralPreferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,13 @@ def populatePanel(self, panel):
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)
Expand Down
3 changes: 2 additions & 1 deletion scripts/compile_lang.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os, glob
import msgfmt
import config

import sys

Expand All @@ -8,6 +9,6 @@

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()
26 changes: 8 additions & 18 deletions service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,47 +539,37 @@ def set(self, type, value):
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:
cls._instance = 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

0 comments on commit 1fe83dd

Please sign in to comment.