Skip to content

Commit

Permalink
[#959] ITranslation interface fix
Browse files Browse the repository at this point in the history
Only add translations strings for the current locale (instead of all).
Allow 'en' translation strings in extensions to overwrite core strings.
  • Loading branch information
joetsoi committed Jun 16, 2015
1 parent 0e9dfaa commit 93d3a54
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
28 changes: 22 additions & 6 deletions ckan/lib/i18n.py
@@ -1,4 +1,5 @@
import os
import gettext

from babel import Locale, localedata
from babel.core import LOCALE_ALIASES
Expand Down Expand Up @@ -129,12 +130,6 @@ def _set_lang(lang):
else:
i18n.set_lang(lang, class_=Translations)

for plugin in PluginImplementations(ITranslation):
pylons.translator.merge(Translations.load(
dirname=plugin.directory(),
locales=plugin.locales(),
domain=plugin.domain()
))


def handle_request(request, tmpl_context):
Expand All @@ -143,6 +138,27 @@ def handle_request(request, tmpl_context):
config.get('ckan.locale_default', 'en')
if lang != 'en':
set_lang(lang)

for plugin in PluginImplementations(ITranslation):
if lang in plugin.locales():
translator = Translations.load(
dirname=plugin.directory(),
locales=lang,
domain=plugin.domain()
)
try:
pylons.translator.merge(translator)
except AttributeError:
# this occurs when an extension has 'en' translations that
# replace the default strings. As set_lang has not been run,
# pylons.translation is the NullTranslation, so we have to
# replace the StackedObjectProxy ourselves manually.
environ = pylons.request.environ
environ['pylons.pylons'].translator = translator
if 'paste.registry' in environ:
environ['paste.registry'].replace(pylons.translator,
translator)

tmpl_context.language = lang
return lang

Expand Down
18 changes: 17 additions & 1 deletion ckanext/example_itranslation/tests/test_plugin.py
Expand Up @@ -31,6 +31,14 @@ def test_translated_string_in_extensions_templates(self):
assert_true('This is an untranslated string' in response.body)
assert_false('This is a itranslated string' in response.body)

# check that we have only overwritten 'fr'
response = app.get(
url=plugins.toolkit.url_for(controller='home', action='index',
locale='es'),
)
assert_true('This is an untranslated string' in response.body)
assert_false('This is a itranslated string' in response.body)

def test_translated_string_in_core_templates(self):
app = self._get_test_app()
response = app.get(
Expand All @@ -39,10 +47,18 @@ def test_translated_string_in_core_templates(self):
)
assert_true('Overwritten string in ckan.mo' in response.body)
assert_false('Connexion' in response.body)

# double check the untranslated strings
response = app.get(
url=plugins.toolkit.url_for(controller='home', action='index'),
)
assert_true('Log in' in response.body)
assert_false('Overwritten string in ckan.mo' in response.body)

# check that we have only overwritten 'fr'
response = app.get(
url=plugins.toolkit.url_for(controller='home', action='index',
locale='de'),
)
assert_true('Einloggen' in response.body)
assert_false('Overwritten string in ckan.mo' in response.body)

0 comments on commit 93d3a54

Please sign in to comment.