From 92aee2732dcb38e5a5299bb31f298a8d11ada830 Mon Sep 17 00:00:00 2001 From: Pierre Paridans Date: Thu, 24 Sep 2020 12:50:33 +0000 Subject: [PATCH] [FIX] website_event_track: properly match custom lang in url In multilanguage website, URLs are automatically prefixed with the language (beside the default language) but only the default language-slug were supported (e.g. "en", "en_US"...). This commit supports also customized language-slug, which can actually be any string, for the ServiceWorker's scope. X-original-commit: 20eddda77d0ed6f2bb9b61d8ae4194e960ccc4ff --- .../static/src/js/website_event_pwa_widget.js | 29 ++++++------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/addons/website_event_track/static/src/js/website_event_pwa_widget.js b/addons/website_event_track/static/src/js/website_event_pwa_widget.js index 7953c9c132b..09a3f635417 100644 --- a/addons/website_event_track/static/src/js/website_event_pwa_widget.js +++ b/addons/website_event_track/static/src/js/website_event_pwa_widget.js @@ -79,32 +79,21 @@ odoo.define("website_event_track.website_event_pwa_widget", function (require) { // Private //-------------------------------------------------------------------------- - /** - * Returns the website's language from the URL - * - * Note: this method performs a matching against both the simple (eg. "en") and complete (eg. "en_US") - * language formatting in the url. This is required as the cookie always returns the complete - * form but the URL can use both, depending on the language (eg. "en_US" => "en" but "en_UK" => "en_UK"). - * @private - * @return {String} - */ - _getLangPrefix: function () { - var lang = utils.get_cookie("frontend_lang"); - var path = window.location.pathname; - var matches = path.match(new RegExp("^\/?(" + lang + "|" + lang.slice(0, 2) + ")\/")); - if (lang && matches && matches[1]) { - return "/" + matches[1]; - } - return ""; - }, - /** * Returns the PWA's scope + * + * Note: this method performs a matching to handle URLs with the language prefix. + * Typically this prefix is in the form of "en" or "en_US" but it can also be + * any string using the customization options in the Website's settings. * @private * @returns {String} */ _getScope: function () { - return this._getLangPrefix() + "/event"; + var matches = window.location.pathname.match(/^(\/(?:event|[^/]+\/event))\/?/); + if (matches && matches[1]) { + return matches[1]; + } + return "/event"; }, /**