示例#1
0
    def getLanguage(self, available, request, fallback=True):
        """
        Returns the best matching language based on `available` languages,
        preferred languages in `request` and negotiation policies.

        If `fallback` is True (default), return first available
        language on failure.
        If `fallback` is False, return None on failure, let third party app
        choose an app-dependant default (e.g. get_default_language() in portal)

        """
        available = map(normalize_code, available)
        # here we keep {'xx': 'xx-zz'} for xx-zz, for fallback cases
        secondary = {}
        for x in [av for av in available if av.find("-") > -1]:
            secondary[x.split("-", 1)[0]] = x

        accept_langs = request.get('HTTP_ACCEPT_LANGUAGE', '')
        if accept_langs in (None, ''):
            accept_langs = []
        else:
            accept_langs = accept_langs.split(';', 1)[0].split(',')

        cookie = request.cookies.get(self.cookie_id, '')
        url = request.form.get(self.cookie_id, '')
        path = request.get(self.cookie_id, '')

        client_langs = {'browser': map(normalize_code, accept_langs),
                        'url': normalize_code(url),
                        'path': normalize_code(path),
                        'cookie': normalize_code(cookie)}

        # compute place in cache and check cache
        key = self._get_cache_key(available, client_langs)
        cached_value = self._query_cache(key, request)
        # 2nd assertion: one last check, just to make sure it's valid
        if cached_value is not None and cached_value in available:
            return cached_value

        for policy in self.policy:
            langs = client_langs[policy]
            if not isinstance(langs, list):
                langs = [langs, ]
            for lang in langs:
                if lang in available:
                    self._update_cache(key, lang, request)
                    return lang
                elif lang.find("-") > -1:
                    first_code = lang.split("-", 1)[0]
                    # if xx-yy not found, but xx is available, return xx
                    if first_code in available:
                        return first_code
                    # if xx-yy not found, but xx-zz is available, return xx-zz
                    elif first_code in secondary.keys():
                        return secondary[first_code]
        if fallback:
            return available[0]
        else:
            return None
