Updated language management in text search
authorThierry Florac <thierry.florac@onf.fr>
Thu, 21 Sep 2017 12:00:57 +0200
changeset 177 6b7ecb3c7bca
parent 176 64e00f5537fe
child 178 1f3b274d8a76
Updated language management in text search
src/pyams_content/shared/common/zmi/search.py
--- a/src/pyams_content/shared/common/zmi/search.py	Wed Sep 20 12:12:58 2017 +0200
+++ b/src/pyams_content/shared/common/zmi/search.py	Thu Sep 21 12:00:57 2017 +0200
@@ -20,6 +20,7 @@
 from pyams_content.profile.interfaces import IAdminProfile
 from pyams_content.shared.common.interfaces import ISharedTool
 from pyams_content.shared.common.interfaces.zmi import ISharedToolDashboardTable
+from pyams_i18n.interfaces import INegotiator
 from pyams_pagelet.interfaces import PageletCreatedEvent
 from pyams_sequence.interfaces import ISequentialIntIds
 from pyams_skin.interfaces import IPageHeader, IContentSearch, IInnerPage
@@ -98,9 +99,15 @@
                 params &= Eq(catalog['oid'], sequence.get_full_oid(query))
             else:
                 query_params = Eq(catalog['oid'], sequence.get_full_oid(query))
-                index = catalog['title:' + self.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 {self.request.registry.settings.get('pyramid.default_locale_name', 'en'),
+                             self.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
         return unique(map(lambda x: IWorkflowVersions(x).get_last_versions()[0],
                           CatalogResultSet(CatalogQuery(catalog).query(params,
@@ -171,9 +178,15 @@
                 params &= Eq(catalog['oid'], sequence.get_full_oid(query))
             else:
                 query_params = Eq(catalog['oid'], sequence.get_full_oid(query))
-                index = catalog['title:' + self.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 {self.request.registry.settings.get('pyramid.default_locale_name', 'en'),
+                             self.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 data.get('owner'):
             params &= Eq(catalog['role:owner'], data['owner'])