[PATCH] Use the firmware-provided language if it's something we support.

Vratislav Podzimek vpodzime at redhat.com
Mon May 6 10:34:34 UTC 2013


On Fri, 2013-05-03 at 14:20 -0400, Peter Jones wrote:
> UEFI provides an easy method of checking what language is selected from
> the firmware.  This should match up with where the machine is sold, so
> has a high chance of being a reasonable default.
> 
> Signed-off-by: Peter Jones <pjones at redhat.com>
> ---
>  pyanaconda/localization.py | 50 ++++++++++++++++++++++++++++++++++++++++++----
>  1 file changed, 46 insertions(+), 4 deletions(-)
> 
> diff --git a/pyanaconda/localization.py b/pyanaconda/localization.py
> index 38c5a76..e036d2e 100644
> --- a/pyanaconda/localization.py
> +++ b/pyanaconda/localization.py
> @@ -24,6 +24,8 @@ import gettext
>  import locale as locale_mod
>  import os
>  import re
> +import logging
> +log = logging.getLogger("anaconda")
>  
>  import babel
>  
> @@ -354,8 +356,11 @@ class Language(object):
>          self.translations = {repr(locale):locale
>                               for locale in get_available_translations()}
>          self.locales = {repr(locale):locale for locale in get_all_locales()}
> -        self.preferred_translation = self.translations[DEFAULT_LANG]
> -        self.preferred_locales = [self.locales[DEFAULT_LANG]]
> +
> +        self.system_lang = self._get_firmware_language()
> +        self.install_lang = self.system_lang
> +        self.preferred_translation = self.translations[self.system_lang]
> +        self.preferred_locales = [self.locales[self.system_lang]]
>          self.preferred_locale = self.preferred_locales[0]
>  
>          self.all_preferences = preferences
> @@ -364,8 +369,45 @@ class Language(object):
>          if self.territory:
>              self._get_preferred_translation_and_locales()
>  
> -        self.install_lang = DEFAULT_LANG
> -        self.system_lang = DEFAULT_LANG
> +    def _get_firmware_language(self):
> +        try:
> +            n = "/sys/firmware/efi/efivars/PlatformLang-8be4df61-93ca-11d2-aa0d-00e098032b8c"
> +            d = open(n, 'r', 0).read()
> +        except:
> +            return DEFAULT_LANG
> +
> +        try:
> +            # the contents of the file are:
> +            # 4-bytes of attribute data that we don't care about
> +            # NUL terminated ASCII string like 'en-US'.
> +            if len(d) < 10:
> +                log.debug("PlatformLang was too short")
> +                raise ValueError
> +            d = d[4:]
> +            if d[2] != '-':
> +                log.debug("PlatformLang was malformed")
> +                raise ValueError
> +
> +            # they use - and we use _, so fix it...
> +            d = d[:2] + '_' + d[3:-1]
> +
> +            # UEFI 2.3.1 Errata C specifies 2 aliases in common use that
> +            # aren't part of RFC 4646, but are allowed in PlatformLang.
> +            # Because why make anything simple?
> +            if d.startswith('zh_chs'):
> +                d = 'zh_Hans'
> +            elif d.startswith('zh_cht'):
> +                d = 'zh_Hant'
> +            d += '.UTF-8'
ACK to this, but I'm quite sure we don't support these two language
codes in a CLDR which are not valid glibc locales. However this should
be fixed later in a general way.

-- 
Vratislav Podzimek

Anaconda Rider | Red Hat, Inc. | Brno - Czech Republic



More information about the anaconda-patches mailing list