basic i18n

This commit is contained in:
Sander Land committed 2020-05-22 20:41:25 +02:00
1 parent 3f695840f3
commit 88d89fe90f
9 files changed
+108 -11

No files matched your search

-4
View File
@@ -77,10 +77,6 @@ coverage.xml
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
+8
View File
@@ -29,6 +29,7 @@ from katrain.gui.kivyutils import *
from katrain.gui.badukpan import BadukPanWidget
from katrain.gui.controlspanel import ControlsPanel
from katrain.gui.popups import NewGamePopup, ConfigPopup, LoadSGFPopup
from katrain.core.common import Lang
__version__ = "1.1.0"
@@ -327,8 +328,11 @@ class KaTrainGui(Screen):
return True
LANGUAGE = "nl"
i18n = Lang(LANGUAGE)
class KaTrainApp(MDApp):
gui = ObjectProperty(None)
language = StringProperty(LANGUAGE)
def build(self):
self.icon = ICON # how you're supposed to set an icon
@@ -340,6 +344,10 @@ class KaTrainApp(MDApp):
Window.bind(on_request_close=self.on_request_close)
return self.gui
def on_language(self, _instance, language):
self.gui.log(f"Switching language to {language}",OUTPUT_INFO)
i18n.switch_lang(language)
def on_start(self):
self.gui.start()
+41 -1
View File
@@ -1,6 +1,7 @@
import os
from typing import Any, List, Tuple
from kivy.lang import Observable
import gettext
try:
import importlib.resources as pkg_resources
except:
@@ -45,3 +46,42 @@ def find_package_resource(path):
return f"FILENOTFOUND::{path}"
else:
return path # absolute path
class Lang(Observable):
observers = []
lang = None
def __init__(self, defaultlang):
super(Lang, self).__init__()
self.ugettext = None
self.lang = defaultlang
self.switch_lang(self.lang)
def _(self, text):
return self.ugettext(text)
def fbind(self, name, func, *args, **kwargs):
if name == "_":
self.observers.append((func, args, kwargs))
else:
return super(Lang, self).fbind(name, func, *args, **kwargs)
def funbind(self, name, func, *args, **kwargs):
if name == "_":
key = (func, args, kwargs)
if key in self.observers:
self.observers.remove(key)
else:
return super(Lang, self).funbind(name, func, *args, **kwargs)
def switch_lang(self, lang):
# get the right locales directory, and instantiate a gettext
i18n_dir,_ = os.path.split(find_package_resource('katrain/i18n/__init__.py'))
locale_dir = os.path.join(i18n_dir,'locales')
locales = gettext.translation('katrain', locale_dir, languages=[lang])
self.ugettext = locales.gettext
# update all the kv rules attached to this text
for func, args, kwargs in self.observers:
func(args[0],None,None)
+9 -6
View File
@@ -1,4 +1,5 @@
#:kivy 1.11.0
#:import i18n __main__.i18n
#:set PLAYER_MODES ['Human', 'Teach','AI']
#:set PLAYER_MODE_VALUES ['human','human+undo','ai']
@@ -331,8 +332,8 @@
pos_hint: {'x': 0}
on_press: root.nav_drawer.set_state("open")
StyledButton:
text: 'Pass'
tooltip_text: '[space]\nPass'
text: i18n._('pass-button-text')
tooltip_text: i18n._('pass-button-tooltip')
id: pass_btn
size_hint: None, 0.5
pos_hint: {'x': 0.1,'center_y':0.5}
@@ -833,10 +834,12 @@
<ContentNavigationDrawer@MDBoxLayout+BackgroundColor>:
orientation: 'vertical'
background_color: BOX_BACKGROUND_COLOR
Label:
text: 'sdafs'
Label:
text: 'sdafs'
MDFlatButton:
text: i18n._('Dutch')
on_press: app.language = 'nl'
MDFlatButton:
text: i18n._('English')
on_press: app.language = 'en'
MDFlatButton:
text: 'size'
on_press: print(self.size,self.parent.size,self.parent.parent.size,self.parent.parent.parent.size,self.parent.parent.parent.parent.size)
View File
Whitespace-only changes.
Binary file not shown.
@@ -0,0 +1,27 @@
# KaTrain i18n file
msgid ""
msgstr ""
"Language: English\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
msgid "Hello World"
msgstr "Hello World"
msgid "KataGo"
msgstr "KataGo"
msgid "English"
msgstr "English"
msgid "Dutch"
msgstr "Dutch"
msgid "pass-button-text"
msgstr "Pass"
msgid "pass-button-tooltip"
msgstr "[space]\nPass"
Binary file not shown.
@@ -0,0 +1,23 @@
# KaTrain i18n file
msgid ""
msgstr ""
"Language: Nederlands\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
msgid "Hello World"
msgstr "Hallo wereld"
msgid "English"
msgstr "Engels"
msgid "Dutch"
msgstr "Nederlands"
msgid "pass-button-text"
msgstr "Pas"
msgid "pass-button-tooltip"
msgstr "[spatiebalk]\nPas"