Skip to content

Commit ed4d4f9

Browse files
authored
Merge pull request #320 from janezd/move-localization
localization: Move from `orangecanvas.utils` to `orangecanvas`
2 parents 86724ab + f1f9e82 commit ed4d4f9

File tree

11 files changed

+371
-324
lines changed

11 files changed

+371
-324
lines changed

i18n/si/msgs.jaml

Lines changed: 100 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -2515,6 +2515,104 @@ help/provider.py:
25152515
content: false
25162516
utf-16: false
25172517
latin-1: false
2518+
localization/__init__.py:
2519+
def `pl`:
2520+
'|': false
2521+
yY: false
2522+
aeiouAEIOU: false
2523+
ies: false
2524+
s: false
2525+
def `get_languages`:
2526+
orangecanvas: false
2527+
i18n: false
2528+
.json: false
2529+
'Invalid language file ': false
2530+
English: false
2531+
def `language_changed`:
2532+
application/language: false
2533+
application/last-used-language: false
2534+
def `update_last_used_language`:
2535+
application/language: false
2536+
English: true
2537+
application/last-used-language: false
2538+
class `Translator`:
2539+
def `__init__`:
2540+
biolab.si: false
2541+
Orange: false
2542+
application/language: false
2543+
i18n: false
2544+
{lang_eng}.json: false
2545+
{DEFAULT_LANGUAGE}.json: false
2546+
Missing language file {path}: false
2547+
def `c`:
2548+
<string>: false
2549+
eval: false
2550+
localization/si.py:
2551+
def `plsi`:
2552+
'|': false
2553+
a: false
2554+
i: false
2555+
e: false
2556+
ov: false
2557+
def `plsi_sz`:
2558+
{n:_}: false
2559+
_: false
2560+
1: false
2561+
z: false
2562+
zszzzzsssssssssszzzzzz: false
2563+
s: false
2564+
0: false
2565+
zzzssssszz: false
2566+
def `z_besedo`:
2567+
nič: false
2568+
m: false
2569+
en: false
2570+
enega: false
2571+
enemu: false
2572+
enem: false
2573+
enim: false
2574+
f: false
2575+
ena: false
2576+
ene: false
2577+
eni: false
2578+
eno: false
2579+
n: false
2580+
dva: false
2581+
dveh: false
2582+
dvema: false
2583+
dve: false
2584+
tri: false
2585+
treh: false
2586+
trem: false
2587+
tremi: false
2588+
štiri: false
2589+
štirih: false
2590+
štirim: false
2591+
štirimi: false
2592+
pet: false
2593+
petih: false
2594+
petim: false
2595+
petimi: false
2596+
šest: false
2597+
šestih: false
2598+
šestim: false
2599+
šestimi: false
2600+
sedem: false
2601+
sedmih: false
2602+
sedmim: false
2603+
sedmimi: false
2604+
osem: false
2605+
osmih: false
2606+
osmim: false
2607+
osmimi: false
2608+
devet: false
2609+
devetih: false
2610+
devetim: false
2611+
devetimi: false
2612+
deset: false
2613+
desetih: false
2614+
desetim: false
2615+
desetimi: false
25182616
preview/previewbrowser.py:
25192617
'
25202618

