def handle_i18n(environ=None): u''' Strips the locale code from the requested url (eg '/sk/about' -> '/about') and sets environ variables for the language selected: * CKAN_LANG is the language code eg en, fr * CKAN_LANG_IS_DEFAULT is set to True or False * CKAN_CURRENT_URL is set to the current application url ''' environ = environ or request.environ locale_list = get_locales_from_config() default_locale = config.get(u'ckan.locale_default', u'en') # We only update once for a request so we can keep # the language and original url which helps with 404 pages etc if u'CKAN_LANG' not in environ: path_parts = environ[u'PATH_INFO'].split(u'/') if len(path_parts) > 1 and path_parts[1] in locale_list: environ[u'CKAN_LANG'] = path_parts[1] environ[u'CKAN_LANG_IS_DEFAULT'] = False # rewrite url if len(path_parts) > 2: environ[u'PATH_INFO'] = u'/'.join([u''] + path_parts[2:]) else: environ[u'PATH_INFO'] = u'/' else: environ[u'CKAN_LANG'] = default_locale environ[u'CKAN_LANG_IS_DEFAULT'] = True set_ckan_current_url(environ)
def i18n_js_translations(lang, ver=API_REST_DEFAULT_VERSION): if lang not in get_locales_from_config(): return _finish_bad_request('Unknown locale: {}'.format(lang)) ckan_path = os.path.join(os.path.dirname(__file__), u'..') source = os.path.abspath(os.path.join(ckan_path, u'public', u'base', u'i18n', u'{0}.js'.format(lang))) if not os.path.exists(source): return u'{}' translations = json.load(io.open(source, u'r', encoding='utf-8')) return _finish_ok(translations)
def __init__(self, app, config): self.app = app self.default_locale = config.get('ckan.locale_default', 'en') self.local_list = get_locales_from_config()
def __init__(self, apps=None): # Dict of apps managed by this middleware {<app_name>: <app_obj>, ...} self.apps = apps or {} self.default_locale = config.get('ckan.locale_default', 'en') self.locale_list = get_locales_from_config()