示例#2
0
    def getLanguage(self, available, request, fallback=True):
        """
        Returns the best matching language based on `available` languages,
        preferred languages in `request` and negotiation policies.

        If `fallback` is True (default), return first available
        language on failure.
        If `fallback` is False, return None on failure, let third party app
        choose an app-dependant default (e.g. get_default_language() in portal)

        """
        available = map(normalize_code, available)
        # here we keep {'xx': 'xx-zz'} for xx-zz, for fallback cases
        secondary = {}
        for x in [av for av in available if av.find("-") > -1]:
            secondary[x.split("-", 1)[0]] = x

        accept_langs = request.get('HTTP_ACCEPT_LANGUAGE', '')
        if accept_langs in (None, ''):
            accept_langs = []
        else:
            accept_langs = accept_langs.split(';', 1)[0].split(',')

        cookie = request.cookies.get(self.cookie_id, '')
        url = request.form.get(self.cookie_id, '')
        path = request.get(self.cookie_id, '')

        client_langs = {'browser': map(normalize_code, accept_langs),
                        'url': normalize_code(url),
                        'path': normalize_code(path),
                        'cookie': normalize_code(cookie)}

        # compute place in cache and check cache
        key = self._get_cache_key(available, client_langs)
        cached_value = self._query_cache(key, request)
        # 2nd assertion: one last check, just to make sure it's valid
        if cached_value is not None and cached_value in available:
            return cached_value

        for policy in self.policy:
            langs = client_langs[policy]
            if not isinstance(langs, list):
                langs = [langs, ]
            for lang in langs:
                if lang in available:
                    self._update_cache(key, lang, request)
                    return lang
                elif lang.find("-") > -1:
                    first_code = lang.split("-", 1)[0]
                    # if xx-yy not found, but xx is available, return xx
                    if first_code in available:
                        return first_code
                    # if xx-yy not found, but xx-zz is available, return xx-zz
                    elif first_code in secondary.keys():
                        return secondary[first_code]
        if fallback and available:
            return available[0]
        else:
            return None
    def getLanguage(self, available, request=None, fallback=True):
        """
        Returns the language dependent on the policy.

        If `fallback` is True (default), return first av. language on failure.
        If `fallback` is False, return None on failure, let third party app
        choose an app-dependant default (eg get_default_language() in portal)
        """
        if request is None:
            if self.request is not None:
                request = self.request
            else:
                raise ValueError("No request to manage negotiation")
        available = map(normalize_code, available)
        # here we keep {'xx': 'xx-zz'} for xx-zz, for fallback cases
        secondary = {}
        for x in [av for av in available if av.find("-") > -1]:
            secondary[x.split("-", 1)[0]] = x

        AcceptLanguage = request.get('HTTP_ACCEPT_LANGUAGE', '')
        if AcceptLanguage is None:
            AcceptLanguage = ''

        cookie = request.cookies.get(self.cookie_id, '')
        url = request.form.get(self.cookie_id, '')
        path = request.get(self.cookie_id, '')

        client_langs = {
            'browser': normalize_code(AcceptLanguage),
            'url': normalize_code(url),
            'path': normalize_code(path),
            'cookie': normalize_code(cookie)
        }

        # compute place in cache and check cache
        key = self._get_cache_key(available, client_langs)
        cached_value = self._query_cache(key, request)
        # 2nd assertion: one last check, just to make sure it's valid
        if cached_value is not None and cached_value in available:
            return cached_value

        for policy in self.policy:
            lang = client_langs[policy]
            if lang in available:
                self._update_cache(key, lang, request)
                return lang
            elif lang.find("-") > -1:
                first_code = lang.split("-", 1)[0]
                # if xx-yy not found, but xx is available, return xx
                if first_code in available:
                    return first_code
                # if xx-yy not found, but xx-zz is available, return xx-zz
                elif first_code in secondary.keys():
                    return secondary[first_code]
        if fallback:
            return available[0]
        else:
            return None
    def getLanguage(self, available, request=None, fallback=True):
        """
        Returns the language dependent on the policy.

        If `fallback` is True (default), return first av. language on failure.
        If `fallback` is False, return None on failure, let third party app
        choose an app-dependant default (eg get_default_language() in portal)
        """
        if request is None:
            if self.request is not None:
                request = self.request
            else:
                raise ValueError("No request to manage negotiation")
        available = map(normalize_code, available)
        # here we keep {'xx': 'xx-zz'} for xx-zz, for fallback cases
        secondary = {}
        for x in [av for av in available if av.find("-") > -1]:
            secondary[x.split("-", 1)[0]] = x

        AcceptLanguage = request.get('HTTP_ACCEPT_LANGUAGE', '')
        if AcceptLanguage is None:
            AcceptLanguage = ''

        cookie = request.cookies.get(self.cookie_id, '')
        url = request.form.get(self.cookie_id, '')
        path = request.get(self.cookie_id, '')

        client_langs = {'browser': normalize_code(AcceptLanguage),
                        'url': normalize_code(url),
                        'path': normalize_code(path),
                        'cookie': normalize_code(cookie)}

        # compute place in cache and check cache
        key = self._get_cache_key(available, client_langs)
        cached_value = self._query_cache(key, request)
        # 2nd assertion: one last check, just to make sure it's valid
        if cached_value is not None and cached_value in available:
            return cached_value

        for policy in self.policy:
            lang = client_langs[policy]
            if lang in available:
                self._update_cache(key, lang, request)
                return lang
            elif lang.find("-") > -1:
                first_code = lang.split("-", 1)[0]
                # if xx-yy not found, but xx is available, return xx
                if first_code in available:
                    return first_code
                # if xx-yy not found, but xx-zz is available, return xx-zz
                elif first_code in secondary.keys():
                    return secondary[first_code]
        if fallback:
            return available[0]
        else:
            return None
 def __init__(self, id, title, languages=[('en', 'English')]):
     self.id = id
     self.title = title
     n_languages = []
     for (code, name) in languages:
         n_languages.append((normalize_code(code), name))
     self._portal_langs = NyPortalLanguageManager(n_languages)
     lang_codes = tuple([x[0] for x in n_languages])
     catalog = NyMessageCatalog('translation_catalog', 'Translation Catalog',
                                 lang_codes)
     self._catalog = catalog
 def __init__(self, id, title, languages=[('en', 'English')]):
     self.id = id
     self.title = title
     n_languages = []
     for (code, name) in languages:
         n_languages.append((normalize_code(code), name))
     self._portal_langs = NyPortalLanguageManager(n_languages)
     lang_codes = tuple([x[0] for x in n_languages])
     catalog = NyMessageCatalog('translation_catalog',
                                'Translation Catalog', lang_codes)
     self._catalog = catalog
    def add_language(self, lang_code, lang_name=None):
        """
        Adds a new supported language in portal_i18n.
        Language code, language name can be any combination.
        If language name is not provided,
        a lookup is being performed in naaya.i18n/languages.txt

        """
        if not lang_code:
            raise ValueError('No language code provided')
        lang_code = normalize_code(lang_code)
        if lang_name is None:
            # search for name directly in languages.txt, obviously not in site
            lang_name = self.get_lang_manager().get_language_name(lang_code)
        # add language to portal:
        self._portal_langs.addAvailableLanguage(lang_code, lang_name)
        # and to catalog:
        self._catalog.add_language(lang_code)
    def add_language(self, lang_code, lang_name=None):
        """
        Adds a new supported language in portal_i18n.
        Language code, language name can be any combination.
        If language name is not provided,
        a lookup is being performed in naaya.i18n/languages.txt

        """
        if not lang_code:
            raise ValueError('No language code provided')
        lang_code = normalize_code(lang_code)
        if lang_name is None:
            # search for name directly in languages.txt, obviously not in site
            lang_name = self.get_lang_manager().get_language_name(lang_code)
        # add language to portal:
        self._portal_langs.addAvailableLanguage(lang_code, lang_name)
        # and to catalog:
        self._catalog.add_language(lang_code)