Add localization for UI #2136
|
@ -2,7 +2,8 @@
|
|||
"""
|
||||
Base class for Navigation Drawer
|
||||
"""
|
||||
|
||||
import gettext
|
||||
import os
|
||||
from kivy.lang import Observable
|
||||
|
||||
from kivy.properties import (
|
||||
|
@ -35,9 +36,35 @@ class BaseLanguage(Observable):
|
|||
self.ugettext = None
|
||||
self.lang = defaultlang
|
||||
|
||||
@staticmethod
|
||||
def _(text):
|
||||
return text
|
||||
def _(self, text):
|
||||
return self.ugettext(text)
|
||||
|
||||
def fbind(self, name, func, args, **kwargs):
|
||||
"""Function for binding to observers """
|
||||
if name == "_":
|
||||
self.observers.append((func, args, kwargs))
|
||||
else:
|
||||
return super(BaseLanguage, self).fbind(name, func, *args, **kwargs)
|
||||
|
||||
def funbind(self, name, func, args, **kwargs):
|
||||
"""Function for unbinding to observers """
|
||||
if name == "_":
|
||||
key = (func, args, kwargs)
|
||||
if key in self.observers:
|
||||
self.observers.remove(key)
|
||||
else:
|
||||
return super(BaseLanguage, self).funbind(name, func, *args, **kwargs)
|
||||
|
||||
def switch_lang(self, lang):
|
||||
"""Function language switching """
|
||||
# get the right locales directory, and instanciate a gettext
|
||||
locale_dir = os.path.join(os.path.dirname(__file__), 'translations', 'locales')
|
||||
locales = gettext.translation('langapp', locale_dir, languages=[lang])
|
||||
self.ugettext = locales.gettext
|
||||
|
||||
# update all the kv rules attached to this text
|
||||
for func, largs, in self.observers:
|
||||
func(largs, None, None)
|
||||
|
||||
|
||||
class BaseNavigationItem(OneLineAvatarIconListItem):
|
||||
|
|
Reference in New Issue
Block a user