locale/i18n: get default language and set it as early as possible
TODO elaborate xxxxx
This commit is contained in:
40
electrum/gui/default_lang.py
Normal file
40
electrum/gui/default_lang.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
# Copyright (C) 2023 The Electrum developers
|
||||||
|
# Distributed under the MIT software license, see the accompanying
|
||||||
|
# file LICENCE or http://www.opensource.org/licenses/mit-license.php
|
||||||
|
#
|
||||||
|
# Note: try not to import modules from electrum, or at least from GUIs.
|
||||||
|
# This is to avoid evaluating module-level string-translations before we get
|
||||||
|
# a chance to set the default language.
|
||||||
|
|
||||||
|
import os
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from electrum.i18n import languages
|
||||||
|
|
||||||
|
|
||||||
|
jLocale = None
|
||||||
|
if "ANDROID_DATA" in os.environ:
|
||||||
|
from jnius import autoclass, cast
|
||||||
|
jLocale = autoclass("java.util.Locale")
|
||||||
|
|
||||||
|
|
||||||
|
def get_default_language(*, gui_name: Optional[str] = None) -> str:
|
||||||
|
if gui_name == "qt":
|
||||||
|
from PyQt5.QtCore import QLocale
|
||||||
|
name = QLocale.system().name()
|
||||||
|
return name if name in languages else "en_UK"
|
||||||
|
elif gui_name == "qml":
|
||||||
|
from PyQt5.QtCore import QLocale
|
||||||
|
# On Android QLocale does not return the system locale
|
||||||
|
try:
|
||||||
|
name = str(jLocale.getDefault().toString())
|
||||||
|
except Exception:
|
||||||
|
name = QLocale.system().name()
|
||||||
|
return name if name in languages else "en_GB"
|
||||||
|
elif gui_name == "kivy":
|
||||||
|
if "ANDROID_DATA" not in os.environ:
|
||||||
|
return "en_UK"
|
||||||
|
# FIXME: CJK/Arabic/etc languages do not work at all with kivy due to font issues,
|
||||||
|
# so it is easiest to just default to English... (see #2032)
|
||||||
|
return "en_UK"
|
||||||
|
return ""
|
||||||
@@ -1,5 +1,10 @@
|
|||||||
import gettext
|
import gettext
|
||||||
|
|
||||||
|
from electrum.logging import get_logger
|
||||||
|
|
||||||
|
|
||||||
|
_logger = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class _(str):
|
class _(str):
|
||||||
|
|
||||||
@@ -35,6 +40,7 @@ class _(str):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def switch_lang(lang):
|
def switch_lang(lang):
|
||||||
|
_logger.info(f"switch_lang() called with {lang=!r}")
|
||||||
# get the right locales directory, and instantiate a gettext
|
# get the right locales directory, and instantiate a gettext
|
||||||
from electrum.i18n import LOCALE_DIR, set_language
|
from electrum.i18n import LOCALE_DIR, set_language
|
||||||
locales = gettext.translation('electrum', LOCALE_DIR, languages=[lang], fallback=True)
|
locales = gettext.translation('electrum', LOCALE_DIR, languages=[lang], fallback=True)
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
from kivy.utils import get_color_from_hex, platform
|
from kivy.utils import get_color_from_hex
|
||||||
|
|
||||||
|
from electrum.gui.default_lang import get_default_language as _get_default_language
|
||||||
|
|
||||||
|
|
||||||
def address_colors(wallet, addr):
|
def address_colors(wallet, addr):
|
||||||
@@ -24,13 +26,4 @@ def address_colors(wallet, addr):
|
|||||||
|
|
||||||
|
|
||||||
def get_default_language() -> str:
|
def get_default_language() -> str:
|
||||||
if platform != 'android':
|
return _get_default_language(gui_name="kivy")
|
||||||
return 'en_UK'
|
|
||||||
# FIXME: CJK/Arabic/etc languages do not work at all with kivy due to font issues,
|
|
||||||
# so it is easiest to just default to English... (see #2032)
|
|
||||||
return 'en_UK'
|
|
||||||
# # try getting the language of the Android OS
|
|
||||||
# from jnius import autoclass
|
|
||||||
# Locale = autoclass("java.util.Locale")
|
|
||||||
# lang = str(Locale.getDefault().toString())
|
|
||||||
# return lang if lang else 'en_UK'
|
|
||||||
|
|||||||
@@ -33,9 +33,6 @@ if TYPE_CHECKING:
|
|||||||
|
|
||||||
from .qeapp import ElectrumQmlApplication, Exception_Hook
|
from .qeapp import ElectrumQmlApplication, Exception_Hook
|
||||||
|
|
||||||
if 'ANDROID_DATA' in os.environ:
|
|
||||||
from jnius import autoclass, cast
|
|
||||||
jLocale = autoclass("java.util.Locale")
|
|
||||||
|
|
||||||
class ElectrumTranslator(QTranslator):
|
class ElectrumTranslator(QTranslator):
|
||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None):
|
||||||
@@ -51,12 +48,6 @@ class ElectrumGui(BaseElectrumGui, Logger):
|
|||||||
BaseElectrumGui.__init__(self, config=config, daemon=daemon, plugins=plugins)
|
BaseElectrumGui.__init__(self, config=config, daemon=daemon, plugins=plugins)
|
||||||
Logger.__init__(self)
|
Logger.__init__(self)
|
||||||
|
|
||||||
lang = config.get('language','')
|
|
||||||
if not lang:
|
|
||||||
lang = self.get_default_language()
|
|
||||||
self.logger.info(f'setting language {lang}')
|
|
||||||
set_language(lang)
|
|
||||||
|
|
||||||
# uncomment to debug plugin and import tracing
|
# uncomment to debug plugin and import tracing
|
||||||
# os.environ['QML_IMPORT_TRACE'] = '1'
|
# os.environ['QML_IMPORT_TRACE'] = '1'
|
||||||
# os.environ['QT_DEBUG_PLUGINS'] = '1'
|
# os.environ['QT_DEBUG_PLUGINS'] = '1'
|
||||||
@@ -119,12 +110,3 @@ class ElectrumGui(BaseElectrumGui, Logger):
|
|||||||
def stop(self):
|
def stop(self):
|
||||||
self.logger.info('closing GUI')
|
self.logger.info('closing GUI')
|
||||||
self.app.quit()
|
self.app.quit()
|
||||||
|
|
||||||
def get_default_language(self):
|
|
||||||
# On Android QLocale does not return the system locale
|
|
||||||
try:
|
|
||||||
name = str(jLocale.getDefault().toString())
|
|
||||||
except Exception:
|
|
||||||
name = QLocale.system().name()
|
|
||||||
self.logger.info(f'System default locale: {name}')
|
|
||||||
return name if name in languages else 'en_GB'
|
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ from electrum.logging import Logger
|
|||||||
from electrum.gui import BaseElectrumGui
|
from electrum.gui import BaseElectrumGui
|
||||||
|
|
||||||
from .installwizard import InstallWizard, WalletAlreadyOpenInMemory
|
from .installwizard import InstallWizard, WalletAlreadyOpenInMemory
|
||||||
from .util import get_default_language, read_QIcon, ColorScheme, custom_message_box, MessageBoxMixin
|
from .util import read_QIcon, ColorScheme, custom_message_box, MessageBoxMixin
|
||||||
from .main_window import ElectrumWindow
|
from .main_window import ElectrumWindow
|
||||||
from .network_dialog import NetworkDialog
|
from .network_dialog import NetworkDialog
|
||||||
from .stylesheet_patcher import patch_qt_stylesheet
|
from .stylesheet_patcher import patch_qt_stylesheet
|
||||||
@@ -111,7 +111,6 @@ class ElectrumGui(BaseElectrumGui, Logger):
|
|||||||
|
|
||||||
@profiler
|
@profiler
|
||||||
def __init__(self, *, config: 'SimpleConfig', daemon: 'Daemon', plugins: 'Plugins'):
|
def __init__(self, *, config: 'SimpleConfig', daemon: 'Daemon', plugins: 'Plugins'):
|
||||||
set_language(config.get('language', get_default_language()))
|
|
||||||
BaseElectrumGui.__init__(self, config=config, daemon=daemon, plugins=plugins)
|
BaseElectrumGui.__init__(self, config=config, daemon=daemon, plugins=plugins)
|
||||||
Logger.__init__(self)
|
Logger.__init__(self)
|
||||||
self.logger.info(f"Qt GUI starting up... Qt={QtCore.QT_VERSION_STR}, PyQt={QtCore.PYQT_VERSION_STR}")
|
self.logger.info(f"Qt GUI starting up... Qt={QtCore.QT_VERSION_STR}, PyQt={QtCore.PYQT_VERSION_STR}")
|
||||||
|
|||||||
@@ -1117,10 +1117,6 @@ class IconLabel(QWidget):
|
|||||||
self.icon.setPixmap(icon.pixmap(self.icon_size))
|
self.icon.setPixmap(icon.pixmap(self.icon_size))
|
||||||
self.icon.repaint() # macOS hack for #6269
|
self.icon.repaint() # macOS hack for #6269
|
||||||
|
|
||||||
def get_default_language():
|
|
||||||
name = QLocale.system().name()
|
|
||||||
return name if name in languages else 'en_UK'
|
|
||||||
|
|
||||||
|
|
||||||
def char_width_in_lineedit() -> int:
|
def char_width_in_lineedit() -> int:
|
||||||
char_width = QFontMetrics(QLineEdit().font()).averageCharWidth()
|
char_width = QFontMetrics(QLineEdit().font()).averageCharWidth()
|
||||||
|
|||||||
18
run_electrum
18
run_electrum
@@ -384,14 +384,22 @@ def main():
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
config = SimpleConfig(config_options)
|
config = SimpleConfig(config_options)
|
||||||
|
cmdname = config.get('cmd')
|
||||||
|
|
||||||
# set language as early as possible
|
# set language as early as possible
|
||||||
# Note: we are already too late for strings that are declared in the global scope
|
# Note: we are already too late for strings that are declared in the global scope
|
||||||
# of an already imported module. However, the GUI and the plugins at least have
|
# of an already imported module. However, the GUI and the plugins at least have
|
||||||
# not been imported yet.
|
# not been imported yet. (see #4621)
|
||||||
# Note: it is ok to call set_language() again later. E.g. the Qt GUI has additional
|
# Note: it is ok to call set_language() again later, but note that any call only applies
|
||||||
# tools to figure out the default language so it will get called again there.
|
# to not-yet-evaluated strings.
|
||||||
set_language(config.get('language'))
|
if cmdname == 'gui':
|
||||||
|
from electrum.gui.default_lang import get_default_language
|
||||||
|
gui_name = config.get('gui', 'qt')
|
||||||
|
lang = config.get('language')
|
||||||
|
if not lang:
|
||||||
|
lang = get_default_language(gui_name=gui_name)
|
||||||
|
_logger.info(f"get_default_language: detected default as {lang=!r}")
|
||||||
|
set_language(lang)
|
||||||
|
|
||||||
if config.get('testnet'):
|
if config.get('testnet'):
|
||||||
constants.set_testnet()
|
constants.set_testnet()
|
||||||
@@ -402,8 +410,6 @@ def main():
|
|||||||
elif config.get('signet'):
|
elif config.get('signet'):
|
||||||
constants.set_signet()
|
constants.set_signet()
|
||||||
|
|
||||||
cmdname = config.get('cmd')
|
|
||||||
|
|
||||||
if cmdname == 'daemon' and config.get("detach"):
|
if cmdname == 'daemon' and config.get("detach"):
|
||||||
# detect lockfile.
|
# detect lockfile.
|
||||||
# This is not as good as get_file_descriptor, but that would require the asyncio loop
|
# This is not as good as get_file_descriptor, but that would require the asyncio loop
|
||||||
|
|||||||
Reference in New Issue
Block a user