Repository navigation
Expand file tree
/
Copy pathlocalization.py
More file actions
52 lines (39 loc) · 1.47 KB
/
Copy pathlocalization.py
File metadata and controls
52 lines (39 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import configparser
from functools import cache
from pathlib import Path
import re
kCatalogDirectory = Path(__file__).parent / "windows/languages"
kSubstitution = re.compile(r"%([1-9%n])")
@cache
def Languages():
return tuple(sorted(path.stem for path in kCatalogDirectory.glob("*.isl")))
@cache
def LoadMessages(language):
if language not in Languages():
raise ValueError("Unknown installer language: " + language)
parser = configparser.ConfigParser(interpolation=None)
parser.optionxform = str
with (kCatalogDirectory / (language + ".isl")).open(encoding="utf-8-sig") as stream:
parser.read_file(stream)
return dict(parser["CustomMessages"])
def Translate(value, language):
if isinstance(value, Message):
return Text(language, value.key, *value.arguments)
if isinstance(value, BaseException) and len(value.args) == 1:
return Translate(value.args[0], language)
return str(value)
def Text(language, key, *arguments):
def Substitute(match):
index = match.group(1)
if index == "%":
return "%"
if index == "n":
return "\n"
return Translate(arguments[int(index) - 1], language)
return kSubstitution.sub(Substitute, LoadMessages(language)[key])
class Message(str):
def __new__(cls, key, *arguments):
message = super().__new__(cls, Text("english", key, *arguments))
message.key = key
message.arguments = arguments
return message