예제 #1
0
파일: app.py 프로젝트: dachenlian/kontext
    def get_lang(self, environ):
        """
        Detects user's preferred language (either via the 'getlang' plugin or from HTTP_ACCEPT_LANGUAGE env value)

        arguments:
        environ -- WSGI environment variable

        returns:
        underscore-separated ISO 639 language code and ISO 3166 country code
        """
        cookies = KonTextCookie(environ.get('HTTP_COOKIE', ''))

        if plugins.runtime.GETLANG.exists:
            lgs_string = plugins.runtime.GETLANG.instance.fetch_current_language(
                cookies)
        else:
            lang_cookie = cookies.get('kontext_ui_lang')
            if not lang_cookie:
                lgs_string = parse_accept_header(
                    environ.get('HTTP_ACCEPT_LANGUAGE')).best
            else:
                lgs_string = lang_cookie.value
            if lgs_string is None:
                lgs_string = 'en_US'
            if len(lgs_string
                   ) == 2:  # in case we obtain just an ISO 639 language code
                lgs_string = self._installed_langs.get(lgs_string)
            else:
                lgs_string = lgs_string.replace('-', '_')
        if lgs_string is None:
            lgs_string = 'en_US'
        return lgs_string
예제 #2
0
파일: app.py 프로젝트: tomachalek/kontext
    def get_lang(self, environ):
        """
        Detects user's preferred language (either via the 'getlang' plugin or from HTTP_ACCEPT_LANGUAGE env value)

        arguments:
        environ -- WSGI environment variable

        returns:
        underscore-separated ISO 639 language code and ISO 3166 country code
        """
        cookies = KonTextCookie(environ.get('HTTP_COOKIE', ''))

        if plugins.runtime.GETLANG.exists:
            lgs_string = plugins.runtime.GETLANG.instance.fetch_current_language(cookies)
        else:
            lang_cookie = cookies.get('kontext_ui_lang')
            if not lang_cookie:
                lgs_string = parse_accept_header(environ.get('HTTP_ACCEPT_LANGUAGE')).best
            else:
                lgs_string = lang_cookie.value
            if len(lgs_string) == 2:  # in case we obtain just an ISO 639 language code
                lgs_string = self._installed_langs.get(lgs_string)
            else:
                lgs_string = lgs_string.replace('-', '_')
        if lgs_string is None:
            lgs_string = 'en_US'
        return lgs_string
예제 #3
0
파일: app.py 프로젝트: anukat2015/kontext
def get_lang(environ):
    """
    Detects user's preferred language (either via the 'getlang' plugin or from HTTP_ACCEPT_LANGUAGE env value)

    arguments:
    environ -- WSGI environment variable

    returns:
    underscore-separated ISO 639 language code and ISO 3166 country code
    """
    installed = dict([
        (x.split('_')[0], x)
        for x in os.listdir('%s/../locale' % os.path.dirname(__file__))
    ])

    if plugins.has_plugin('getlang'):
        lgs_string = plugins.get('getlang').fetch_current_language(
            KonTextCookie(environ.get('HTTP_COOKIE', '')))
    else:
        lgs_string = parse_accept_header(
            environ.get('HTTP_ACCEPT_LANGUAGE')).best
        if len(lgs_string
               ) == 2:  # in case we obtain just an ISO 639 language code
            lgs_string = installed.get(lgs_string)
        else:
            lgs_string = lgs_string.replace('-', '_')
    if lgs_string is None:
        lgs_string = 'en_US'
    return lgs_string
예제 #4
0
 def __init__(self, request, ui_lang):
     self.request = request
     self.environ = self.request.environ
     self.ui_lang = ui_lang
     self.cookies = KonTextCookie(self.environ.get('HTTP_COOKIE', ''))