Add localization for UI #2136

Open
osamacis wants to merge 1 commits from osamacis/Kivy-baseclass-localization into v0.6

View File

@ -2,7 +2,8 @@
""" """
Base class for Navigation Drawer Base class for Navigation Drawer
""" """
import gettext
import os
from kivy.lang import Observable from kivy.lang import Observable
from kivy.properties import ( from kivy.properties import (
@ -35,9 +36,35 @@ class BaseLanguage(Observable):
self.ugettext = None self.ugettext = None
self.lang = defaultlang self.lang = defaultlang
@staticmethod def _(self, text):
def _(text): return self.ugettext(text)
return 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): class BaseNavigationItem(OneLineAvatarIconListItem):