Exemple #1
0
    def on_language_changed(self, locale_code: str):
        """Translate to the given locale"""
        for setting in self.settings.region_settings:
            setting.on_language_changed(locale_code)

        self.translate_to(locale_code)
        self.mainwindow.current_language = localization.language_from_locale(
            locale_code)
Exemple #2
0
    def on_language_changed(self, locale: str):
        """Set the keyboard layout according to the new language"""

        # Don't overwrite a user chosen value
        if self.value_changed_by_user:
            return

        language = language_from_locale(locale)
        country = country_from_locale(locale)

        # First, build a list of layouts to consider for the language
        language_layouts = self._layouts_for_language(language)
        logging.debug("Language %s layouts: %s", language, language_layouts)
        country_layouts = self._layouts_for_country(country)
        logging.debug("Country %s layouts: %s", country, country_layouts)
        layouts = set(language_layouts).intersection(country_layouts)
        logging.debug("Intersection of language %s and country %s: %s",
                      language, country, layouts)

        if not layouts:
            layouts = set(l for l in language_layouts if self._split_variant(l)[0] == country.lower)
            logging.debug("Empty intersection of language and country, filter "
                          "by country %s only: %s", country, layouts)
        if not layouts:
            layouts = set(l for l in language_layouts if self._split_variant(l)[0] == language)
            logging.debug("List still empty, filter by language %s only: %s",
                          language, layouts)
        if not layouts:
            layouts = language_layouts
            logging.debug("List still empty, use all language %s layouts: %s",
                          language, layouts)

        # Then, filter the list
        layouts = self._filter_layouts(layouts, country, language)
        if len(layouts) != 1:
            # Can't find a single result, build a new list for the country
            layouts = country_layouts
            logging.debug("Still not 1 layouts. Try again using all country "
                          "%s layouts: %s", country, layouts)
            layouts = self._filter_layouts(layouts, country, language)
        if len(layouts) == 1:
            default_layout = layouts.pop()
            logging.debug("Selecting single matching layout %s",
                          default_layout)
        elif len(layouts) > 1:
            default_layout = layouts.pop()
            logging.debug("No good layout, arbitrary using layout %s",
                          default_layout)
        else:
            default_layout = 'us'
            logging.debug("Using us as fallback default layout")
        self.set_value(default_layout)
Exemple #3
0
    def _language_from_locale(self, locale_code: str) -> str:
        lang_code = language_from_locale(locale_code)
        if lang_code != "zh":
            return lang_code

        # We treat Chinese differently to have simplified and traditional
        # Chinese in separate groups
        if locale_code in ("zh_CN", "zh_SG"):
            # Our own "language code" for simplified Chinese
            return "zhs"
        if locale_code in ("zh_HK", "zh_TW"):
            # Our own "language code" for traditional Chinese
            return "zht"
        return lang_code
Exemple #4
0
    def get_default_locale(self, country_code=None) -> str:
        """Return default locale for given country

        Returns the 1st locale among:
            - the locale whose country name matches country name
            - the 1st locale for the language
            - en_US
        """
        locales = self.locales_per_country[country_code]
        if not locales:
            return 'en_US'

        # Get the default locale for the country
        for locale_code in locales:
            if country_from_locale(locale_code).lower() == language_from_locale(locale_code):
                return locale_code

        return locales[0]
Exemple #5
0
 def _locale_name(locale_code) -> str:
     lang_code = language_from_locale(locale_code)
     country_code = country_from_locale(locale_code)
     language_name_locale = GnomeDesktop.get_language_from_code(lang_code)
     language_name_native = GnomeDesktop.get_language_from_code(
             lang_code, locale_code)
     country_name_locale = GnomeDesktop.get_country_from_code(country_code)
     country_name_native = GnomeDesktop.get_country_from_code(
             country_code, locale_code)
     try:
         if (language_name_native == language_name_locale and
                 country_name_native == country_name_locale):
             return "{country} - {language}".format(
                     language=language_name_native.capitalize(),
                     country=country_name_native)
         else:
             return "{country} - {language} " \
                    "({local_country} - {local_language})".format(
                     language=language_name_native.capitalize(),
                     country=country_name_native,
                     local_language=language_name_locale.capitalize(),
                     local_country=country_name_locale)
     except AttributeError:
         return locale_code
Exemple #6
0
 def on_language_changed(self, locale_code: str):
     """Translate to the given locale"""
     self.localisationsettings.formats.on_language_changed(locale_code)  # XXX: notify
     self.localisationsettings.keyboard.on_language_changed(locale_code)  # XXX: notify
     self.translate_to(locale_code)
     self.mainwindow.current_language = localization.language_from_locale(locale_code)