-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslations.py
253 lines (217 loc) · 7.21 KB
/
translations.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/env python3
"""
Get human-readable values for i18n tokens
This module relies heavily on the data/translations/common.csv layout and will
likely break if that file is changed too much. For reference, the code below
makes the following assumptions:
rows[0] is the header
rows[0] has the structure:
[<empty>, ...languages..., <empty>, ...notes...]
If a token lacks a translation for the selected language, use the "en" one.
"""
import csv
import locale
import os
from . import materials
import utility.loghelper
logger = utility.loghelper.DelayLogger(__name__)
TRANSLATIONS = "data/translations/common.csv"
LANG_FALLBACK = "en"
PERK_MAP = {
"wand_radar": "radar_wand",
"item_radar": "radar_item",
"moon_radar": "radar_moon",
"enemy_radar": "radar_enemy"
}
def plural(num, word):
"Return '<num> <word>' with <word> plural, if needed"
if word:
if num != 1:
if word[-1] == "y":
word = word + "ies"
elif word[-1] in "aiou":
word = word + "es"
elif word[-1] == "e":
word = word + "s"
else:
word = word + "s"
return f"{num} {word}"
return f"{num}"
def get_preferred_language():
"Return the preferred two-letter language code"
lang, _ = locale.getlocale()
if not lang:
logger.warning("No locale defined! Defaulting to English")
return LANG_FALLBACK
return lang.split("_")[0]
def load_translations_csv(fpath):
"""
Load the common.csv translations file
See the module docstring for the assumptions this function makes.
"""
with open(fpath, "rt") as fobj:
rows = list(csv.reader(fobj))
headers = rows[0]
notes_col = headers.index("", 1) # see module docstring for explanation
lang_codes = headers[1:notes_col]
token_map = {}
for rownum in range(1, len(rows)):
row = rows[rownum]
token = row[0]
values = row[1:notes_col]
notes = row[notes_col:]
langmap = dict(zip(lang_codes, values))
if not token:
logger.trace("Skipping row %d %r; no token defined", rownum, row)
elif token in token_map:
curr = token_map[token]
logger.debug("Skipping duplicate token %r", token)
logger.trace("Have: (%r, %r)", langmap, notes)
logger.trace("Skip: (%r, %r)", curr.translations, curr.notes)
else:
token_map[token] = Token(token, langmap, notes)
return lang_codes, token_map
class Token:
"A single localized token"
def __init__(self, token, langmap, notes=None):
"See help(type(self))"
self._token = token
self._map = langmap
self._languages = tuple(langmap.keys())
self._notes = notes
@property
def token(self):
"Get the token this map translates"
return self._token
@property
def translations(self):
"Get the {lang_code: str} mapping"
return self._map
@property
def notes(self):
"Get the notes list, or None"
return self._notes
def get(self, lang=None, fallback=LANG_FALLBACK):
"Get the localized value for the specified language"
if lang is None:
lang = get_preferred_language()
return self._map.get(lang, self._map[fallback])
def __str__(self):
"str(self)"
return f"${self._token}"
def __repr__(self):
"repr(self)"
value = plural(len(self._map), "value")
if self._notes:
value += ", " + plural(len(self._notes), "note")
return f"Token({self._token!r}, {value})"
class LanguageMap:
"Map tokens to their localized values"
def __init__(self, game_path, language=None, defer=False):
"Construct the map; see help(type(self)) for signature"
self._game_path = game_path
self._language = language
if language is None:
self._language = get_preferred_language()
self._languages = []
self._tokens = {}
self._loaded = False
if not defer:
self.reload_map()
@property
def translations_file(self):
"Return the path to the translations CSV file"
return os.path.join(self._game_path, TRANSLATIONS)
@property
def languages(self):
"Return the known languages"
return self._languages
def has_token(self, token):
"True if the token is localized, False otherwise"
return token in self._tokens
def get_token(self, token):
"Get the Token instance for the given string"
if token in self._tokens:
return self._tokens[token]
raise ValueError(f"Token {token!r} not localized")
def reload_map(self):
"Load the translations CSV"
langs, tokens = load_translations_csv(self.translations_file)
self._languages = langs
self._tokens = tokens
self._loaded = True
def get(self, token, language=None, insertions=()):
"Localize a single token with optional insertions"
if token.startswith("$"):
token = token[1:]
if token in self._tokens:
phrase = self._tokens[token].get(lang=language)
if insertions:
for inum, ivalue in enumerate(insertions):
phrase = phrase.replace(f"${inum}", ivalue)
return phrase
logger.debug("Unknown token %r", token)
return token
def localize(self, phrase, *insertions, title=False):
"""
Localize a word or phrase with optional insertions
Localization is done word-by-word. Words without a leading "$" are
skipped.
"""
if not self._loaded:
return (phrase + " " + " ".join(insertions)).rstrip()
result = []
for word in phrase.split():
if word.startswith("$"):
result.append(self.get(word, insertions=insertions))
else:
result.append(word)
final_result = " ".join(result)
if title:
final_result = final_result.title()
logger.trace("localize(%r, *%r) = %r", phrase, insertions, final_result)
return final_result
def __iter__(self):
"Obtain the translation tokens"
yield from self._tokens.keys()
def __getitem__(self, token):
"Get translations for a given token"
return self._tokens[token].translations
def __call__(self, phrase, *insertions, **kwargs):
"Alias for self.localize"
return self.localize(phrase, *insertions, **kwargs)
# Noita-specific shorthand functions
def material(self, matid, with_as=False):
"Get the localized name of a material"
matid = materials.MATERIAL_MAP.get(matid, matid)
matstr = matid
if matid.startswith("mat_") or matstr.startswith("material_"):
matstr = self.localize("$" + matid)
elif "mat_" + matid in self._tokens:
matstr = self.localize("$mat_" + matid)
elif "material_" + matid in self._tokens:
matstr = self.localize("$material_" + matid)
if with_as and matstr != matid:
matstr += f" (as {matid})"
return matstr
def is_material(self, matid):
"True if the matid is a real material"
matid = materials.MATERIAL_MAP.get(matid, matid)
if matid.startswith("mat_") or matid.startswith("material_"):
return True
for prefix in ("mat", "material"):
if prefix + "_" + matid in self._tokens:
return True
return False
def perk(self, perkid, count=1):
"Get the localized name of a perk"
perkid = PERK_MAP.get(perkid, perkid)
perkstr = self.localize("$perk_" + perkid)
if count > 0:
perkstr += f" x{count}"
return perkstr
def is_perk(self, perkid):
"True if the perkid is a real perk"
perkid = PERK_MAP.get(perkid, perkid)
return "perk_" + perkid in self._tokens
# vim: set ts=2 sts=2 sw=2: