Esempio n. 1
0
def set_locale(cls, force=None):
    """
    retrieve locale from a prioritized list of sources and then set locale and save it
    cls: self object
    force: a locale to force set (ie 'en_US')
    return: locale as string or None if i18n should be disabled
    """
    # disable i18n if config.locales array is empty or None
    if not config.locales:
        return None
    # 1. force locale if provided
    locale = force
    if locale not in config.locales:
        # 2. retrieve locale from url query string
        locale = cls.request.get("hl", None)
        if locale not in config.locales:
            # 3. retrieve locale from cookie
            locale = cls.request.cookies.get('hl', None)
            if locale not in config.locales:
                # 4. retrieve locale from accept language header
                locale = get_locale_from_accept_header(cls.request)
                if locale not in config.locales:
                    # 5. detect locale from IP address location
                    territory = get_territory_from_ip(cls) or 'ZZ'
                    locale = str(Locale.negotiate(territory, config.locales))
                    if locale not in config.locales:
                        # 6. use default locale
                        locale = i18n.get_store().default_locale
    i18n.get_i18n().set_locale(locale)
    # save locale in cookie with 26 weeks expiration (in seconds)
    cls.response.set_cookie('hl', locale, max_age = 15724800)
    return locale
Esempio n. 2
0
def set_locale(cls, force=None):
    """
    retrieve locale from a prioritized list of sources and then set locale and save it
    cls: self object
    force: a locale to force set (ie 'en_US')
    return: locale as string or None if i18n should be disabled
    """
    # disable i18n if config.locales array is empty or None
    if not config.locales:
        return None
    # 1. force locale if provided
    locale = force
    if locale not in config.locales:
        # 2. retrieve locale from url query string
        locale = cls.request.get("hl", None)
        if locale not in config.locales:
            # 3. retrieve locale from cookie
            locale = cls.request.cookies.get('hl', None)
            if locale not in config.locales:
                # 4. retrieve locale from accept language header
                locale = get_locale_from_accept_header(cls.request)
                if locale not in config.locales:
                    # 5. detect locale from IP address location
                    territory = get_territory_from_ip(cls) or 'ZZ'
                    locale = str(Locale.negotiate(territory, config.locales))
                    if locale not in config.locales:
                        # 6. use default locale
                        locale = i18n.get_store().default_locale
    i18n.get_i18n().set_locale(locale)
    # save locale in cookie with 26 weeks expiration (in seconds)
    cls.response.set_cookie('hl', locale, max_age = 15724800)
    return locale
 def test_get_i18n_store(self):
     app = webapp2.WSGIApplication(debug=True)
     req = webapp2.Request.blank('/')
     req.app = app
     self.assertEqual(len(app.registry), 0)
     s = i18n.get_store(app=app)
     self.assertEqual(len(app.registry), 1)
     self.assertTrue(isinstance(s, i18n.I18nStore))
Esempio n. 4
0
 def test_get_i18n_store(self):
     app = webapp2.WSGIApplication()
     req = webapp2.Request.blank('/')
     req.app = app
     self.assertEqual(len(app.registry), 0)
     s = i18n.get_store(app=app)
     self.assertEqual(len(app.registry), 1)
     self.assertTrue(isinstance(s, i18n.I18nStore))
Esempio n. 5
0
def _set_locale(request, force=None):
    """
    retrieve locale from a prioritized list of sources and then set locale and save it
    cls: self object
    force: a locale to force set (ie 'en_US')
    return: locale as string or None if i18n should be disabled
    """
    try:
        webapp2_instance = webapp2.get_app()
    except AssertionError:
        logging.debug('No webapp2 global set; skipping registry lookup for jinja2 template engine.')
        locales = []
    else:
        locales = webapp2_instance.config.get(I18N_LOCALES_KEY) or []

    # disable i18n if config.locales array is empty or None
    if not locales:
        return None
    # 1. force locale if provided
    locale = force
    if locale not in locales:
        # 2. retrieve locale from url query string
        locale = request.get("hl", None)
        if locale not in locales:
            # 3. retrieve locale from user preferences
            user_session_info = request.registry['user_session_info']
            if user_session_info:
                user_id = user_session_info.get('user_id', None)
                if user_id:
                    stored_locale = request.session.get('inferred_locale', None)
                    if not stored_locale:
                        lower = str(request.registry['user_session_info'].get('language_preference', None))
                        upper = str(lower).upper()
                        stored_locale = '{0}_{1}'.format(lower, upper)
                        request.session['inferred_locale'] = stored_locale
                    locale = stored_locale
                else:
                    locale = None
            if locale not in locales:
                # 4. retrieve locale from accept language header
                locale = get_locale_from_accept_header(request)
                if locale not in locales:
                    # 5. detect locale from IP address location
                    territory = get_country_code(request) or 'ZZ'
                    locale = str(Locale.negotiate(territory, locales))
                    if locale not in locales:
                        # 6. use default locale
                        locale = i18n.get_store().default_locale
    i18n.get_i18n(request=request).set_locale(locale)
    request.registry['locale'] = locale
 def test_set_timezone_selector(self):
     i18n.get_store().set_timezone_selector(
         'resources.i18n.timezone_selector')
 def test_set_locale_selector(self):
     i18n.get_store().set_locale_selector('resources.i18n.locale_selector')
Esempio n. 8
0
def timezone_selector(store, request):
    return i18n.get_store().default_timezone
Esempio n. 9
0
def locale_selector(store, request):
    return i18n.get_store().default_locale
Esempio n. 10
0
 def test_set_timezone_selector(self):
     i18n.get_store().set_timezone_selector(
         'tests.resources.i18n.timezone_selector')
Esempio n. 11
0
 def test_set_locale_selector(self):
     i18n.get_store().set_locale_selector(
         'tests.resources.i18n.locale_selector')
Esempio n. 12
0
def locale_selector(store, request):
    return i18n.get_store().default_locale
Esempio n. 13
0
def timezone_selector(store, request):
    return i18n.get_store().default_timezone