From 5ed38b7a0962815af1673f6a8738e4b843182757 Mon Sep 17 00:00:00 2001 From: codefiles <11915375+codefiles@users.noreply.github.com> Date: Sun, 11 Sep 2022 23:09:46 -0400 Subject: [PATCH] Rework system locale --- archinstall/lib/locale_helpers.py | 77 ++++++++++++++----------------- 1 file changed, 34 insertions(+), 43 deletions(-) diff --git a/archinstall/lib/locale_helpers.py b/archinstall/lib/locale_helpers.py index 6776bd2791..83df7f14bf 100644 --- a/archinstall/lib/locale_helpers.py +++ b/archinstall/lib/locale_helpers.py @@ -29,7 +29,7 @@ def __init__(self, name: str, encoding: str = 'UTF-8'): self.encoding = encoding self.modifier = '' - # Make sure we extract the modifier, that way we can put it in if needed. + # Extract the modifier if found. if '@' in name: name, potential_modifier = name.split('@') @@ -44,7 +44,7 @@ def __init__(self, name: str, encoding: str = 'UTF-8'): if '.' in name: self.language, potential_encoding = name.split('.') - # Override encoding if the name contains an encoding that differs. + # Override encoding if name contains an encoding that differs. if encoding != potential_encoding: self.encoding = potential_encoding else: @@ -99,53 +99,36 @@ def __init__(self, locales: List[Locale] = [], target: str = ''): self.locale_gen = f'{target}/etc/locale.gen' self.locale_conf = f'{target}/etc/locale.conf' - def list_locales(self) -> List[Locale]: + def list_supported(self) -> List[Locale]: """ - Get a list of all the locales in the locale-gen configuration file. + Get a list of supported locales. - :return: A list of all the locales. + :return: A list of supported locales. :rtype: List[Locale] """ locales = [] - try: - with open(self.locale_gen, 'r') as fh: - entries = fh.readlines() - except FileNotFoundError: - log(f"Configuration file for locale-gen not found: '{self.locale_gen}'", fg="red", level=logging.ERROR) - else: - # Before the list of locales begins there is an empty line with a '#' in front - # so collect the locales from bottom up and halt when done. - entries.reverse() - - for entry in entries: - text = entry.replace('#', '').strip() - - if text == '': - break - - locales.append(Locale(*text.split())) - - locales.reverse() + for locale in list_locales(self.target): + locales.append(Locale(*locale.split())) return locales def verify_locales(self) -> bool: """ - Check if the locales match entries in the locale-gen configuration file. + Check if the locales match supported locales. If a match is found then update the name of the locale to the name of the matching entry in case they differ. :return: If matched return True else False. :rtype: bool """ - list_locales = self.list_locales() + supported = self.list_supported() found_all = True for locale in self.locales: found = False - for entry_locale in list_locales: - if locale == entry_locale: - locale.name = entry_locale.name + for entry in supported: + if locale == entry: + locale.name = entry.name found = True break @@ -327,7 +310,8 @@ def get_system_locale(self) -> str: except FileNotFoundError: pass else: - # Set up a regular expression pattern of a line beginning with 'LANG=' followed by and ending in a locale in optional double quotes. + # Set up a regular expression pattern of a line beginning with 'LANG=' + # followed by and ending in a locale in optional double quotes. pattern = re.compile(rf'^LANG="?(.+?)"?$') for line in lines: @@ -405,22 +389,29 @@ def run(self) -> bool: return True -def list_locales() -> List[str]: - with open('/etc/locale.gen', 'r') as fp: - locales = [] - # Before the list of locales begins there is an empty line with a '#' in front - # so collect the locales from bottom up and halt when done. - entries = fp.readlines() - entries.reverse() +def list_locales(target: str = '') -> List[str]: + supported = f'{target}/usr/share/i18n/SUPPORTED' + locales = [] - for entry in entries: - text = entry.replace('#', '').strip() - if text == '': + try: + with open(supported, 'r') as fh: + entries = fh.readlines() + except FileNotFoundError: + log(f"Supported locale file not found: '{supported}'", fg="red", level=logging.ERROR) + else: + # Remove lines that do not contain locales. + for index, line in enumerate(entries): + if line == 'SUPPORTED-LOCALES=\\\n': + entries = entries[index + 1:] break - locales.append(text) - locales.reverse() - return locales + # Remove C.UTF-8 since it is provided by the glibc package. + entries.remove('C.UTF-8/UTF-8 \\\n') + + for entry in entries: + locales.append(entry[:-3].replace('/', ' ')) + + return locales def get_locale_mode_text(mode): if mode == 'LC_ALL':