Skip to content

Commit 604c33a

Browse files
committed
- Fixed input text bug by moving one line in lang.py
- Changed instances of list[tuple[int, int]] to dict[str, str] where applicable - Added some type annotation
1 parent 9993dd7 commit 604c33a

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

lang.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,24 @@ def is_in_string(idx: int, string_positions: list[tuple[int, int]]) -> bool:
4343
"""
4444

4545
for start, end in string_positions:
46-
if start <= idx < end:
46+
if start < idx < end:
4747
return True
4848
return False
4949

5050

51-
def get_translations() -> list[tuple[str, str]]:
51+
def get_translations() -> dict[str, str]:
5252
"""
5353
Reads translatable keywords from a csv file.
5454
55-
:return: list of tuples containing python keywords and translatable keyword
55+
:return: a dictionary containing python keywords and translatable keyword
5656
"""
5757

58-
translations: list[tuple[str, str]] = []
58+
translations: dict[str, str] = {}
5959

6060
with open('lang.csv', encoding='utf-8', newline='') as f:
6161
reader = csv.reader(f)
6262
for row in reader:
63-
translations.append((row[0], row[1]))
63+
translations[row[0]] = row[1]
6464

6565
return translations
6666

@@ -77,14 +77,16 @@ def replacer(match) -> str:
7777
"""Replaces translatable keywords if they are not located in a string"""
7878

7979
idx = match.start()
80+
strings: list[tuple[int, int]] = get_strings(program)
81+
8082
return py_word \
8183
if not is_in_string(idx, strings) \
8284
else match.group(0)
8385

84-
strings = get_strings(program)
86+
translations: dict[str, str] = get_translations()
8587

86-
for py_word, lang_word in get_translations():
87-
pattern = r'\b' + re.escape(lang_word) + r'\b'
88+
for py_word in translations.keys():
89+
pattern = re.escape(translations[py_word])
8890
program = re.sub(pattern, replacer, program)
8991

9092
return program

0 commit comments

Comments
 (0)