-
Notifications
You must be signed in to change notification settings - Fork 152
Allow i18n of colander error messages #206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| # License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
| # You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| import logging | ||
| from functools import partial | ||
|
|
||
| from cornice import util | ||
| from cornice.errors import Errors # NOQA | ||
|
|
@@ -14,7 +15,8 @@ | |
| register_resource_views, | ||
| ) | ||
| from cornice.util import ContentTypePredicate | ||
|
|
||
| from pyramid.events import BeforeRender, NewRequest | ||
| from pyramid.i18n import get_localizer | ||
| from pyramid.httpexceptions import HTTPNotFound, HTTPForbidden | ||
| from pyramid.security import NO_PERMISSION_REQUIRED | ||
|
|
||
|
|
@@ -33,11 +35,53 @@ def add_apidoc(config, pattern, func, service, **kwargs): | |
| info['func'] = func | ||
|
|
||
|
|
||
| def set_localizer_for_languages(event, available_languages, | ||
| default_locale_name): | ||
| """ | ||
| Sets the current locale based on the incoming Accept-Language header, if | ||
| present, and sets a localizer attribute on the request object based on | ||
| the current locale. | ||
|
|
||
| To be used as an event handler, this function needs to be partially applied | ||
| with the available_languages and default_locale_name arguments. The | ||
| resulting function will be an event handler which takes an event object as | ||
| its only argument. | ||
| """ | ||
| request = event.request | ||
| if request.accept_language: | ||
| accepted = request.accept_language | ||
| locale = accepted.best_match(available_languages, default_locale_name) | ||
| request._LOCALE_ = locale | ||
| localizer = get_localizer(request) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should do that all the time, or only lazy load it and do it when we really need? |
||
| request.localizer = localizer | ||
|
|
||
|
|
||
| def setup_localization(config): | ||
| """ | ||
| Setup localization based on the available_languages and | ||
| pyramid.default_locale_name settings. | ||
|
|
||
| These settings are named after suggestions from the "Internationalization | ||
| and Localization" section of the Pyramid documentation. | ||
| """ | ||
| try: | ||
| config.add_translation_dirs('colander:locale/') | ||
| settings = config.get_settings() | ||
| available_languages = settings['available_languages'].split() | ||
| default_locale_name = settings.get('pyramid.default_locale_name', 'en') | ||
| set_localizer = partial(set_localizer_for_languages, | ||
| available_languages=available_languages, | ||
| default_locale_name=default_locale_name) | ||
| config.add_subscriber(set_localizer, NewRequest) | ||
| except ImportError: | ||
| # add_translation_dirs raises an ImportError if colander is not | ||
| # installed | ||
| pass | ||
|
|
||
|
|
||
| def includeme(config): | ||
| """Include the Cornice definitions | ||
| """ | ||
| from pyramid.events import BeforeRender, NewRequest | ||
|
|
||
| # attributes required to maintain services | ||
| config.registry.cornice_services = {} | ||
|
|
||
|
|
@@ -61,3 +105,6 @@ def includeme(config): | |
| permission=NO_PERMISSION_REQUIRED) | ||
| config.add_view(handle_exceptions, context=HTTPForbidden, | ||
| permission=NO_PERMISSION_REQUIRED) | ||
|
|
||
| if settings.get('available_languages'): | ||
| setup_localization(config) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -201,6 +201,10 @@ before passing the result to Colander. | |
| View-specific deserializers have priority over global content-type | ||
| deserializers. | ||
|
|
||
| To enable localization of Colander error messages, you must set | ||
| `available_languages <http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/i18n.html#detecting-available-languages>`_ in your settings. | ||
| You may also set `pyramid.default_locale_name <http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/environment.html#default-locale-name-setting>`_. | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO, you should refer to or update 1.3 -> 1.6
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lmctv good catch, done |
||
|
|
||
| Using formencode | ||
| ~~~~~~~~~~~~~~~~ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a docstring here?