def invalid_langcodes_test(self):
        """Should return None for invalid langcodes."""

        # None
        parts = localization.parse_langcode(None)
        self.assertIsNone(parts)

        # nonsense
        parts = localization.parse_langcode("*_&!")
        self.assertIsNone(parts)

        # no language
        parts = localization.parse_langcode("_CZ")
        self.assertIsNone(parts)
Exemple #2
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
        parts = localization.parse_langcode(locale)
        if "language" not in parts:
            # invalid locale, cannot select
            return (None, None)

        lang_itr = set_treeview_selection(self._langView,
                                          parts["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)
    def parsing_test(self):
        """Should correctly parse valid langcodes."""

        parts = localization.parse_langcode("cs")
        self.assertIn("language", parts)
        self.assertEqual(parts["language"], "cs")

        parts = localization.parse_langcode("cs_CZ")
        self.assertIn("language", parts)
        self.assertIn("territory", parts)
        self.assertEqual(parts["language"], "cs")
        self.assertEqual(parts["territory"], "CZ")

        parts = localization.parse_langcode("cs_CZ.UTF-8")
        self.assertIn("language", parts)
        self.assertIn("territory", parts)
        self.assertIn("encoding", parts)
        self.assertEqual(parts["language"], "cs")
        self.assertEqual(parts["territory"], "CZ")
        self.assertEqual(parts["encoding"], "UTF-8")

        parts = localization.parse_langcode("cs_CZ.UTF-8@latin")
        self.assertIn("language", parts)
        self.assertIn("territory", parts)
        self.assertIn("encoding", parts)
        self.assertIn("script", parts)
        self.assertEqual(parts["language"], "cs")
        self.assertEqual(parts["territory"], "CZ")
        self.assertEqual(parts["encoding"], "UTF-8")
        self.assertEqual(parts["script"], "latin")

        parts = localization.parse_langcode("cs.UTF-8@latin")
        self.assertIn("language", parts)
        self.assertIn("encoding", parts)
        self.assertIn("script", parts)
        self.assertEqual(parts["language"], "cs")
        self.assertEqual(parts["encoding"], "UTF-8")
        self.assertEqual(parts["script"], "latin")

        parts = localization.parse_langcode("cs_CZ@latin")
        self.assertIn("language", parts)
        self.assertIn("territory", parts)
        self.assertIn("script", parts)
        self.assertEqual(parts["language"], "cs")
        self.assertEqual(parts["territory"], "CZ")
        self.assertEqual(parts["script"], "latin")
    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
        parts = localization.parse_langcode(locale)
        if "language" not in parts:
            # invalid locale, cannot select
            return (None, None)

        lang_itr = set_treeview_selection(self._langView, parts["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)
Exemple #5
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.proxy.Language
        if language and self._l12_module.proxy.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.parse_langcode(locale)['language']
            for locale in locales
        ]

        # check which of the geolocated 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.proxy,
                                      text_mode=False)
            lang_itr, _locale_itr = self._select_locale(
                self._l12_module.proxy.Language)
            langs_with_translations[DEFAULT_LANG] = lang_itr
            locales = [DEFAULT_LANG]

        # go over all geolocated 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.parse_langcode(l)['language'] != lang
                ]

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

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

        # report that we are done
        self.initialize_done()
Exemple #6
0
    def initialize(self):
        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.
        territory = geoloc.get_territory_code(wait=True)

        # bootopts and kickstart have priority over geoip
        if self.data.lang.lang and self.data.lang.seen:
            locales = [self.data.lang.lang]
        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.parse_langcode(locale)['language'] for locale in locales]

        # check which of the geolocated 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:
            localization.setup_locale(DEFAULT_LANG, self.data.lang)
            lang_itr, _locale_itr = self._select_locale(self.data.lang.lang)
            langs_with_translations[DEFAULT_LANG] = lang_itr
            locales = [DEFAULT_LANG]

        # go over all geolocated 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.parse_langcode(l)['language'] != lang]

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

        # setup the "best" locale
        localization.setup_locale(locales[0], self.data.lang)
        self._select_locale(self.data.lang.lang)
Exemple #7
0
    def initialize(self):
        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.
        territory = geoloc.get_territory_code(wait=True)

        # bootopts and kickstart have priority over geoip
        if self.data.lang.lang and self.data.lang.seen:
            locales = [self.data.lang.lang]
        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.parse_langcode(locale)['language'] for locale in locales]

        # check which of the geolocated 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.data.lang, text_mode=False)
            lang_itr, _locale_itr = self._select_locale(self.data.lang.lang)
            langs_with_translations[DEFAULT_LANG] = lang_itr
            locales = [DEFAULT_LANG]

        # go over all geolocated 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.parse_langcode(l)['language'] != lang]

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

        # setup the "best" locale
        locale = localization.setup_locale(locales[0], self.data.lang)
        self._set_lang(locale)
        self._select_locale(self.data.lang.lang)
Exemple #8
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.proxy.Language
        if language and self._l12_module.proxy.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.parse_langcode(locale)['language'] for locale in locales]

        # check which of the geolocated 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.proxy, text_mode=False)
            lang_itr, _locale_itr = self._select_locale(self._l12_module.proxy.Language)
            langs_with_translations[DEFAULT_LANG] = lang_itr
            locales = [DEFAULT_LANG]

        # go over all geolocated 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.parse_langcode(l)['language'] != lang]

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

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

        # report that we are done
        self.initialize_done()