Beispiel #1
0
    def getPreferredLanguages(self):
        languages_data = self._getLanguagesData()
        if "overridden" in languages_data:
            # if the language has been overridden by, say, ++lang++

            return languages_data["overridden"]

        elif "cached" not in languages_data:

            # actually do our calculation here; we only cache the result
            # if we're using one of the query string parameters
            path_pieces = self.request['PATH_INFO'].split('/')

            # see if we're dealing with a BrwoserRequest
            if IBrowserApplicationRequest in providedBy(self.request):
                # this request has a form available; look for query string, etc

                # XXX the form may not be processed yet, so lets double-check
                if self.request.form == {} and \
                   'licenses' in path_pieces and \
                   self.request.get('QUERY_STRING', '').find('lang=') > -1:

                    self.request.processInputs()

                if self.request.form.get(u'lang', False):
                    # check for the query string

                    languages_data["cached"] = [
                        normalize_lang(self.request['lang']), u'en']

                elif len(path_pieces) >= 6 and 'licenses' in path_pieces:
                    # /licenses doesn't do content negotiation;
                    # this request specifies a jurisdiction

                    languages_data["cached"] = [
                        normalize_lang(n) for n in
                        cc.license.support.default_locales(path_pieces[-2])] + \
                        [u'en']

                elif self.request.form.get('language', False):
                    languages_data["cached"] = [
                        normalize_lang(self.request['language']), u'en']

                    # XXX look for the cookie
                else:
                    # fall back to default selection (HTTP_ACCEPT_LANGUAGE)
                    return super(
                        PreferredLanguages, self).getPreferredLanguages()

            else:
                # fall back to default selection (HTTP_ACCEPT_LANGUAGE)
                return super(
                    PreferredLanguages, self).getPreferredLanguages()

        return languages_data["cached"]
Beispiel #2
0
    def update(self):
        def css_class(language):
            css_attr = None
            if language == selected:
                css_attr = "selected"
            if language in translations:
                if css_attr:
                    css_attr = css_attr + " available"
                else:
                    css_attr = "available"
            return css_attr

        def language_name(language):
            return language.get("native", language.get("name"))

        translations = get_available_translations(self.context)
        if hasattr(self.context, "language"):
            translations[self.context.language] = None
        languages = get_all_languages()
        selected = normalize_lang(self.request.locale.getLocaleID())
        url = ui_utils.url.absoluteURL(getSite(), self.request)

        self.languages = sorted(
            [{
                "code": language,
                "flag": url + languages[language].get('flag', ''),
                "name": language_name(languages[language]),
                "css_class": css_class(language),
                "url": "%s/change-language?language=%s" % (url, language),
            } for language in languages],
            key=lambda l: l.get("code"))
Beispiel #3
0
    def update(self):
        
        def css_class(language):
            css_attr = None
            if language == selected:
                css_attr = 'selected'
            if language in translations:
                if css_attr:
                    css_attr = css_attr + ' available'
                else:
                    css_attr = 'available'
            return css_attr
        
        def language_name(language): 
            langname = language.get('native', None)
            if langname == None:
                langname = language.get('name')
            return langname

        translations = get_available_translations(self.context)
        if hasattr(self.context,'language'):
            translations[self.context.language] = None
        languages = get_all_languages()
        selected = normalize_lang(self.request.locale.getLocaleID())
        url = ui_utils.url.absoluteURL(getSite(), self.request)
        
        self.languages = [{
            'code': language,
            'flag': url+languages[language].get('flag', ''),
            'name': language_name(languages[language]),
            'css_class': css_class(language),
            'url': "%s/change-language?language=%s" % (url, language),
            } for language in languages]
    def update(self):
        def css_class(language):
            css_attr = None
            if language == selected:
                css_attr = "selected"
            if language in translations:
                if css_attr:
                    css_attr = css_attr + " available"
                else:
                    css_attr = "available"
            return css_attr

        def language_name(language):
            return language.get("native", language.get("name"))

        translations = get_available_translations(self.context)
        if hasattr(self.context, "language"):
            translations[self.context.language] = None
        languages = get_all_languages()
        selected = normalize_lang(self.request.locale.getLocaleID())
        url = ui_utils.url.absoluteURL(getSite(), self.request)

        self.languages = sorted(
            [
                {
                    "code": language,
                    "flag": url + languages[language].get("flag", ""),
                    "name": language_name(languages[language]),
                    "css_class": css_class(language),
                    "url": "%s/change-language?language=%s" % (url, language),
                }
                for language in languages
            ],
            key=lambda l: l.get("code"),
        )
 def test_getAvailableLanguages(self, mock_queryUtility):
     mock_translation_domain = Mock()
     langs_dict = {}
     for l in self.AVAILABLE_LANGS + ['test']:
         langs_dict[l] = l
     mock_translation_domain.getCatalogsInfo.return_value = langs_dict
     mock_queryUtility.return_value = mock_translation_domain
     langs = self.negotiator.getAvailableLanguages()
     self.assertEqual(langs, dict(zip(
         [normalize_lang(l) for l in self.AVAILABLE_LANGS],
         self.AVAILABLE_LANGS_NAME)))
Beispiel #6
0
    def getPreferredLanguages(self):
        if not hasattr(self.context, 'AcceptLanguage'):
            return []

        accept_language = self.context.AcceptLanguage
        langs = []
        for lang, node in accept_language.children.items():
            # Localizer may use xx_YY and xx-YY as language codes,
            # while Zope expect xx-yy only, so we normalize the code here.
            langs.append((node.get_quality(), normalize_lang(lang)))
            langs.extend([(n.get_quality(), l)
                          for l, n in node.children.items()])
        langs.sort()
        langs.reverse()
        langs = [l for q, l in langs]
        if '' in langs:
            langs.remove('')
        return langs
Beispiel #7
0
 def getPreferredLanguages(self):
     if not hasattr(self.context, 'AcceptLanguage'):
         return []
         
     accept_language = self.context.AcceptLanguage
     langs = []
     for lang, node in accept_language.children.items():
         # Localizer may use xx_YY and xx-YY as language codes,
         # while Zope expect xx-yy only, so we normalize the code here.
         langs.append((node.get_quality(), normalize_lang(lang)))
         langs.extend([(n.get_quality(), l) for l, n
                       in node.children.items()])
     langs.sort()
     langs.reverse()
     langs = [l for q, l in langs]
     if '' in langs:
         langs.remove('')
     return langs
Beispiel #8
0
 def __call__(self):
     return normalize_lang(self.getLanguage())
 def __call__(self):
     return normalize_lang(self.getLanguage())
Beispiel #10
0
 def __call__(self):
     language = self.getLanguage()
     return normalize_lang(language) if language else None
Beispiel #11
0
 def __call__(self):
     language = self.getLanguage()
     return normalize_lang(language) if language else None
Beispiel #12
0
 def getCookieLanguage(self, request):
     lang = request.cookies.get('reportek_language', None)
     return normalize_lang(lang) if lang else None