diff --git a/base_name_search_multi_lang/README.rst b/base_name_search_multi_lang/README.rst new file mode 100644 index 00000000000..e750aab6b26 --- /dev/null +++ b/base_name_search_multi_lang/README.rst @@ -0,0 +1,115 @@ +====================== +Name Search Multi Lang +====================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:93b25b918e684a24cabcf121005768369e05189b159a408efd4715ea703b18cd + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github + :target: https://github.com/OCA/server-tools/tree/18.0/base_name_search_multi_lang + :alt: OCA/server-tools +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/server-tools-18-0/server-tools-18-0-base_name_search_multi_lang + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/server-tools&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module extends the name search feature to use other translated +languages. + +For example, if a product name in English is "Chair", and Thai is "เก้าอี้" + +Given user language preference is English, when user types name to +search on product field, "Chair", the product with this name will be +found. But if user types in Thai, "เก้าอี้", no result will be shown. + +With this module installed, and model product.product is set to use +"Search Translated Name". Searching by Thai name, "เก้าอี้", now finds the +product "Chair". + +Every search matching a translated field with a pattern is extended, so +this covers the Many2one fields as well as the search views. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +Additional search fields can be configured at Settings > Technical > +Database > Models, using the "Search Translated Name" field. + +Usage +===== + +Just type into any field with translated names, such as Product on a +Sales Order, or into the search box of a list view. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Ecosoft + +Contributors +------------ + +- Kitti U. + +Migration +~~~~~~~~~ + +- Nakrob S. (18.0 migration) + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +.. |maintainer-kittiu| image:: https://github.com/kittiu.png?size=40px + :target: https://github.com/kittiu + :alt: kittiu + +Current `maintainer `__: + +|maintainer-kittiu| + +This module is part of the `OCA/server-tools `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/base_name_search_multi_lang/__init__.py b/base_name_search_multi_lang/__init__.py new file mode 100644 index 00000000000..c48f631227b --- /dev/null +++ b/base_name_search_multi_lang/__init__.py @@ -0,0 +1,3 @@ +# Copyright 2020 Ecosoft Co., Ltd (http://ecosoft.co.th/) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html) +from . import models diff --git a/base_name_search_multi_lang/__manifest__.py b/base_name_search_multi_lang/__manifest__.py new file mode 100644 index 00000000000..faf3764310d --- /dev/null +++ b/base_name_search_multi_lang/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2020 Ecosoft Co., Ltd (http://ecosoft.co.th/) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html) +{ + "name": "Name Search Multi Lang", + "summary": "Name search by multiple active language", + "version": "18.0.1.0.0", + "category": "Tools", + "website": "https://github.com/OCA/server-tools", + "author": "Ecosoft, Odoo Community Association (OCA)", + "license": "AGPL-3", + "depends": ["base"], + "data": ["views/ir_model_views.xml"], + "maintainers": ["kittiu"], + "installable": True, +} diff --git a/base_name_search_multi_lang/i18n/base_name_search_multi_lang.pot b/base_name_search_multi_lang/i18n/base_name_search_multi_lang.pot new file mode 100644 index 00000000000..1b6875207fe --- /dev/null +++ b/base_name_search_multi_lang/i18n/base_name_search_multi_lang.pot @@ -0,0 +1,49 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * base_name_search_multi_lang +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 14.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: base_name_search_multi_lang +#: model:ir.model.fields,field_description:base_name_search_multi_lang.field_ir_model__display_name +msgid "Display Name" +msgstr "" + +#. module: base_name_search_multi_lang +#: model:ir.model.fields,field_description:base_name_search_multi_lang.field_ir_model__id +msgid "ID" +msgstr "" + +#. module: base_name_search_multi_lang +#: model:ir.model.fields,field_description:base_name_search_multi_lang.field_ir_model____last_update +msgid "Last Modified on" +msgstr "" + +#. module: base_name_search_multi_lang +#: model:ir.model,name:base_name_search_multi_lang.model_ir_model +msgid "Models" +msgstr "" + +#. module: base_name_search_multi_lang +#: model:ir.model.fields,help:base_name_search_multi_lang.field_ir_model__name_search_multi_lang +msgid "Name search this model from all translated languages" +msgstr "" + +#. module: base_name_search_multi_lang +#: model:ir.model.fields,field_description:base_name_search_multi_lang.field_ir_model__name_search_multi_lang +msgid "Search Translated Name" +msgstr "" + +#. module: base_name_search_multi_lang +#: model:ir.model.fields,field_description:base_name_search_multi_lang.field_ir_model__smart_search +msgid "Smart Search" +msgstr "" diff --git a/base_name_search_multi_lang/i18n/es_AR.po b/base_name_search_multi_lang/i18n/es_AR.po new file mode 100644 index 00000000000..9e1e860ace1 --- /dev/null +++ b/base_name_search_multi_lang/i18n/es_AR.po @@ -0,0 +1,52 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * base_name_search_multi_lang +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 14.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2022-09-04 06:07+0000\n" +"Last-Translator: Ignacio Buioli \n" +"Language-Team: none\n" +"Language: es_AR\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 4.3.2\n" + +#. module: base_name_search_multi_lang +#: model:ir.model.fields,field_description:base_name_search_multi_lang.field_ir_model__display_name +msgid "Display Name" +msgstr "Mostrar Nombre" + +#. module: base_name_search_multi_lang +#: model:ir.model.fields,field_description:base_name_search_multi_lang.field_ir_model__id +msgid "ID" +msgstr "ID" + +#. module: base_name_search_multi_lang +#: model:ir.model.fields,field_description:base_name_search_multi_lang.field_ir_model____last_update +msgid "Last Modified on" +msgstr "Última modificación en" + +#. module: base_name_search_multi_lang +#: model:ir.model,name:base_name_search_multi_lang.model_ir_model +msgid "Models" +msgstr "Modelos" + +#. module: base_name_search_multi_lang +#: model:ir.model.fields,help:base_name_search_multi_lang.field_ir_model__name_search_multi_lang +msgid "Name search this model from all translated languages" +msgstr "Búsqueda por nombre de este modelo en todos los idiomas traducidos" + +#. module: base_name_search_multi_lang +#: model:ir.model.fields,field_description:base_name_search_multi_lang.field_ir_model__name_search_multi_lang +msgid "Search Translated Name" +msgstr "Buscar Nombre Traducido" + +#. module: base_name_search_multi_lang +#: model:ir.model.fields,field_description:base_name_search_multi_lang.field_ir_model__smart_search +msgid "Smart Search" +msgstr "Búsqueda Inteligente" diff --git a/base_name_search_multi_lang/i18n/it.po b/base_name_search_multi_lang/i18n/it.po new file mode 100644 index 00000000000..ea1afce0bbf --- /dev/null +++ b/base_name_search_multi_lang/i18n/it.po @@ -0,0 +1,52 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * base_name_search_multi_lang +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 14.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2025-03-14 15:06+0000\n" +"Last-Translator: mymage \n" +"Language-Team: none\n" +"Language: it\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 5.10.2\n" + +#. module: base_name_search_multi_lang +#: model:ir.model.fields,field_description:base_name_search_multi_lang.field_ir_model__display_name +msgid "Display Name" +msgstr "Nome visualizzato" + +#. module: base_name_search_multi_lang +#: model:ir.model.fields,field_description:base_name_search_multi_lang.field_ir_model__id +msgid "ID" +msgstr "" + +#. module: base_name_search_multi_lang +#: model:ir.model.fields,field_description:base_name_search_multi_lang.field_ir_model____last_update +msgid "Last Modified on" +msgstr "" + +#. module: base_name_search_multi_lang +#: model:ir.model,name:base_name_search_multi_lang.model_ir_model +msgid "Models" +msgstr "" + +#. module: base_name_search_multi_lang +#: model:ir.model.fields,help:base_name_search_multi_lang.field_ir_model__name_search_multi_lang +msgid "Name search this model from all translated languages" +msgstr "" + +#. module: base_name_search_multi_lang +#: model:ir.model.fields,field_description:base_name_search_multi_lang.field_ir_model__name_search_multi_lang +msgid "Search Translated Name" +msgstr "" + +#. module: base_name_search_multi_lang +#: model:ir.model.fields,field_description:base_name_search_multi_lang.field_ir_model__smart_search +msgid "Smart Search" +msgstr "" diff --git a/base_name_search_multi_lang/models/__init__.py b/base_name_search_multi_lang/models/__init__.py new file mode 100644 index 00000000000..6847d5dc32b --- /dev/null +++ b/base_name_search_multi_lang/models/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2020 Ecosoft Co., Ltd (http://ecosoft.co.th/) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html) +from . import base +from . import ir_model diff --git a/base_name_search_multi_lang/models/base.py b/base_name_search_multi_lang/models/base.py new file mode 100644 index 00000000000..88d25ffb5e3 --- /dev/null +++ b/base_name_search_multi_lang/models/base.py @@ -0,0 +1,65 @@ +# Copyright 2020 Ecosoft Co., Ltd (http://ecosoft.co.th/) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html) +from odoo import models, tools +from odoo.osv import expression +from odoo.tools import SQL + +# Other operators compare the value as a whole, where matching another +# language makes no sense +PATTERN_OPERATORS = ("like", "ilike", "not like", "not ilike", "=like", "=ilike") + + +class Base(models.AbstractModel): + _inherit = "base" + + @tools.ormcache("self._name") + def _name_search_multi_lang_enabled(self): + """Whether this model is searched in all the installed languages""" + data = ( + self.env["ir.model"] + .sudo() + .search_read([("model", "=", self._name)], ["name_search_multi_lang"]) + ) + return bool(data and data[0]["name_search_multi_lang"]) + + def _condition_to_sql(self, alias, fname, operator, value, query): + """Match a translated field in every language + + The standard implementation only matches the current language, with a + fallback on the company ones. + """ + sql = super()._condition_to_sql(alias, fname, operator, value, query) + field = self._fields.get(fname) + if ( + field is None + or not field.translate + or not field.store + or operator not in PATTERN_OPERATORS + or not isinstance(value, str) + or not value + or not self._name_search_multi_lang_enabled() + ): + return sql + # All languages live in the same jsonb column, and this is the + # expression a trigram index is built on, so such an index is still used + sql_langs = self.env.registry.unaccent( + SQL( + "jsonb_path_query_array(%s, '$.*')::text", + SQL.identifier(alias, fname, to_flush=field), + ) + ) + sql_like = SQL("ILIKE") if operator.endswith("ilike") else SQL("LIKE") + sql_pattern = self.env.registry.unaccent( + SQL("%s", value if operator.startswith("=") else f"%{value}%") + ) + if operator in expression.NEGATIVE_TERM_OPERATORS: + # Exclude the record as soon as one language matches + return SQL( + "(%s AND (%s IS NULL OR %s NOT %s %s))", + sql, + sql_langs, + sql_langs, + sql_like, + sql_pattern, + ) + return SQL("(%s OR %s %s %s)", sql, sql_langs, sql_like, sql_pattern) diff --git a/base_name_search_multi_lang/models/ir_model.py b/base_name_search_multi_lang/models/ir_model.py new file mode 100644 index 00000000000..b51125bf3e7 --- /dev/null +++ b/base_name_search_multi_lang/models/ir_model.py @@ -0,0 +1,16 @@ +# Copyright 2020 Ecosoft Co., Ltd (http://ecosoft.co.th/) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html) +from odoo import api, fields, models + + +class IrModel(models.Model): + _inherit = "ir.model" + + name_search_multi_lang = fields.Boolean( + string="Search Translated Name", + help="Name search this model from all translated languages", + ) + + @api.constrains("name_search_multi_lang") + def update_name_search_multi_lang(self): + self.env.registry.clear_cache() diff --git a/base_name_search_multi_lang/pyproject.toml b/base_name_search_multi_lang/pyproject.toml new file mode 100644 index 00000000000..4231d0cccb3 --- /dev/null +++ b/base_name_search_multi_lang/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/base_name_search_multi_lang/readme/CONFIGURE.md b/base_name_search_multi_lang/readme/CONFIGURE.md new file mode 100644 index 00000000000..2bc23d11f4c --- /dev/null +++ b/base_name_search_multi_lang/readme/CONFIGURE.md @@ -0,0 +1,2 @@ +Additional search fields can be configured at Settings \> Technical \> +Database \> Models, using the "Search Translated Name" field. diff --git a/base_name_search_multi_lang/readme/CONTRIBUTORS.md b/base_name_search_multi_lang/readme/CONTRIBUTORS.md new file mode 100644 index 00000000000..08c27ad1041 --- /dev/null +++ b/base_name_search_multi_lang/readme/CONTRIBUTORS.md @@ -0,0 +1,5 @@ +- Kitti U. \ + +## Migration + +- Nakrob S. \ (18.0 migration) diff --git a/base_name_search_multi_lang/readme/DESCRIPTION.md b/base_name_search_multi_lang/readme/DESCRIPTION.md new file mode 100644 index 00000000000..c226ed160be --- /dev/null +++ b/base_name_search_multi_lang/readme/DESCRIPTION.md @@ -0,0 +1,15 @@ +This module extends the name search feature to use other translated +languages. + +For example, if a product name in English is "Chair", and Thai is "เก้าอี้" + +Given user language preference is English, when user types name to search +on product field, "Chair", the product with this name will be found. But +if user types in Thai, "เก้าอี้", no result will be shown. + +With this module installed, and model product.product is set to use +"Search Translated Name". Searching by Thai name, "เก้าอี้", now finds the +product "Chair". + +Every search matching a translated field with a pattern is extended, so +this covers the Many2one fields as well as the search views. diff --git a/base_name_search_multi_lang/readme/USAGE.md b/base_name_search_multi_lang/readme/USAGE.md new file mode 100644 index 00000000000..d16553449c7 --- /dev/null +++ b/base_name_search_multi_lang/readme/USAGE.md @@ -0,0 +1,2 @@ +Just type into any field with translated names, such as Product on a Sales +Order, or into the search box of a list view. diff --git a/base_name_search_multi_lang/static/description/icon.png b/base_name_search_multi_lang/static/description/icon.png new file mode 100644 index 00000000000..3a0328b516c Binary files /dev/null and b/base_name_search_multi_lang/static/description/icon.png differ diff --git a/base_name_search_multi_lang/static/description/index.html b/base_name_search_multi_lang/static/description/index.html new file mode 100644 index 00000000000..5a823b27d5b --- /dev/null +++ b/base_name_search_multi_lang/static/description/index.html @@ -0,0 +1,456 @@ + + + + + +Name Search Multi Lang + + + +
+

