Skip to content

Commit

Permalink
Language selection: Use locale to choose default language
Browse files Browse the repository at this point in the history
  • Loading branch information
janezd committed May 16, 2024
1 parent 0dc6007 commit 5736846
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions orangecanvas/utils/localization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down Expand Up @@ -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


Expand All @@ -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):
Expand Down

0 comments on commit 5736846

Please sign in to comment.