From c6d367d2a77c91464643055c207306dcd548258d Mon Sep 17 00:00:00 2001 From: osamacis Date: Mon, 12 Dec 2022 21:05:00 +0530 Subject: [PATCH] Add localization for UI --- src/bitmessagekivy/base_navigation.py | 35 ++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/bitmessagekivy/base_navigation.py b/src/bitmessagekivy/base_navigation.py index 5f6b1aa5..228309cd 100644 --- a/src/bitmessagekivy/base_navigation.py +++ b/src/bitmessagekivy/base_navigation.py @@ -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): -- 2.45.1