@@ -3653,100 +3751,6 @@ utils/shtools.py:
36533751
utf-8: false
36543752
wt: false
36553753
utils/localization/__init__.py:
3656-
def `pl`:
3657-
'|': false
3658-
yY: false
3659-
aeiouAEIOU: false
3660-
ies: false
3661-
s: false
3662-
def `get_languages`:
3663-
orangecanvas: false
3664-
i18n: false
3665-
.json: false
3666-
'Invalid language file ': false
3667-
English: false
3668-
def `language_changed`:
3669-
application/language: false
3670-
application/last-used-language: false
3671-
def `update_last_used_language`:
3672-
application/language: false
3673-
English: true
3674-
application/last-used-language: false
3675-
class `Translator`:
3676-
def `__init__`:
3677-
biolab.si: false
3678-
Orange: false
3679-
application/language: false
3680-
i18n: false
3681-
{lang_eng}.json: false
3682-
{DEFAULT_LANGUAGE}.json: false
3683-
Missing language file {path}: false
3684-
def `c`:
3685-
<string>: false
3686-
eval: false
3754+
import 'orangecanvas.localization', not 'orangecanvas.utils.localization': false
36873755
utils/localization/si.py:
3688-
def `plsi`:
3689-
'|': false
3690-
a: false
3691-
i: false
3692-
e: false
3693-
ov: false
3694-
def `plsi_sz`:
3695-
{n:_}: false
3696-
_: false
3697-
1: false
3698-
z: false
3699-
zszzzzsssssssssszzzzzz: false
3700-
s: false
3701-
0: false
3702-
zzzssssszz: false
3703-
def `z_besedo`:
3704-
nič: false
3705-
m: false
3706-
en: false
3707-
enega: false
3708-
enemu: false
3709-
enem: false
3710-
enim: false
3711-
f: false
3712-
ena: false
3713-
ene: false
3714-
eni: false
3715-
eno: false
3716-
n: false
3717-
dva: false
3718-
dveh: false
3719-
dvema: false
3720-
dve: false
3721-
tri: false
3722-
treh: false
3723-
trem: false
3724-
tremi: false
3725-
štiri: false
3726-
štirih: false
3727-
štirim: false
3728-
štirimi: false
3729-
pet: false
3730-
petih: false
3731-
petim: false
3732-
petimi: false
3733-
šest: false
3734-
šestih: false
3735-
šestim: false
3736-
šestimi: false
3737-
sedem: false
3738-
sedmih: false
3739-
sedmim: false
3740-
sedmimi: false
3741-
osem: false
3742-
osmih: false
3743-
osmim: false
3744-
osmimi: false
3745-
devet: false
3746-
devetih: false
3747-
devetim: false
3748-
devetimi: false
3749-
deset: false
3750-
desetih: false
3751-
desetim: false
3752-
desetimi: false
3756+
import 'orangecanvas.localization.si', not 'orangecanvas.utils.localization.si': false

i18n/trubar-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ languages:
55
si:
66
name: Slovenščina
77
international-name: Slovenian
8-
auto-import: from orangecanvas.utils.localization.si import plsi, plsi_sz, z_besedo # pylint: disable=wrong-import-order
8+
auto-import: from orangecanvas.localization.si import plsi, plsi_sz, z_besedo # pylint: disable=wrong-import-order
99
auto-import: |2
10-
from orangecanvas.utils.localization import Translator # pylint: disable=wrong-import-order
10+
from orangecanvas.localization import Translator # pylint: disable=wrong-import-order
1111
_tr = Translator("orangecanvas", "biolab.si", "Orange")
1212
del Translator
1313
encoding: "utf-8"

orangecanvas/application/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
Signal)
2222

