def get_user_locale(self):
     """
     This locale handler is a bit old fashioned and picks up locale from
     the argument in the URL
     """
     _locale = locale.get(self.get_argument('locale', 'en_US'))
     return _locale
 def get_user_locale(self):
     # locale.get will use the first non-empty argument that matches a
     # supported language.
     return locale.get(
         self.get_argument('lang', None),
         self.get_django_session().get('django_language', None),
         self.get_cookie('django_language', None))
    def get_browser_locale(self, default="en_US"):
        """Determines the user's locale from Accept-Language header.

        See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
        """
        if "Accept-Language" in self.request.headers:
            languages = self.request.headers["Accept-Language"].split(",")
            locales = []
            for language in languages:
                parts = language.strip().split(";")
                if len(parts) > 1 and parts[1].startswith("q="):
                    try:
                        score = float(parts[1][2:])
                    except (ValueError, TypeError):
                        score = 0.0
                else:
                    score = 1.0
                locales.append((parts[0], score))
            if locales:
                locales.sort(key=lambda l: l[1], reverse=True)
                codes = [l[0] for l in locales]
                return locale.get(*codes)
        return locale.get(default)
Beispiel #4
0
    def get_browser_locale(self, default="en_US"):
        """Determines the user's locale from Accept-Language header.

        See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
        """
        if "Accept-Language" in self.request.headers:
            languages = self.request.headers["Accept-Language"].split(",")
            locales = []
            for language in languages:
                parts = language.strip().split(";")
                if len(parts) > 1 and parts[1].startswith("q="):
                    try:
                        score = float(parts[1][2:])
                    except (ValueError, TypeError):
                        score = 0.0
                else:
                    score = 1.0
                locales.append((parts[0], score))
            if locales:
                locales.sort(key=lambda (l, s): s, reverse=True)
                codes = [l[0] for l in locales]
                return locale.get(*codes)
        return locale.get(default)
    def test_0010_locales(self):
        self.assertTrue("es_ES" in locale._supported_locales)
        self.assertTrue("fr_FR" in locale._supported_locales)

        es_ES = locale.get('es_ES')
        self.assertEqual(es_ES.translate('Welcome'), "bienvenido")
Beispiel #6
0
    def get_user_locale(self):
        if self.current_user:
            return locale.get(self.current_user.locale)

        # Fallback to browser based locale detection
        return self.get_browser_locale()