Skip to content

Commit 784a0f3

Browse files
committed
Add localization for UI
1 parent 7ee2217 commit 784a0f3

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/bitmessagekivy/base_navigation.py

+31-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"""
33
Base class for Navigation Drawer
44
"""
5-
5+
import gettext
6+
import os
67
from kivy.lang import Observable
78

89
from kivy.properties import (
@@ -35,9 +36,35 @@ def __init__(self, defaultlang):
3536
self.ugettext = None
3637
self.lang = defaultlang
3738

38-
@staticmethod
39-
def _(text):
40-
return text
39+
def _(self, text):
40+
return self.ugettext(text)
41+
42+
def fbind(self, name, func, args, **kwargs):
43+
"""Function for binding to observers """
44+
if name == "_":
45+
self.observers.append((func, args, kwargs))
46+
else:
47+
return super(BaseLanguage, self).fbind(name, func, *args, **kwargs)
48+
49+
def funbind(self, name, func, args, **kwargs):
50+
"""Function for unbinding to observers """
51+
if name == "_":
52+
key = (func, args, kwargs)
53+
if key in self.observers:
54+
self.observers.remove(key)
55+
else:
56+
return super(BaseLanguage, self).funbind(name, func, *args, **kwargs)
57+
58+
def switch_lang(self, lang):
59+
"""Function language switching """
60+
# get the right locales directory, and instanciate a gettext
61+
locale_dir = os.path.join(os.path.dirname(__file__), 'translations', 'locales')
62+
locales = gettext.translation('langapp', locale_dir, languages=[lang])
63+
self.ugettext = locales.gettext
64+
65+
# update all the kv rules attached to this text
66+
for func, largs, in self.observers:
67+
func(largs, None, None)
4168

4269

4370
class BaseNavigationItem(OneLineAvatarIconListItem):

0 commit comments

Comments
 (0)