Skip to content

Commit

Permalink
Merge pull request #320 from janezd/move-localization
Browse files Browse the repository at this point in the history
localization: Move from `orangecanvas.utils` to `orangecanvas`
  • Loading branch information
markotoplak authored Dec 16, 2024
2 parents 86724ab + f1f9e82 commit ed4d4f9
Show file tree
Hide file tree
Showing 11 changed files with 371 additions and 324 deletions.
196 changes: 100 additions & 96 deletions i18n/si/msgs.jaml
Original file line number Diff line number Diff line change
Expand Up @@ -2515,6 +2515,104 @@ help/provider.py:
content: false
utf-16: false
latin-1: false
localization/__init__.py:
def `pl`:
'|': false
yY: false
aeiouAEIOU: false
ies: false
s: false
def `get_languages`:
orangecanvas: false
i18n: false
.json: false
'Invalid language file ': false
English: false
def `language_changed`:
application/language: false
application/last-used-language: false
def `update_last_used_language`:
application/language: false
English: true
application/last-used-language: false
class `Translator`:
def `__init__`:
biolab.si: false
Orange: false
application/language: false
i18n: false
{lang_eng}.json: false
{DEFAULT_LANGUAGE}.json: false
Missing language file {path}: false
def `c`:
<string>: false
eval: false
localization/si.py:
def `plsi`:
'|': false
a: false
i: false
e: false
ov: false
def `plsi_sz`:
{n:_}: false
_: false
1: false
z: false
zszzzzsssssssssszzzzzz: false
s: false
0: false
zzzssssszz: false
def `z_besedo`:
nič: false
m: false
en: false
enega: false
enemu: false
enem: false
enim: false
f: false
ena: false
ene: false
eni: false
eno: false
n: false
dva: false
dveh: false
dvema: false
dve: false
tri: false
treh: false
trem: false
tremi: false
štiri: false
štirih: false
štirim: false
štirimi: false
pet: false
petih: false
petim: false
petimi: false
šest: false
šestih: false
šestim: false
šestimi: false
sedem: false
sedmih: false
sedmim: false
sedmimi: false
osem: false
osmih: false
osmim: false
osmimi: false
devet: false
devetih: false
devetim: false
devetimi: false
deset: false
desetih: false
desetim: false
desetimi: false
preview/previewbrowser.py:
'

Expand Down Expand Up @@ -3653,100 +3751,6 @@ utils/shtools.py:
utf-8: false
wt: false
utils/localization/__init__.py:
def `pl`:
'|': false
yY: false
aeiouAEIOU: false
ies: false
s: false
def `get_languages`:
orangecanvas: false
i18n: false
.json: false
'Invalid language file ': false
English: false
def `language_changed`:
application/language: false
application/last-used-language: false
def `update_last_used_language`:
application/language: false
English: true
application/last-used-language: false
class `Translator`:
def `__init__`:
biolab.si: false
Orange: false
application/language: false
i18n: false
{lang_eng}.json: false
{DEFAULT_LANGUAGE}.json: false
Missing language file {path}: false
def `c`:
<string>: false
eval: false
import 'orangecanvas.localization', not 'orangecanvas.utils.localization': false
utils/localization/si.py:
def `plsi`:
'|': false
a: false
i: false
e: false
ov: false
def `plsi_sz`:
{n:_}: false
_: false
1: false
z: false
zszzzzsssssssssszzzzzz: false
s: false
0: false
zzzssssszz: false
def `z_besedo`:
nič: false
m: false
en: false
enega: false
enemu: false
enem: false
enim: false
f: false
ena: false
ene: false
eni: false
eno: false
n: false
dva: false
dveh: false
dvema: false
dve: false
tri: false
treh: false
trem: false
tremi: false
štiri: false
štirih: false
štirim: false
štirimi: false
pet: false
petih: false
petim: false
petimi: false
šest: false
šestih: false
šestim: false
šestimi: false
sedem: false
sedmih: false
sedmim: false
sedmimi: false
osem: false
osmih: false
osmim: false
osmimi: false
devet: false
devetih: false
devetim: false
devetimi: false
deset: false
desetih: false
desetim: false
desetimi: false
import 'orangecanvas.localization.si', not 'orangecanvas.utils.localization.si': false
4 changes: 2 additions & 2 deletions i18n/trubar-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ languages:
si:
name: Slovenščina
international-name: Slovenian
auto-import: from orangecanvas.utils.localization.si import plsi, plsi_sz, z_besedo # pylint: disable=wrong-import-order
auto-import: from orangecanvas.localization.si import plsi, plsi_sz, z_besedo # pylint: disable=wrong-import-order
auto-import: |2
from orangecanvas.utils.localization import Translator # pylint: disable=wrong-import-order
from orangecanvas.localization import Translator # pylint: disable=wrong-import-order
_tr = Translator("orangecanvas", "biolab.si", "Orange")
del Translator
encoding: "utf-8"
2 changes: 1 addition & 1 deletion orangecanvas/application/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Signal)

