Skip to content

Commit 34b3cec

Browse files
authored
Refactor TranslationHandler locales directory (#4692)
1 parent 0d6ffc3 commit 34b3cec

1 file changed

Lines changed: 5 additions & 19 deletions

File tree

archinstall/lib/translationhandler.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def json(self) -> str:
4444

4545
class TranslationHandler:
4646
def __init__(self) -> None:
47+
self._locales_dir = Path(__file__).parent.parent / 'locales'
4748
self._base_pot = 'base.pot'
4849
self._languages = 'languages.json'
4950
self._active_language: Language | None = None
@@ -136,7 +137,7 @@ def _get_translations(self) -> list[Language]:
136137

137138
try:
138139
# get a translation for a specific language
139-
translation = gettext.translation('base', localedir=self._get_locales_dir(), languages=(abbr, lang))
140+
translation = gettext.translation('base', localedir=self._locales_dir, languages=(abbr, lang))
140141
except FileNotFoundError as err:
141142
raise FileNotFoundError(f"Could not locate language file for '{lang}': {err}")
142143

@@ -158,10 +159,7 @@ def _load_language_mappings(self) -> list[dict[str, str]]:
158159
"""
159160
Load the mapping table of all known languages
160161
"""
161-
locales_dir = self._get_locales_dir()
162-
languages = Path.joinpath(locales_dir, self._languages)
163-
164-
with open(languages) as fp:
162+
with (self._locales_dir / self._languages).open() as fp:
165163
return json.load(fp)
166164

167165
def _get_catalog_size(self, translation: gettext.NullTranslations) -> int:
@@ -178,8 +176,7 @@ def _get_total_active_messages(self) -> int:
178176
"""
179177
Get total messages that could be translated
180178
"""
181-
locales = self._get_locales_dir()
182-
with open(f'{locales}/{self._base_pot}') as fp:
179+
with (self._locales_dir / self._base_pot).open() as fp:
183180
lines = fp.readlines()
184181
msgid_lines = [line for line in lines if 'msgid' in line]
185182

@@ -237,23 +234,12 @@ def apply_console_font(self) -> None:
237234
self._set_font(self.active_font)
238235
debug(f'Console font set from language mapping: {self.active_font}')
239236

240-
def _get_locales_dir(self) -> Path:
241-
"""
242-
Get the locales directory path
243-
"""
244-
cur_path = Path(__file__).parent.parent
245-
locales_dir = Path.joinpath(cur_path, 'locales')
246-
return locales_dir
247-
248237
def _provided_translations(self) -> list[str]:
249238
"""
250239
Get a list of all known languages
251240
"""
252-
locales_dir = self._get_locales_dir()
253-
filenames = os.listdir(locales_dir)
254-
255241
translation_files = []
256-
for filename in filenames:
242+
for filename in os.listdir(self._locales_dir):
257243
if len(filename) == 2 or filename in ['pt_BR', 'zh-CN', 'zh-TW']:
258244
translation_files.append(filename)
259245

0 commit comments

Comments
 (0)