commit 2ffafbf6348e4b6870cf6424680407ca338e261d
parent 0beb7e88d76423d71337c1539164081b96dc7850
Author: ThomasV <thomasv@electrum.org>
Date: Thu, 4 Feb 2016 10:49:51 +0100
kivy: change language without restarting the app
Diffstat:
8 files changed, 57 insertions(+), 17 deletions(-)
diff --git a/gui/kivy/__init__.py b/gui/kivy/__init__.py
@@ -32,7 +32,6 @@ except ImportError:
# minimum required version for kivy
kivy.require('1.8.0')
-from electrum.i18n import set_language
from kivy.logger import Logger
from main_window import ElectrumWindow
@@ -43,7 +42,6 @@ class ElectrumGui:
self.network = daemon.network
self.config = config
self.plugins = plugins
- set_language(config.get('language'))
def main(self):
w = ElectrumWindow(config=self.config,
diff --git a/gui/kivy/i18n.py b/gui/kivy/i18n.py
@@ -0,0 +1,36 @@
+import gettext
+
+class _(str):
+
+ observers = set()
+ lang = None
+
+ def __new__(cls, s, *args, **kwargs):
+ if _.lang is None:
+ _.switch_lang('en')
+ t = _.translate(s, *args, **kwargs)
+ o = super(_, cls).__new__(cls, t)
+ o.source_text = s
+ return o
+
+ @staticmethod
+ def translate(s, *args, **kwargs):
+ return _.lang(s).format(args, kwargs)
+
+ @staticmethod
+ def bind(label):
+ if isinstance(label.text, _):
+ _.observers.add(label)
+
+ @staticmethod
+ def switch_lang(lang):
+ # get the right locales directory, and instanciate a gettext
+ from electrum.i18n import LOCALE_DIR
+ locales = gettext.translation('electrum', LOCALE_DIR, languages=[lang], fallback=True)
+ _.lang = locales.gettext
+ for label in _.observers:
+ try:
+ label.text = _(label.text.source_text)
+ except ReferenceError:
+ pass
+
diff --git a/gui/kivy/main.kv b/gui/kivy/main.kv
@@ -1,11 +1,12 @@
#:import Clock kivy.clock.Clock
#:import Window kivy.core.window.Window
#:import Factory kivy.factory.Factory
-#:import _ electrum.i18n._
+#:import _ electrum_gui.kivy.i18n._
# Custom Global Widgets
<Button>
on_parent: self.MIN_STATE_TIME = 0.1
+ on_text: _.bind(self)
<EmptyLabel@Label>
color: (0.8, 0.8, 0.8, 1)
@@ -28,6 +29,7 @@
markup: True
font_name: 'Roboto'
font_size: '16sp'
+ on_text: _.bind(self)
<ListItemButton>
font_size: '12sp'
diff --git a/gui/kivy/main_window.py b/gui/kivy/main_window.py
@@ -7,7 +7,7 @@ from decimal import Decimal
import electrum
from electrum import WalletStorage, Wallet
-from electrum.i18n import _
+from electrum_gui.kivy.i18n import _
from electrum.contacts import Contacts
from electrum.paymentrequest import InvoiceStore
from electrum.util import profiler, InvalidPassword
@@ -64,6 +64,12 @@ class ElectrumWindow(App):
electrum_config = ObjectProperty(None)
+ language = StringProperty('en')
+
+ def on_language(self, instance, language):
+ Logger.info('language: {}'.format(language))
+ _.switch_lang(language)
+
def on_quotes(self, d):
print "main_window: on_quotes"
pass
@@ -177,6 +183,7 @@ class ElectrumWindow(App):
title = _('Electrum App')
self.electrum_config = config = kwargs.get('config', None)
+ self.language = config.get('language', 'en')
self.network = network = kwargs.get('network', None)
self.plugins = kwargs.get('plugins', [])
@@ -447,7 +454,7 @@ class ElectrumWindow(App):
else:
c, u, x = self.wallet.get_account_balance(self.current_account)
text = self.format_amount(c+x+u)
- self.status = text.strip() + ' ' + self.base_unit
+ self.status = str(text.strip() + ' ' + self.base_unit)
else:
self.status = _("Not connected")
@@ -471,8 +478,6 @@ class ElectrumWindow(App):
@profiler
def notify_transactions(self, *dt):
- '''
- '''
if not self.network or not self.network.is_connected():
return
# temporarily disabled for merge
diff --git a/gui/kivy/uix/dialogs/settings.py b/gui/kivy/uix/dialogs/settings.py
@@ -3,14 +3,15 @@ from kivy.factory import Factory
from kivy.properties import ObjectProperty
from kivy.lang import Builder
-from electrum.i18n import _
from electrum.util import base_units
-from electrum.i18n import languages, set_language
+from electrum.i18n import languages
+from electrum_gui.kivy.i18n import _
from electrum.plugins import run_hook
from electrum.bitcoin import RECOMMENDED_FEE
Builder.load_string('''
#:import partial functools.partial
+#:import _ electrum_gui.kivy.i18n._
<SettingsItem@ButtonBehavior+BoxLayout>
orientation: 'vertical'
@@ -57,8 +58,8 @@ Builder.load_string('''
size_hint: 1, None
SettingsItem:
lang: settings.get_language_name()
- title: _('Language') + ': %s'%self.lang
- description: _("Language")
+ title: 'Language' + ': ' + str(self.lang)
+ description: _('Language')
action: partial(root.language_dialog, self)
height: '48dp'
SettingsItem:
@@ -124,7 +125,7 @@ class SettingsDialog(Factory.Popup):
def cb(key):
self.config.set_key("language", key, True)
item.lang = self.get_language_name()
- set_language(key)
+ self.app.language = key
d = ChoiceDialog(_('Language'), languages, l, cb)
d.open()
diff --git a/gui/kivy/uix/ui_screens/history.kv b/gui/kivy/uix/ui_screens/history.kv
@@ -1,4 +1,4 @@
-#:import _ electrum.i18n._
+#:import _ electrum_gui.kivy.i18n._
#:import Factory kivy.factory.Factory
#:set font_light 'gui/kivy/data/fonts/Roboto-Condensed.ttf'
#:set btc_symbol unichr(171)
diff --git a/gui/kivy/uix/ui_screens/receive.kv b/gui/kivy/uix/ui_screens/receive.kv
@@ -1,4 +1,4 @@
-#:import _ electrum.i18n._
+#:import _ electrum_gui.kivy.i18n._
#:import Decimal decimal.Decimal
#:set btc_symbol unichr(171)
#:set mbtc_symbol unichr(187)
diff --git a/gui/kivy/uix/ui_screens/send.kv b/gui/kivy/uix/ui_screens/send.kv
@@ -1,7 +1,5 @@
-#:import _ electrum.i18n._
+#:import _ electrum_gui.kivy.i18n._
#:import Decimal decimal.Decimal
-
-#:import Factory kivy.factory.Factory
#:set btc_symbol unichr(171)
#:set mbtc_symbol unichr(187)
#:set font_light 'gui/kivy/data/fonts/Roboto-Condensed.ttf'