From 57368464ec2ca6b9aca9d6beccb3dfd140a967e5 Mon Sep 17 00:00:00 2001 From: janezd Date: Thu, 16 May 2024 18:29:57 +0200 Subject: [PATCH] Language selection: Use locale to choose default language --- orangecanvas/utils/localization/__init__.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/orangecanvas/utils/localization/__init__.py b/orangecanvas/utils/localization/__init__.py index 96dfbc6c..ef208203 100644 --- a/orangecanvas/utils/localization/__init__.py +++ b/orangecanvas/utils/localization/__init__.py @@ -2,7 +2,7 @@ import json import importlib -from AnyQt.QtCore import QSettings +from AnyQt.QtCore import QSettings, QLocale def pl(n: int, forms: str) -> str: # pylint: disable=invalid-name """ @@ -49,10 +49,15 @@ def get_languages(package=None): if ext == ".json"] +DEFAULT_LANGUAGE = QLocale().languageToString(QLocale().language()) +if DEFAULT_LANGUAGE not in get_languages(): + DEFAULT_LANGUAGE = "English" + + def language_changed(): s = QSettings() - lang = s.value("application/language", "English") - last_lang = s.value("application/last-used-language", "English") + lang = s.value("application/language", DEFAULT_LANGUAGE) + last_lang = s.value("application/last-used-language", DEFAULT_LANGUAGE) return lang != last_lang @@ -68,14 +73,14 @@ class Translator: def __init__(self, package, organization="biolab.si", application="Orange"): s = QSettings(QSettings.IniFormat, QSettings.UserScope, organization, application) - lang = s.value("application/language", "English") + lang = s.value("application/language", DEFAULT_LANGUAGE) # For testing purposes (and potential fallback) # lang = os.environ.get("ORANGE_LANG", "English") package_path = os.path.dirname(importlib.import_module(package).__file__) path = os.path.join(package_path, "i18n", f"{lang}.json") if not os.path.exists(path): - path = os.path.join(package_path, "i18n", "English.json") - assert os.path.exists(path) + path = os.path.join(package_path, "i18n", f"{DEFAULT_LANGUAGE}.json") + assert os.path.exists(path), f"Missing language file {path}" self.m = json.load(open(path)) def c(self, idx):