Name Search Multi Lang

+ + +

Beta License: AGPL-3 OCA/server-tools Translate me on Weblate Try me on Runboat

+

This module extends the name search feature to use other translated +languages.

+

For example, if a product name in English is “Chair”, and Thai is “เก้าอี้”

+

Given user language preference is English, when user types name to +search on product field, “Chair”, the product with this name will be +found. But if user types in Thai, “เก้าอี้”, no result will be shown.

+

With this module installed, and model product.product is set to use +“Search Translated Name”. Searching by Thai name, “เก้าอี้”, now finds the +product “Chair”.

+

Every search matching a translated field with a pattern is extended, so +this covers the Many2one fields as well as the search views.

+

Table of contents

+ +
+

Configuration

+

Additional search fields can be configured at Settings > Technical > +Database > Models, using the “Search Translated Name” field.

+
+
+

Usage

+

Just type into any field with translated names, such as Product on a +Sales Order, or into the search box of a list view.

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Ecosoft
  • +
+
+
+

Contributors

+ +
+

Migration

+ +
+
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

Current maintainer:

+

kittiu

+

This module is part of the OCA/server-tools project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/base_name_search_multi_lang/tests/__init__.py b/base_name_search_multi_lang/tests/__init__.py new file mode 100644 index 00000000000..5b3b05c2ef1 --- /dev/null +++ b/base_name_search_multi_lang/tests/__init__.py @@ -0,0 +1,3 @@ +# Copyright 2020 Ecosoft Co., Ltd (http://ecosoft.co.th/) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html) +from . import test_name_search diff --git a/base_name_search_multi_lang/tests/test_name_search.py b/base_name_search_multi_lang/tests/test_name_search.py new file mode 100644 index 00000000000..f6cb05a89ab --- /dev/null +++ b/base_name_search_multi_lang/tests/test_name_search.py @@ -0,0 +1,48 @@ +# Copyright 2020 Ecosoft Co., Ltd (http://ecosoft.co.th/) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html) +from odoo.tests import tagged + +from odoo.addons.base.tests.common import BaseCommon + + +@tagged("-at_install", "post_install") +class NameSearchCase(BaseCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env["res.lang"]._activate_lang("th_TH") + cls.env.ref("base.module_base")._update_translations() + cls.model = cls.env["ir.model"] + cls.dependency = cls.env.ref("base.model_ir_module_module_dependency") + + def _enable_multi_lang(self): + self.env.ref("base.model_ir_model").name_search_multi_lang = True + + def test_name_search_normal(self): + """Search for a model name in other language, not found""" + res = self.model.name_search("Module dependency") + self.assertTrue(res) + res = self.model.name_search("การพึ่งพาของโมดูล") + self.assertFalse(res) # Not found in other language + + def test_name_search_multi_lang(self): + """Search for a model name in other language, found it""" + self._enable_multi_lang() + res = self.model.name_search("Module dependency") + self.assertTrue(res) + res = self.model.name_search("การพึ่งพาของโมดูล") + self.assertTrue(res) # Found it even in other language + + def test_search_translated_field_multi_lang(self): + """Searching the translated field itself, as a search view does""" + domain = [("name", "ilike", "การพึ่งพาของโมดูล")] + self.assertFalse(self.model.search(domain)) + self._enable_multi_lang() + self.assertEqual(self.model.search(domain), self.dependency) + + def test_search_negative_operator_multi_lang(self): + """A record matching in any language is excluded""" + domain = [("name", "not ilike", "การพึ่งพาของโมดูล")] + self.assertIn(self.dependency, self.model.search(domain)) + self._enable_multi_lang() + self.assertNotIn(self.dependency, self.model.search(domain)) diff --git a/base_name_search_multi_lang/views/ir_model_views.xml b/base_name_search_multi_lang/views/ir_model_views.xml new file mode 100644 index 00000000000..99a3a1363e1 --- /dev/null +++ b/base_name_search_multi_lang/views/ir_model_views.xml @@ -0,0 +1,15 @@ + + + + + view.model.form + ir.model + + + + + + + +