Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/archinstall/lib/translation.py
diff options
context:
space:
mode:
authorDaniel <blackrabbit256@gmail.com>2022-02-05 10:27:29 +1100
committerGitHub <noreply@github.com>2022-02-05 00:27:29 +0100
commitf6d133804b7fdfab5a00d95a1985c3e220935ac1 (patch)
tree7eeeedf57b2d8068ce8f382ebbe830af77f58df0 /archinstall/lib/translation.py
parent68c2988358426e8d0074479cef539ddadc2a31e6 (diff)
Provide nationalization (#893)
* Nationalization * Add _ as builtins to flake8 * Removing conflict hash tag Co-authored-by: Daniel Girtler <girtler.daniel@gmail.com> Co-authored-by: Anton Hvornum <anton@hvornum.se>
Diffstat (limited to 'archinstall/lib/translation.py')
-rw-r--r--archinstall/lib/translation.py82
1 files changed, 82 insertions, 0 deletions
diff --git a/archinstall/lib/translation.py b/archinstall/lib/translation.py
new file mode 100644
index 00000000..1ae6989d
--- /dev/null
+++ b/archinstall/lib/translation.py
@@ -0,0 +1,82 @@
+from __future__ import annotations
+
+import json
+import os
+import gettext
+
+from pathlib import Path
+from typing import List, Dict
+
+
+class Languages:
+ def __init__(self):
+ self._mappings = self._get_language_mappings()
+
+ def _get_language_mappings(self) -> List[Dict[str, str]]:
+ locales_dir = Translation.get_locales_dir()
+ languages = Path.joinpath(locales_dir, 'languages.json')
+
+ with open(languages, 'r') as fp:
+ return json.load(fp)
+
+ def get_language(self, abbr: str) -> str:
+ for entry in self._mappings:
+ if entry['abbr'] == abbr:
+ return entry['lang']
+
+ raise ValueError(f'No language with abbrevation "{abbr}" found')
+
+
+class DeferredTranslation:
+ def __init__(self, message):
+ self.message = message
+
+ def __len__(self) -> int:
+ return len(self.message)
+
+ def __str__(self) -> str:
+ translate = _
+ if translate is DeferredTranslation:
+ return self.message
+ return translate(self.message)
+
+ @classmethod
+ def install(cls):
+ import builtins
+ builtins._ = cls
+
+
+class Translation:
+ def __init__(self, locales_dir):
+ DeferredTranslation.install()
+
+ self._languages = {}
+
+ for name in self.get_all_names():
+ self._languages[name] = gettext.translation('base', localedir=locales_dir, languages=[name])
+
+ def activate(self, name):
+ if language := self._languages.get(name, None):
+ language.install()
+ else:
+ raise ValueError(f'Language not supported: {name}')
+
+ @classmethod
+ def load_nationalization(cls) -> Translation:
+ locales_dir = cls.get_locales_dir()
+ return Translation(locales_dir)
+
+ @classmethod
+ def get_locales_dir(cls) -> Path:
+ cur_path = Path(__file__).parent.parent
+ locales_dir = Path.joinpath(cur_path, 'locales')
+ return locales_dir
+
+ @classmethod
+ def get_all_names(cls) -> List[str]:
+ locales_dir = cls.get_locales_dir()
+ filenames = os.listdir(locales_dir)
+ def_languages = filter(lambda x: len(x) == 2, filenames)
+
+ languages = Languages()
+ return [languages.get_language(lang) for lang in def_languages]