Add some boilerplate code to add support for localization

This commit is contained in:
blitzmann
2020-05-15 21:55:48 -04:00
parent ac7ccfb6a3
commit fdd06ed3d6
5 changed files with 137 additions and 1 deletions

81
gui/app.py Normal file
View File

@@ -0,0 +1,81 @@
import wx
import config
import os
from logbook import Logger
pyfalog = Logger(__name__)
class PyfaApp(wx.App):
def OnInit(self):
"""
Do application initialization work, e.g. define application globals.
"""
# Name for my application.
self.appName = "pyfa"
#------------
# # Simplified init method.
# self.DoConfig()
# self.Init() # InspectionMixin
# # work around for Python stealing "_".
# sys.displayhook = _displayHook
#
# #------------
# Return locale folder.
localeDir = os.path.join(config.pyfaPath, "locale")
# Set language stuff and update to last used language.
self.locale = None
wx.Locale.AddCatalogLookupPathPrefix(localeDir)
# Set language stuff and update to last used language.
self.UpdateLanguage(config.language)
print(_("Market"))
return True
#-----------------------------------------------------------------------
def UpdateLanguage(self, lang = "en_US"):
"""
Update the language to the requested one.
Make *sure* any existing locale is deleted before the new
one is created. The old C++ object needs to be deleted
before the new one is created, and if we just assign a new
instance to the old Python variable, the old C++ locale will
not be destroyed soon enough, likely causing a crash.
:param string `lang`: one of the supported language codes.
"""
# Language domain.
langDomain = "lang"
# Languages you want to support.
supLang = {
"en_US": wx.LANGUAGE_ENGLISH,
"zh_CN": wx.LANGUAGE_CHINESE_SIMPLIFIED,
"de": wx.LANGUAGE_GERMAN
}
# If an unsupported language is requested default to English.
if lang in supLang:
selLang = supLang[lang]
else:
selLang = wx.LANGUAGE_ENGLISH
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, selLang)
if not success:
print("Langauage catalog not successfully loaded")
else:
self.locale = None

View File

@@ -143,6 +143,7 @@ class MainFrame(wx.Frame):
pyfalog.debug("Initialize MainFrame")
self.title = title
super().__init__(None, wx.ID_ANY, self.title)
self.supress_left_up = False
MainFrame.__instance = self
@@ -177,7 +178,7 @@ class MainFrame(wx.Frame):
shipBrowserImg = BitmapLoader.getImage("ship_small", "gui")
self.marketBrowser = MarketBrowser(self.notebookBrowsers)
self.notebookBrowsers.AddPage(self.marketBrowser, "Market", image=marketImg, closeable=False)
self.notebookBrowsers.AddPage(self.marketBrowser, _("Market"), image=marketImg, closeable=False)
self.marketBrowser.splitter.SetSashPosition(self.marketHeight)
self.shipBrowser = ShipBrowser(self.notebookBrowsers)

12
locale/README.md Normal file
View File

@@ -0,0 +1,12 @@
On my Windows wsl2 (more generic explaination is needed):
`find ./gui -iname "*.py" | xargs pygettext3 -d lang -o locale/lang.pot`
This will generate the pot that should be carried over to the various language directories and renamed .po
`msgfmt -o lang.mo lang`
Run in each language directory, will compile the .po files to .mo files
## Issues
`zh_CH` doesn't seem to work. AddCatalog is not functioning. See https://discuss.wxpython.org/t/localization-not-working-with-zh-ch-addcatalog-returns-false/34628

View File

@@ -0,0 +1,21 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2020-05-14 01:11-0400\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: pygettext.py 1.5\n"
#: ./gui/mainFrame.py:189
msgid "Market"
msgstr "de Market"

View File

@@ -0,0 +1,21 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2016-05-19 17:07"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: utf8\n"
"Generated-By: pygettext.py 1.5\n"
#: ./gui/mainFrame.py:189
msgid "Market"
msgstr "zhCH Market"