Skip to content

Commit 9ecda85

Browse files
[18.0][MIG] Migrated module website_menu_url_translate from v15 to v18 (#213)
* [18.0][MIG] Migrated module website_menu_url_translate from v15 to v18 * [ADD/IMP] Implemented the translation controller and Added the test data file and test .py file * Update author and website fields in manifest * Simplify README by removing usage and credits Removed detailed usage instructions and credits from README. * fix title Updated the formatting of the README to improve consistency. --------- Co-authored-by: jans23 <[email protected]>
1 parent 768b00f commit 9ecda85

File tree

12 files changed

+236
-0
lines changed

12 files changed

+236
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.. image:: https://img.shields.io/badge/license-LGPL--3-blue.svg
2+
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
3+
:alt: License: LGPL-3
4+
5+
============================
6+
Website Menu URL Translation
7+
============================
8+
9+
This module allows you to translate the URL of the website menu in case if you have a multilanguage website
10+
11+
Usage
12+
=====
13+
14+
1. Go to Website -> Configuration -> Menus
15+
2. Create a new menu entry or open an existing one.
16+
3. You will see an icon over the field URL for translation (e.g. "EN" for English).
17+
4. Click on that icon and change the URL of the required language and save.
18+
19+
20+
Credits
21+
=======
22+
23+
Contributors
24+
------------
25+
26+
* Nilesh Sheliya <[email protected]>
27+
* Nitrokey: www.nitrokey.com
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import controllers
2+
from . import models
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "Website Menu URL Translation",
3+
"summary": "Translate website menu URLs for different languages.",
4+
"category": "Website",
5+
"version": "18.0.1.0.0",
6+
"author": "GYB IT SOLUTIONS, Nitrokey GmbH, Synodica Solutions Pvt. Ltd.",
7+
"website": "https://github.com/Nitrokey/odoo-modules/",
8+
"license": "LGPL-3",
9+
"depends": ["website"],
10+
"data": [
11+
"data/res_lang.xml",
12+
"views/website_page.xml",
13+
"data/test_website_menu_url_data.xml",
14+
],
15+
"installable": True,
16+
"application": False,
17+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import main
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import re
2+
3+
from odoo import http
4+
from odoo.http import request
5+
6+
from odoo.addons.web.controllers.home import Home as WebHome
7+
8+
9+
class Website(WebHome):
10+
"""Override Odoo's default language switch to support translated URLs."""
11+
12+
@http.route(
13+
"/website/lang/<lang>",
14+
type="http",
15+
auth="public",
16+
website=True,
17+
multilang=False,
18+
)
19+
def change_lang(self, lang, r="/", **kwargs):
20+
"""Redirect user to translated URL when changing language."""
21+
website = request.website
22+
default_lang = website.default_lang_id.url_code
23+
24+
# Resolve target language code
25+
lang_data = request.env["res.lang"]._get_data(url_code=lang)
26+
lang_code = lang_data.code or lang
27+
request.update_context(lang=lang_code)
28+
29+
# Remove any language prefix (e.g. /de/contactus → /contactus)
30+
clean_url = re.sub(r"^/[a-z]{2}(?:_[A-Z]{2})?/", "/", r or "/")
31+
32+
# Get translated page URL (if available)
33+
translated_url = self._get_translated_url(clean_url, lang_code)
34+
35+
# Build redirect target
36+
redirect_url = (
37+
f"/{lang}{translated_url}" if lang != default_lang else translated_url
38+
)
39+
redirect = request.redirect(redirect_url)
40+
redirect.set_cookie("frontend_lang", lang_code)
41+
return redirect
42+
43+
def _get_translated_url(self, src_url, lang_code):
44+
"""Return translated page URL for the given language."""
45+
web_page = request.env["website.page"].sudo()
46+
clean_url = re.sub(r"^/[a-z]{2}(?:_[A-Z]{2})?/", "/", src_url or "/")
47+
48+
# Try to find the page in ANY language to get the base source
49+
page = None
50+
for lang in web_page.env["res.lang"].search([("active", "=", True)]):
51+
candidate = web_page.with_context(lang=lang.code).search(
52+
[("url", "=", clean_url)], limit=1
53+
)
54+
if candidate:
55+
page = candidate
56+
break
57+
58+
# Return translated version or fallback to clean URL
59+
return page.with_context(lang=lang_code).url if page else clean_url
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<odoo>
3+
<!-- Activate German language -->
4+
<record id="base.lang_de" model="res.lang">
5+
<field name="active">True</field>
6+
</record>
7+
</odoo>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<odoo>
3+
<!-- Shared view for both language versions -->
4+
<record id="test_contact_page_view" model="ir.ui.view">
5+
<field name="name">contactus.test.page.view</field>
6+
<field name="type">qweb</field>
7+
<field name="key">website_menu_url_translate.contactus_test_page</field>
8+
<field name="arch" type="xml">
9+
<t t-name="website_menu_url_translate.contactus_test_page">
10+
<t t-call="website.layout">
11+
<div class="container">
12+
<h1>Contact Test Page</h1>
13+
</div>
14+
</t>
15+
</t>
16+
</field>
17+
</record>
18+
19+
<!-- English page -->
20+
<record id="test_contact_page_en" model="website.page">
21+
<field name="name">Contact Us</field>
22+
<field name="url">/test/contactus</field>
23+
<field name="is_published">True</field>
24+
<field name="view_id" ref="test_contact_page_view" />
25+
<field name="website_id" ref="website.default_website" />
26+
</record>
27+
28+
<!-- German page (separate translated version) -->
29+
<record id="test_contact_page_de" model="website.page">
30+
<field name="name">Kontakt</field>
31+
<field name="url">/test/kontakt</field>
32+
<field name="is_published">True</field>
33+
<field name="view_id" ref="test_contact_page_view" />
34+
<field name="website_id" ref="website.default_website" />
35+
</record>
36+
37+
<!-- English menu -->
38+
<record id="test_contact_menu" model="website.menu">
39+
<field name="name">Contact</field>
40+
<field name="url">/test/contactus</field>
41+
<field name="website_id" ref="website.default_website" />
42+
</record>
43+
</odoo>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import website
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from odoo import fields, models
2+
3+
4+
class WebsiteMenu(models.Model):
5+
_inherit = "website.menu"
6+
7+
url = fields.Char(translate=True)
8+
9+
10+
class WebsitePage(models.Model):
11+
_inherit = "website.page"
12+
13+
url = fields.Char("Page URL", translate=True)
14+
15+
def action_edit_page_form(self):
16+
"""Open the page in form view from tree."""
17+
self.ensure_one()
18+
return {
19+
"type": "ir.actions.act_window",
20+
"name": "Edit Website Page",
21+
"res_model": "website.page",
22+
"view_mode": "form",
23+
"res_id": self.id,
24+
"target": "current",
25+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import test_website

0 commit comments

Comments
 (0)