2323
from .. import config
24-
from ..utils.localization import get_languages
24+
from ..localization import get_languages
2525
from ..utils.settings import SettingChangedEvent
2626
from ..utils.propertybindings import (
2727
AbstractBoundProperty, PropertyBinding, BindingManager

orangecanvas/localization/__init__.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
from functools import lru_cache
2+
import warnings
3+
4+
import os
5+
import json
6+
import importlib
7+
8+
try:
9+
from AnyQt.QtCore import QSettings, QLocale
10+
except ImportError:
11+
QSettings = QLocale = None
12+
13+
14+
def pl(n: int, forms: str) -> str: # pylint: disable=invalid-name
15+
"""
16+
Choose a singular/plural form for English - or create one, for regular nouns
17+
18+
`forms` can be a string containing the singular and plural form, separated
19+
by "|", for instance `pl(n, "leaf|leaves")`.
20+
21+
For nouns that are formed by adding an -s (e.g. tree -> trees),
22+
and for nouns that end with -y that is replaced by -ies
23+
(dictionary -> dictionaries), it suffices to pass the noun,
24+
e.g. `pl(n, "tree")`, `pl(n, "dictionary")`.
25+
26+
Args:
27+
n: number
28+
forms: plural forms, separated by "|", or a single (regular) noun
29+
30+
Returns:
31+
form corresponding to the given number
32+
"""
33+
plural = int(n != 1)
34+
35+
if "|" in forms:
36+
return forms.split("|")[plural]
37+
38+
if forms[-1] in "yY" and forms[-2] not in "aeiouAEIOU":
39+
word = [forms, forms[:-1] + "ies"][plural]
40+
else:
41+
word = forms + "s" * plural
42+
if forms.isupper():
43+
word = word.upper()
44+
return word
45+
46+
def _load_json(path):
47+
with open(path) as handle:
48+
return json.load(handle)
49+
50+
@lru_cache
51+
def get_languages(package=None):
52+
if package is None:
53+
package = "orangecanvas"
54+
package_path = os.path.dirname(importlib.import_module(package).__file__)
55+
msgs_path = os.path.join(package_path, "i18n")
56+
if not os.path.exists(msgs_path):
57+
return {}
58+
names = {}
59+
for name, ext in map(os.path.splitext, os.listdir(msgs_path)):
60+
if ext == ".json":
61+
try:
62+
msgs = _load_json(os.path.join(msgs_path, name + ext))
63+
except json.JSONDecodeError:
64+
warnings.warn("Invalid language file "
65+
+ os.path.join(msgs_path, name + ext))
66+
else:
67+
names[msgs[0]] = name
68+
return names
69+
70+
71+
if QLocale is not None:
72+
DEFAULT_LANGUAGE = QLocale().languageToString(QLocale().language())
73+
if DEFAULT_LANGUAGE not in get_languages():
74+
DEFAULT_LANGUAGE = "English"
75+
else:
76+
DEFAULT_LANGUAGE = "English"
77+
78+
79+
def language_changed():
80+
assert QSettings is not None
81+
82+
s = QSettings()
83+
lang = s.value("application/language", DEFAULT_LANGUAGE)
84+
last_lang = s.value("application/last-used-language", DEFAULT_LANGUAGE)
85+
return lang != last_lang
86+
87+
88+
def update_last_used_language():
89+
assert QSettings is not None
90+
91+
s = QSettings()
92+
lang = s.value("application/language", "English")
93+
s.setValue("application/last-used-language", lang)
94+
95+
96+
class _list(list):
97+
# Accept extra argument to allow for the original string
98+
def __getitem__(self, item):
99+
if isinstance(item, tuple):
100+
item = item[0]
101+
return super().__getitem__(item)
102+
103+
104+
class Translator:
105+
e = eval
106+
107+
def __init__(self, package, organization="biolab.si", application="Orange"):
108+
if QSettings is not None:
109+
s = QSettings(QSettings.IniFormat, QSettings.UserScope,
110+
organization, application)
111+
lang = s.value("application/language", DEFAULT_LANGUAGE)
112+
else:
113+
lang = DEFAULT_LANGUAGE
114+
# For testing purposes (and potential fallback)
115+
# lang = os.environ.get("ORANGE_LANG", "English")
116+
package_path = os.path.dirname(importlib.import_module(package).__file__)
117+
lang_eng = get_languages().get(lang, lang)
118+
path = os.path.join(package_path, "i18n", f"{lang_eng}.json")
119+
if not os.path.exists(path):
120+
path = os.path.join(package_path, "i18n", f"{DEFAULT_LANGUAGE}.json")
121+
assert os.path.exists(path), f"Missing language file {path}"
122+
self.m = _list(_load_json(path))
123+
124+
# Extra argument(s) can give the original string or any other relevant data
125+
def c(self, idx, *_):
126+
return compile(self.m[idx], '<string>', 'eval')

0 commit comments

Comments
 (0)