Skip to content

Commit

Permalink
[#2199] Translate facets on dataset search page
Browse files Browse the repository at this point in the history
ckanext/multilingual/plugin.py
Add after_search() method that looks up translations of facet items and
puts them in a translations dict in the pylons template context.

ckan/lib/helpers.py:facet_items()
Append a third value to each facet item: its translated name from the
translations dict.

ckan/templates/facets.html:facet_sidebar()
Use the translated names put into the facet items by facet_items().
  • Loading branch information
Sean Hammond committed Feb 28, 2012
1 parent 9285e3c commit 531bf9a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
18 changes: 16 additions & 2 deletions ckan/lib/helpers.py
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions ckan/templates/facets.html
Expand Up @@ -9,9 +9,10 @@
<div py:if="len(h.facet_items(c, code, limit=limit))" class="facet-box">
<h2>${title(code)}</h2>
<ul class="facet-options">
<li py:for="name, count in h.facet_items(c, code, limit=limit)"
<li py:for="name, count, translated_name in h.facet_items(c,
code, limit=limit)"
py:if="not (code, name) in c.fields">
<a href="${c.drill_down_url(**{code: name})}">${label(name)}</a> (${count})
<a href="${c.drill_down_url(**{code: name})}">${label(translated_name)}</a> (${count})
</li>
</ul>
</div>
Expand Down
50 changes: 48 additions & 2 deletions ckanext/multilingual/plugin.py
@@ -1,3 +1,4 @@
import sets
import ckan
from ckan.plugins import SingletonPlugin, implements, IPackageController
import pylons
Expand Down Expand Up @@ -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')
Expand All @@ -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
Expand Down

0 comments on commit 531bf9a

Please sign in to comment.