-
Notifications
You must be signed in to change notification settings - Fork 1
/
lang.py
41 lines (32 loc) · 1.18 KB
/
lang.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from kivy.lang import Observable
import gettext
from os.path import dirname, join
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):
locale_dir = join(dirname(__file__), 'data', 'locales')
locales = gettext.translation('flower', locale_dir, languages=[lang])
self.ugettext = locales.gettext
self.lang = lang
for func, largs, kwargs in self.observers:
func(largs, None, None)