Updated language management in text search
authorThierry Florac <thierry.florac@onf.fr>
Thu, 21 Sep 2017 15:02:29 +0200
changeset 19 c4ce1f216ae4
parent 18 6c2acd16cd62
child 20 fea1fe09dd19
Updated language management in text search
src/pyams_sequence/rpc/json/__init__.py
--- a/src/pyams_sequence/rpc/json/__init__.py	Tue Sep 19 11:35:50 2017 +0200
+++ b/src/pyams_sequence/rpc/json/__init__.py	Thu Sep 21 15:02:29 2017 +0200
@@ -17,6 +17,7 @@
 
 # import interfaces
 from hypatia.interfaces import ICatalog
+from pyams_i18n.interfaces import INegotiator
 from pyams_sequence.interfaces import ISequentialIntIds
 
 # import packages
@@ -40,9 +41,15 @@
         params = Eq(catalog['oid'], sequence.get_full_oid(query))
     else:
         query_params = Eq(catalog['oid'], sequence.get_full_oid(query))
-        index = catalog['title:' + request.registry.settings.get('pyramid.default_locale_name', 'en')]
-        if index.check_query(query):
-            query_params |= Contains(index, ' and '.join((w+'*' for w in query.split())))
+        negotiator = get_utility(INegotiator)
+        for lang in {request.registry.settings.get('pyramid.default_locale_name', 'en'),
+                     request.locale_name,
+                     negotiator.server_language} | negotiator.offered_languages:
+            index_name = 'title:{0}'.format(lang)
+            if index_name in catalog:
+                index = catalog[index_name]
+                if index.check_query(query):
+                    query_params |= Contains(index, ' and '.join((w+'*' for w in query.split())))
         params = query_params
     if content_type:
         params &= Eq(catalog['content_type'], content_type)