Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/archinstall/lib/translationhandler.py
diff options
context:
space:
mode:
Diffstat (limited to 'archinstall/lib/translationhandler.py')
-rw-r--r--archinstall/lib/translationhandler.py50
1 files changed, 46 insertions, 4 deletions
diff --git a/archinstall/lib/translationhandler.py b/archinstall/lib/translationhandler.py
index 58b7ebd4..d6b3ccb6 100644
--- a/archinstall/lib/translationhandler.py
+++ b/archinstall/lib/translationhandler.py
@@ -40,6 +40,7 @@ class Language:
def json(self) -> str:
return self.lang
+
class TranslationHandler:
_base_pot = 'base.pot'
_languages = 'languages.json'
@@ -48,7 +49,7 @@ class TranslationHandler:
# to display cyrillic languages correctly
self._set_font('UniCyr_8x16')
- self._total_messages = self._get_total_messages()
+ self._total_messages = self._get_total_active_messages()
self._translated_languages = self._get_translations()
@property
@@ -56,6 +57,9 @@ class TranslationHandler:
return self._translated_languages
def _get_translations(self) -> List[Language]:
+ """
+ Load all translated languages and return a list of such
+ """
mappings = self._load_language_mappings()
defined_languages = self._defined_languages()
@@ -68,13 +72,17 @@ class TranslationHandler:
translated_lang = mapping_entry.get('translated_lang', None)
try:
+ # get a translation for a specific language
translation = gettext.translation('base', localedir=self._get_locales_dir(), languages=(abbr, lang))
+ # calculate the percentage of total translated text to total number of messages
if abbr == 'en':
percent = 100
else:
num_translations = self._get_catalog_size(translation)
percent = int((num_translations / self._total_messages) * 100)
+ # prevent cases where the .pot file is out of date and the percentage is above 100
+ percent = min(100, percent)
language = Language(abbr, lang, translation, percent, translated_lang)
languages.append(language)
@@ -84,6 +92,9 @@ class TranslationHandler:
return languages
def _set_font(self, font: str):
+ """
+ Set the provided font as the new terminal font
+ """
from archinstall import SysCommand, log
try:
log(f'Setting font: {font}', level=logging.DEBUG)
@@ -92,6 +103,9 @@ class TranslationHandler:
log(f'Unable to set font {font}', level=logging.ERROR)
def _load_language_mappings(self) -> List[Dict[str, Any]]:
+ """
+ Load the mapping table of all known languages
+ """
locales_dir = self._get_locales_dir()
languages = Path.joinpath(locales_dir, self._languages)
@@ -99,34 +113,62 @@ class TranslationHandler:
return json.load(fp)
def _get_catalog_size(self, translation: gettext.NullTranslations) -> int:
- # this is a ery naughty way of retrieving the data but
+ """
+ Get the number of translated messages for a translations
+ """
+ # this is a very naughty way of retrieving the data but
# there's no alternative method exposed unfortunately
catalog = translation._catalog # type: ignore
messages = {k: v for k, v in catalog.items() if k and v}
return len(messages)
- def _get_total_messages(self) -> int:
+ def _get_total_active_messages(self) -> int:
+ """
+ Get total messages that could be translated
+ """
locales = self._get_locales_dir()
with open(f'{locales}/{self._base_pot}', 'r') as fp:
lines = fp.readlines()
msgid_lines = [line for line in lines if 'msgid' in line]
+
return len(msgid_lines) - 1 # don't count the first line which contains the metadata
- def get_language(self, abbr: str) -> Language:
+ def get_language_by_name(self, name: str) -> Language:
+ """
+ Get a language object by it's name, e.g. English
+ """
+ try:
+ return next(filter(lambda x: x.lang == name, self._translated_languages))
+ except Exception:
+ raise ValueError(f'No language with name found: {name}')
+
+ def get_language_by_abbr(self, abbr: str) -> Language:
+ """
+ Get a language object by its abbrevation, e.g. en
+ """
try:
return next(filter(lambda x: x.abbr == abbr, self._translated_languages))
except Exception:
raise ValueError(f'No language with abbreviation "{abbr}" found')
def activate(self, language: Language):
+ """
+ Set the provided language as the current translation
+ """
language.translation.install()
def _get_locales_dir(self) -> Path:
+ """
+ Get the locales directory path
+ """
cur_path = Path(__file__).parent.parent
locales_dir = Path.joinpath(cur_path, 'locales')
return locales_dir
def _defined_languages(self) -> List[str]:
+ """
+ Get a list of all known languages
+ """
locales_dir = self._get_locales_dir()
filenames = os.listdir(locales_dir)
return list(filter(lambda x: len(x) == 2 or x == 'pt_BR', filenames))