Skip to content

Commit

Permalink
Call the i18n Middleware first before asking the apps
Browse files Browse the repository at this point in the history
The i18n will remove the locale part of the environ['PATH_INFO']
(eg `fr` in `/fr/dataset`) and set the necessary lang vars in the
environ. We need to remove that before asking the apps if they can
handle a request, otherwise localized URLs won't get recognized.
  • Loading branch information
amercader committed Jun 7, 2016
1 parent 9f07da4 commit c2aa9c0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
8 changes: 8 additions & 0 deletions ckan/config/middleware/__init__.py
Expand Up @@ -10,6 +10,8 @@
from ckan.config.middleware.flask_app import make_flask_stack
from ckan.config.middleware.pylons_app import make_pylons_stack

from ckan.config.middleware.common_middleware import I18nMiddleware

import logging
log = logging.getLogger(__name__)

Expand Down Expand Up @@ -85,6 +87,8 @@ def __init__(self, apps=None, invites=(), ignore_missing_services=False):

self.send_invitations(apps)

self.i18n_middleware = I18nMiddleware()

def send_invitations(self, apps):
'''Call each app at the invite route to establish a partyline. Called
on init.'''
Expand All @@ -102,6 +106,10 @@ def __call__(self, environ, start_response):
url and method defined on the eviron'''
# :::TODO::: Enforce order of precedence for dispatching to apps here.

# Handle the i18n first, otherwise localized URLs (eg `/jp/about`)
# won't get recognized by the app route mappers
self.i18n_middleware(environ, start_response)

app_name = 'pylons_app' # currently defaulting to pylons app
answers = self.ask_around('can_handle_request', environ)
log.debug('Route support answers for {0} {1}: {2}'.format(
Expand Down
7 changes: 3 additions & 4 deletions ckan/config/middleware/common_middleware.py
Expand Up @@ -7,6 +7,8 @@
import urllib
import json

from pylons import config

import sqlalchemy as sa

from ckan.lib.i18n import get_locales_from_config
Expand All @@ -15,8 +17,7 @@
class I18nMiddleware(object):
"""I18n Middleware selects the language based on the url
eg /fr/home is French"""
def __init__(self, app, config):
self.app = app
def __init__(self):
self.default_locale = config.get('ckan.locale_default', 'en')
self.local_list = get_locales_from_config()

Expand Down Expand Up @@ -58,8 +59,6 @@ def __call__(self, environ, start_response):
else:
environ['CKAN_CURRENT_URL'] = path_info

return self.app(environ, start_response)


class RootPathMiddleware(object):
'''
Expand Down
2 changes: 0 additions & 2 deletions ckan/config/middleware/flask_app.py
Expand Up @@ -142,8 +142,6 @@ def hello_world_post():

# Start other middleware

app = common_middleware.I18nMiddleware(app, config)

# Initialize repoze.who
who_parser = WhoConfig(conf['here'])
who_parser.parse(open(app_conf['who.config_file']))
Expand Down
2 changes: 0 additions & 2 deletions ckan/config/middleware/pylons_app.py
Expand Up @@ -133,8 +133,6 @@ def make_pylons_stack(conf, full_stack=True, static_files=True, **app_conf):
# Establish the Registry for this application
app = RegistryManager(app)

app = common_middleware.I18nMiddleware(app, config)

if asbool(static_files):
# Serve static files
static_max_age = None if not asbool(config.get('ckan.cache_enabled')) \
Expand Down

0 comments on commit c2aa9c0

Please sign in to comment.