Skip to content

Commit

Permalink
Merge branch '3196-i18n' into 3229-api-blueprint
Browse files Browse the repository at this point in the history
Conflicts:
	ckan/common.py
  • Loading branch information
amercader committed Sep 2, 2016
2 parents 621763f + d6aad5d commit 2ef2c57
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 12 deletions.
21 changes: 20 additions & 1 deletion ckan/common.py
Expand Up @@ -15,7 +15,11 @@

from werkzeug.local import Local, LocalProxy

from pylons.i18n import _, ungettext
from flask_babel import (gettext as flask_ugettext,
ngettext as flask_ungettext)
from pylons.i18n import (ugettext as pylons_ugettext,
ungettext as pylons_ungettext)

from pylons import response
import simplejson as json

Expand All @@ -41,6 +45,21 @@ def is_flask_request():
not pylons_request_available))


def ugettext(*args, **kwargs):
if is_flask_request():
return flask_ugettext(*args, **kwargs)
else:
return pylons_ugettext(*args, **kwargs)
_ = ugettext


def ungettext(*args, **kwargs):
if is_flask_request():
return flask_ungettext(*args, **kwargs)
else:
return pylons_ungettext(*args, **kwargs)


class CKANConfig(MutableMapping):
u'''Main CKAN configuration object
Expand Down
19 changes: 19 additions & 0 deletions ckan/config/middleware/flask_app.py
Expand Up @@ -13,6 +13,7 @@
from werkzeug.exceptions import HTTPException
from werkzeug.routing import Rule

from flask_babel import Babel
from flask_debugtoolbar import DebugToolbarExtension

from beaker.middleware import SessionMiddleware
Expand Down Expand Up @@ -159,6 +160,24 @@ def c_object():
'''
return dict(c=g)

# Babel
app.config[u'BABEL_TRANSLATION_DIRECTORIES'] = os.path.join(root, u'i18n')
# TODO: this relies on unpublished changes in flask-babel
app.config[u'BABEL_DOMAIN'] = 'ckan'

babel = Babel(app)

@babel.localeselector
def get_locale():
u'''
Return the value of the `CKAN_LANG` key of the WSGI environ,
set by the I18nMiddleware based on the URL.
If no value is defined, it defaults to `ckan.locale_default` or `en`.
'''
return request.environ.get(
u'CKAN_LANG',
config.get(u'ckan.locale_default', u'en'))

@app.route('/hello', methods=['GET'])
def hello_world():
return 'Hello World, this is served by Flask'
Expand Down
1 change: 0 additions & 1 deletion ckan/lib/base.py
Expand Up @@ -12,7 +12,6 @@
from pylons.controllers import WSGIController
from pylons.controllers.util import abort as _abort
from pylons.decorators import jsonify
from pylons.i18n import N_, gettext, ngettext
from pylons.templating import cached_template, pylons_globals
from webhelpers.html import literal

Expand Down
2 changes: 1 addition & 1 deletion ckan/logic/action/__init__.py
Expand Up @@ -4,7 +4,7 @@
import re

from ckan.logic import NotFound
from ckan.lib.base import _, abort
from ckan.common import _


def rename_keys(dict_, key_map, reverse=False, destructive=False):
Expand Down
2 changes: 1 addition & 1 deletion ckan/logic/auth/delete.py
Expand Up @@ -4,7 +4,7 @@
import ckan.authz as authz
from ckan.logic.auth import get_group_object
from ckan.logic.auth import get_resource_object
from ckan.lib.base import _
from ckan.common import _


def user_delete(context, data_dict):
Expand Down
2 changes: 1 addition & 1 deletion ckan/logic/auth/get.py
Expand Up @@ -2,7 +2,7 @@

import ckan.logic as logic
import ckan.authz as authz
from ckan.lib.base import _
from ckan.common import _
from ckan.logic.auth import (get_package_object, get_group_object,
get_resource_object)

Expand Down
10 changes: 6 additions & 4 deletions ckan/plugins/toolkit.py
Expand Up @@ -155,19 +155,21 @@ def _initialize(self):
'''
t['_'] = common._
self.docstring_overrides['_'] = '''The Pylons ``_()`` function.
self.docstring_overrides['_'] = '''Translates a string to the
current locale.
The Pylons ``_()`` function is a reference to the ``ugettext()`` function.
The ``_()`` function is a reference to the ``ugettext()`` function.
Everywhere in your code where you want strings to be internationalized
(made available for translation into different languages), wrap them in the
``_()`` function, eg.::
msg = toolkit._("Hello")
Returns the localized unicode string.
'''
t['ungettext'] = common.ungettext
self.docstring_overrides['ungettext'] = '''The Pylons ``ungettext``
function.
self.docstring_overrides['ungettext'] = '''Translates a string with
plural forms to the current locale.
Mark a string for translation that has pural forms in the format
``ungettext(singular, plural, n)``. Returns the localized unicode string of
Expand Down
12 changes: 9 additions & 3 deletions doc/contributing/string-i18n.rst
Expand Up @@ -148,16 +148,22 @@ Singular and plural forms are handled by ``ungettext()``:
Internationalizing strings in Python code
-----------------------------------------

CKAN uses the :py:func:`~pylons.i18n._` and :py:func:`~pylons.i18n.ungettext`
functions from the `pylons.i18n.translation`_ module to internationalize
CKAN uses the :py:func:`~flask_babel._` and :py:func:`~flask_babel.ngettext`
functions from the `Flask-Babel`_ library to internationalize
strings in Python code.

.. _Flask-Babel: https://pythonhosted.org/Flask-Babel/

.. note::
Code running on Pylons will use the functions provided by
the `pylons.i18n.translation`_ module, but their behaviour is the same.

.. _pylons.i18n.translation: http://docs.pylonsproject.org/projects/pylons-webframework/en/latest/modules/i18n_translation.html#module-pylons.i18n.translation

Core CKAN modules should import :py:func:`~ckan.common._` and
:py:func:`~ckan.common.ungettext` from :py:mod:`ckan.common`,
i.e. ``from ckan.common import _, ungettext``
(don't import :py:func:`pylons.i18n.translation._` directly, for example).
(don't import :py:func:`flask_babel._` or :py:func:`pylons.i18n.translation._` directly, for example).

CKAN plugins should import :py:mod:`ckan.plugins.toolkit` and use
:py:func:`ckan.plugins.toolkit._` and
Expand Down
1 change: 1 addition & 0 deletions requirements.in
Expand Up @@ -5,6 +5,7 @@ Beaker==1.8.0 # Needs to be pinned to a more up to date version than the Pylons
bleach==1.4.3
fanstatic==0.12
Flask==0.10.1
Flask-Babel==0.11.1
Jinja2==2.8
Markdown==2.6.6
ofs==0.4.2
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -11,6 +11,7 @@ bleach==1.4.3
decorator==4.0.6 # via pylons, sqlalchemy-migrate
fanstatic==0.12
Flask==0.10.1
Flask-Babel==0.11.1
FormEncode==1.3.0
html5lib==0.9999999 # via bleach
itsdangerous==0.24 # via flask
Expand Down

0 comments on commit 2ef2c57

Please sign in to comment.