diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py
index 150083b0c6c..65378540044 100644
--- a/ckan/lib/helpers.py
+++ b/ckan/lib/helpers.py
@@ -223,8 +223,22 @@ def facet_items(c, name, limit=10):
if not len(k.strip()):
continue
if not (name, k) in request.params.items():
- facets.append((k, v))
- return sorted(facets, key=lambda (k, v): v, reverse=True)[:limit]
+ facets.append([k, v])
+
+ # Append a third value to each facet item: the translated name of the facet
+ # item, to be displayed to the user. Translations are looked for in a
+ # 'translations' dict in the Pylons context.
+ for facet in facets:
+ name, count = facet
+ if c.translations:
+ facet.append(c.translations.get(name, name))
+ else:
+ # If there is no translations dict in the context, just use the
+ # facet item's untranslated name.
+ facet.append(name)
+
+ return sorted(facets, key=lambda (name, count, translated_name):
+ translated_name, reverse=True)[:limit]
def facet_title(name):
from pylons import config
diff --git a/ckan/templates/facets.html b/ckan/templates/facets.html
index 0ffe47bbd91..5eefc96cab8 100644
--- a/ckan/templates/facets.html
+++ b/ckan/templates/facets.html
@@ -9,9 +9,10 @@
diff --git a/ckanext/multilingual/plugin.py b/ckanext/multilingual/plugin.py
index 62e868110d6..1f174a85ea0 100644
--- a/ckanext/multilingual/plugin.py
+++ b/ckanext/multilingual/plugin.py
@@ -1,3 +1,4 @@
+import sets
import ckan
from ckan.plugins import SingletonPlugin, implements, IPackageController
import pylons
@@ -33,6 +34,52 @@ def before_search(self, search_params):
return search_params
+ def after_search(self, search_results, search_params):
+
+ facets = search_results.get('facets')
+ if not facets:
+ return search_results
+
+ desired_lang_code = pylons.request.environ['CKAN_LANG']
+ fallback_lang_code = pylons.config.get('ckan.locale_default', 'en')
+
+ # Look up all the term translations in one db query.
+ terms = sets.Set()
+ for facet in facets.values():
+ for name, count in facet.items():
+ terms.add(name)
+ translations = ckan.logic.action.get.term_translation_show(
+ {'model': ckan.model},
+ {'terms': terms,
+ 'lang_codes': (desired_lang_code, fallback_lang_code)})
+
+ # Add translations of facet items to a translations dict in the pylons
+ # template context.
+ pylons.c.translations = {}
+ for facet in facets.values():
+ for name, count in facet.items():
+ matching_translations = [translation for translation
+ in translations
+ if translation['term'] == name
+ and translation['lang_code'] == desired_lang_code]
+ if not matching_translations:
+ # If no translation in desired language then look for one
+ # in fallback language.
+ matching_translations = [translation for translation
+ in translations
+ if translation['term'] == name
+ and translation['lang_code'] == fallback_lang_code]
+ assert len(matching_translations) in (0,1)
+ if matching_translations:
+ pylons.c.translations[name] = (
+ matching_translations[0]['term_translation'])
+ else:
+ # If no translation in either desired or fallback language,
+ # just use the item name itself, untranslated.
+ pylons.c.translations[name] = name
+
+ return search_results
+
def before_view(self, data_dict):
desired_lang_code = pylons.request.environ['CKAN_LANG']
fallback_lang_code = pylons.config.get('ckan.locale_default', 'en')
@@ -43,8 +90,7 @@ def before_view(self, data_dict):
# Get a simple flat list of all the terms to be translated, from the
# flattened data dict.
- from sets import Set
- terms = Set()
+ terms = sets.Set()
for (key, value) in flattened.items():
if value in (None, True, False):
continue