from .. import config
from ..utils.localization import get_languages
from ..localization import get_languages
from ..utils.settings import SettingChangedEvent
from ..utils.propertybindings import (
AbstractBoundProperty, PropertyBinding, BindingManager
Expand Down
126 changes: 126 additions & 0 deletions orangecanvas/localization/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
from functools import lru_cache
import warnings

import os
import json
import importlib

try:
from AnyQt.QtCore import QSettings, QLocale
except ImportError:
QSettings = QLocale = None


def pl(n: int, forms: str) -> str: # pylint: disable=invalid-name
"""
Choose a singular/plural form for English - or create one, for regular nouns
`forms` can be a string containing the singular and plural form, separated
by "|", for instance `pl(n, "leaf|leaves")`.
For nouns that are formed by adding an -s (e.g. tree -> trees),
and for nouns that end with -y that is replaced by -ies
(dictionary -> dictionaries), it suffices to pass the noun,
e.g. `pl(n, "tree")`, `pl(n, "dictionary")`.
Args:
n: number
forms: plural forms, separated by "|", or a single (regular) noun
Returns:
form corresponding to the given number
"""
plural = int(n != 1)

if "|" in forms:
return forms.split("|")[plural]

if forms[-1] in "yY" and forms[-2] not in "aeiouAEIOU":
word = [forms, forms[:-1] + "ies"][plural]
else:
word = forms + "s" * plural
if forms.isupper():
word = word.upper()
return word

def _load_json(path):
with open(path) as handle:
return json.load(handle)

@lru_cache
def get_languages(package=None):
if package is None:
package = "orangecanvas"
package_path = os.path.dirname(importlib.import_module(package).__file__)
msgs_path = os.path.join(package_path, "i18n")
if not os.path.exists(msgs_path):
return {}
names = {}
for name, ext in map(os.path.splitext, os.listdir(msgs_path)):
if ext == ".json":
try:
msgs = _load_json(os.path.join(msgs_path, name + ext))
except json.JSONDecodeError:
warnings.warn("Invalid language file "
+ os.path.join(msgs_path, name + ext))
else:
names[msgs[0]] = name
return names


if QLocale is not None:
DEFAULT_LANGUAGE = QLocale().languageToString(QLocale().language())
if DEFAULT_LANGUAGE not in get_languages():
DEFAULT_LANGUAGE = "English"
else:
DEFAULT_LANGUAGE = "English"


def language_changed():
assert QSettings is not None

s = QSettings()
lang = s.value("application/language", DEFAULT_LANGUAGE)
last_lang = s.value("application/last-used-language", DEFAULT_LANGUAGE)
return lang != last_lang


def update_last_used_language():
assert QSettings is not None

s = QSettings()
lang = s.value("application/language", "English")
s.setValue("application/last-used-language", lang)


class _list(list):
# Accept extra argument to allow for the original string
def __getitem__(self, item):
if isinstance(item, tuple):
item = item[0]
return super().__getitem__(item)


class Translator:
e = eval

def __init__(self, package, organization="biolab.si", application="Orange"):
if QSettings is not None:
s = QSettings(QSettings.IniFormat, QSettings.UserScope,
organization, application)
lang = s.value("application/language", DEFAULT_LANGUAGE)
else:
lang = 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__)
lang_eng = get_languages().get(lang, lang)
path = os.path.join(package_path, "i18n", f"{lang_eng}.json")
if not 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 = _list(_load_json(path))

# Extra argument(s) can give the original string or any other relevant data
def c(self, idx, *_):
return compile(self.m[idx], '<string>', 'eval')
Loading

0 comments on commit ed4d4f9

Please sign in to comment.