kivy: change language without restarting the app
This commit is contained in:
parent
0beb7e88d7
commit
2ffafbf634
|
@ -32,7 +32,6 @@ except ImportError:
|
||||||
|
|
||||||
# minimum required version for kivy
|
# minimum required version for kivy
|
||||||
kivy.require('1.8.0')
|
kivy.require('1.8.0')
|
||||||
from electrum.i18n import set_language
|
|
||||||
from kivy.logger import Logger
|
from kivy.logger import Logger
|
||||||
from main_window import ElectrumWindow
|
from main_window import ElectrumWindow
|
||||||
|
|
||||||
|
@ -43,7 +42,6 @@ class ElectrumGui:
|
||||||
self.network = daemon.network
|
self.network = daemon.network
|
||||||
self.config = config
|
self.config = config
|
||||||
self.plugins = plugins
|
self.plugins = plugins
|
||||||
set_language(config.get('language'))
|
|
||||||
|
|
||||||
def main(self):
|
def main(self):
|
||||||
w = ElectrumWindow(config=self.config,
|
w = ElectrumWindow(config=self.config,
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
#:import Clock kivy.clock.Clock
|
#:import Clock kivy.clock.Clock
|
||||||
#:import Window kivy.core.window.Window
|
#:import Window kivy.core.window.Window
|
||||||
#:import Factory kivy.factory.Factory
|
#:import Factory kivy.factory.Factory
|
||||||
#:import _ electrum.i18n._
|
#:import _ electrum_gui.kivy.i18n._
|
||||||
|
|
||||||
# Custom Global Widgets
|
# Custom Global Widgets
|
||||||
<Button>
|
<Button>
|
||||||
on_parent: self.MIN_STATE_TIME = 0.1
|
on_parent: self.MIN_STATE_TIME = 0.1
|
||||||
|
on_text: _.bind(self)
|
||||||
|
|
||||||
<EmptyLabel@Label>
|
<EmptyLabel@Label>
|
||||||
color: (0.8, 0.8, 0.8, 1)
|
color: (0.8, 0.8, 0.8, 1)
|
||||||
|
@ -28,6 +29,7 @@
|
||||||
markup: True
|
markup: True
|
||||||
font_name: 'Roboto'
|
font_name: 'Roboto'
|
||||||
font_size: '16sp'
|
font_size: '16sp'
|
||||||
|
on_text: _.bind(self)
|
||||||
|
|
||||||
<ListItemButton>
|
<ListItemButton>
|
||||||
font_size: '12sp'
|
font_size: '12sp'
|
||||||
|
|
|
@ -7,7 +7,7 @@ from decimal import Decimal
|
||||||
|
|
||||||
import electrum
|
import electrum
|
||||||
from electrum import WalletStorage, Wallet
|
from electrum import WalletStorage, Wallet
|
||||||
from electrum.i18n import _
|
from electrum_gui.kivy.i18n import _
|
||||||
from electrum.contacts import Contacts
|
from electrum.contacts import Contacts
|
||||||
from electrum.paymentrequest import InvoiceStore
|
from electrum.paymentrequest import InvoiceStore
|
||||||
from electrum.util import profiler, InvalidPassword
|
from electrum.util import profiler, InvalidPassword
|
||||||
|
@ -64,6 +64,12 @@ class ElectrumWindow(App):
|
||||||
|
|
||||||
electrum_config = ObjectProperty(None)
|
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):
|
def on_quotes(self, d):
|
||||||
print "main_window: on_quotes"
|
print "main_window: on_quotes"
|
||||||
pass
|
pass
|
||||||
|
@ -177,6 +183,7 @@ class ElectrumWindow(App):
|
||||||
|
|
||||||
title = _('Electrum App')
|
title = _('Electrum App')
|
||||||
self.electrum_config = config = kwargs.get('config', None)
|
self.electrum_config = config = kwargs.get('config', None)
|
||||||
|
self.language = config.get('language', 'en')
|
||||||
self.network = network = kwargs.get('network', None)
|
self.network = network = kwargs.get('network', None)
|
||||||
self.plugins = kwargs.get('plugins', [])
|
self.plugins = kwargs.get('plugins', [])
|
||||||
|
|
||||||
|
@ -447,7 +454,7 @@ class ElectrumWindow(App):
|
||||||
else:
|
else:
|
||||||
c, u, x = self.wallet.get_account_balance(self.current_account)
|
c, u, x = self.wallet.get_account_balance(self.current_account)
|
||||||
text = self.format_amount(c+x+u)
|
text = self.format_amount(c+x+u)
|
||||||
self.status = text.strip() + ' ' + self.base_unit
|
self.status = str(text.strip() + ' ' + self.base_unit)
|
||||||
else:
|
else:
|
||||||
self.status = _("Not connected")
|
self.status = _("Not connected")
|
||||||
|
|
||||||
|
@ -471,8 +478,6 @@ class ElectrumWindow(App):
|
||||||
|
|
||||||
@profiler
|
@profiler
|
||||||
def notify_transactions(self, *dt):
|
def notify_transactions(self, *dt):
|
||||||
'''
|
|
||||||
'''
|
|
||||||
if not self.network or not self.network.is_connected():
|
if not self.network or not self.network.is_connected():
|
||||||
return
|
return
|
||||||
# temporarily disabled for merge
|
# temporarily disabled for merge
|
||||||
|
|
|
@ -3,14 +3,15 @@ from kivy.factory import Factory
|
||||||
from kivy.properties import ObjectProperty
|
from kivy.properties import ObjectProperty
|
||||||
from kivy.lang import Builder
|
from kivy.lang import Builder
|
||||||
|
|
||||||
from electrum.i18n import _
|
|
||||||
from electrum.util import base_units
|
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.plugins import run_hook
|
||||||
from electrum.bitcoin import RECOMMENDED_FEE
|
from electrum.bitcoin import RECOMMENDED_FEE
|
||||||
|
|
||||||
Builder.load_string('''
|
Builder.load_string('''
|
||||||
#:import partial functools.partial
|
#:import partial functools.partial
|
||||||
|
#:import _ electrum_gui.kivy.i18n._
|
||||||
|
|
||||||
<SettingsItem@ButtonBehavior+BoxLayout>
|
<SettingsItem@ButtonBehavior+BoxLayout>
|
||||||
orientation: 'vertical'
|
orientation: 'vertical'
|
||||||
|
@ -57,8 +58,8 @@ Builder.load_string('''
|
||||||
size_hint: 1, None
|
size_hint: 1, None
|
||||||
SettingsItem:
|
SettingsItem:
|
||||||
lang: settings.get_language_name()
|
lang: settings.get_language_name()
|
||||||
title: _('Language') + ': %s'%self.lang
|
title: 'Language' + ': ' + str(self.lang)
|
||||||
description: _("Language")
|
description: _('Language')
|
||||||
action: partial(root.language_dialog, self)
|
action: partial(root.language_dialog, self)
|
||||||
height: '48dp'
|
height: '48dp'
|
||||||
SettingsItem:
|
SettingsItem:
|
||||||
|
@ -124,7 +125,7 @@ class SettingsDialog(Factory.Popup):
|
||||||
def cb(key):
|
def cb(key):
|
||||||
self.config.set_key("language", key, True)
|
self.config.set_key("language", key, True)
|
||||||
item.lang = self.get_language_name()
|
item.lang = self.get_language_name()
|
||||||
set_language(key)
|
self.app.language = key
|
||||||
d = ChoiceDialog(_('Language'), languages, l, cb)
|
d = ChoiceDialog(_('Language'), languages, l, cb)
|
||||||
d.open()
|
d.open()
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#:import _ electrum.i18n._
|
#:import _ electrum_gui.kivy.i18n._
|
||||||
#:import Factory kivy.factory.Factory
|
#:import Factory kivy.factory.Factory
|
||||||
#:set font_light 'gui/kivy/data/fonts/Roboto-Condensed.ttf'
|
#:set font_light 'gui/kivy/data/fonts/Roboto-Condensed.ttf'
|
||||||
#:set btc_symbol unichr(171)
|
#:set btc_symbol unichr(171)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#:import _ electrum.i18n._
|
#:import _ electrum_gui.kivy.i18n._
|
||||||
#:import Decimal decimal.Decimal
|
#:import Decimal decimal.Decimal
|
||||||
#:set btc_symbol unichr(171)
|
#:set btc_symbol unichr(171)
|
||||||
#:set mbtc_symbol unichr(187)
|
#:set mbtc_symbol unichr(187)
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
#:import _ electrum.i18n._
|
#:import _ electrum_gui.kivy.i18n._
|
||||||
#:import Decimal decimal.Decimal
|
#:import Decimal decimal.Decimal
|
||||||
|
|
||||||
#:import Factory kivy.factory.Factory
|
|
||||||
#:set btc_symbol unichr(171)
|
#:set btc_symbol unichr(171)
|
||||||
#:set mbtc_symbol unichr(187)
|
#:set mbtc_symbol unichr(187)
|
||||||
#:set font_light 'gui/kivy/data/fonts/Roboto-Condensed.ttf'
|
#:set font_light 'gui/kivy/data/fonts/Roboto-Condensed.ttf'
|
||||||
|
|
Loading…
Reference in New Issue