Example #1
0
    def _select_locale(self, locale):
        """
        Try to select the given locale in the language and locale
        treeviews. This method tries to find the best match for the given
        locale.

        :return: a pair of selected iterators (language and locale)
        :rtype: a pair of GtkTreeIter or None objects

        """

        # get lang and select it
        language = localization.get_language_id(locale)
        if not language:
            # invalid locale, cannot select
            return (None, None)

        lang_itr = set_treeview_selection(self._langView, language, col=2)

        # find matches and use the one with the highest rank
        locales = localization.get_language_locales(locale)
        locale_itr = set_treeview_selection(self._localeView,
                                            locales[0],
                                            col=1)

        return (lang_itr, locale_itr)
Example #2
0
    def get_locale_data(self, locale_id):
        """Get data about the specified locale.

        :param: a locale id (for example, "en_US.UTF-8")
        :return: a locale data
        """
        tdata = LocaleData()
        tdata.english_name = get_english_name(locale_id)
        tdata.language_id = get_language_id(locale_id)
        tdata.locale_id = locale_id
        tdata.native_name = get_native_name(locale_id)

        return tdata
Example #3
0
    def initialize(self):
        self.initialize_start()
        self._languageStore = self.builder.get_object("languageStore")
        self._languageStoreFilter = self.builder.get_object(
            "languageStoreFilter")
        self._languageEntry = self.builder.get_object("languageEntry")
        self._langSelection = self.builder.get_object("languageViewSelection")
        self._langSelectedRenderer = self.builder.get_object(
            "langSelectedRenderer")
        self._langSelectedColumn = self.builder.get_object(
            "langSelectedColumn")
        self._langView = self.builder.get_object("languageView")
        self._localeView = self.builder.get_object("localeView")
        self._localeStore = self.builder.get_object("localeStore")
        self._localeSelection = self.builder.get_object("localeViewSelection")

        LangLocaleHandler.initialize(self)

        # We need to tell the view whether something is a separator or not.
        self._langView.set_row_separator_func(self._row_is_separator, None)

        # We can use the territory from geolocation here
        # to preselect the translation, when it's available.
        #
        # But as the lookup might still be in progress we need to make sure
        # to wait for it to finish. If the lookup has already finished
        # the wait function is basically a noop.
        geoloc.geoloc.wait_for_refresh_to_finish()

        # the lookup should be done now, get the teorritory
        territory = geoloc.geoloc.result.territory_code

        # bootopts and kickstart have priority over geoip
        language = self._l12_module.Language
        if language and self._l12_module.LanguageKickstarted:
            locales = [language]
        else:
            locales = localization.get_territory_locales(territory) or [
                DEFAULT_LANG
            ]

        # get the data models
        filter_store = self._languageStoreFilter
        store = filter_store.get_model()

        # get language codes for the locales
        langs = [localization.get_language_id(locale) for locale in locales]

        # get common languages and append them here
        common_langs = localization.get_common_languages()
        common_langs_len = len(set(common_langs).difference(set(langs)))
        langs += common_langs

        # check which of the geolocated or common languages have translations
        # and store the iterators for those languages in a dictionary
        langs_with_translations = {}
        itr = store.get_iter_first()
        while itr:
            row_lang = store[itr][2]
            if row_lang in langs:
                langs_with_translations[row_lang] = itr
            itr = store.iter_next(itr)

        # if there are no translations for the given locales,
        # use default
        if not langs_with_translations:
            self._set_lang(DEFAULT_LANG)
            localization.setup_locale(DEFAULT_LANG,
                                      self._l12_module,
                                      text_mode=False)
            lang_itr, _locale_itr = self._select_locale(
                self._l12_module.Language)
            langs_with_translations[DEFAULT_LANG] = lang_itr
            locales = [DEFAULT_LANG]

        # go over all geolocated and common languages in reverse order
        # and move those we have translation for to the top of the
        # list, above the separator
        for lang in reversed(langs):
            itr = langs_with_translations.get(lang)
            if itr:
                store.move_after(itr, None)
            else:
                # we don't have translation for this language,
                # so dump all locales for it
                locales = [
                    l for l in locales
                    if localization.get_language_id(l) != lang
                ]

        # And then we add a separator after the selected best language
        # and any additional languages (that have translations) from geoip
        langs_with_translations_len = len(langs_with_translations)
        newItr = store.insert(langs_with_translations_len)
        store.set(newItr, 0, "", 1, "", 2, "", 3, True)

        # add a separator before common languages as well
        commonLangsItr = store.insert(langs_with_translations_len -
                                      common_langs_len)
        store.set(commonLangsItr, 0, "", 1, "", 2, "", 3, True)

        # setup the "best" locale
        locale = localization.setup_locale(locales[0], self._l12_module)
        self._set_lang(locale)
        self._select_locale(self._l12_module.Language)

        # report that we are done
        self.initialize_done()