From bacbeb0c40396e62b44d5ec5b94826e42ec6c349 Mon Sep 17 00:00:00 2001 From: maryia-deriv Date: Mon, 3 Jun 2024 22:36:28 +0300 Subject: [PATCH 01/10] fix: open EN page when lang is unsupported --- build/connect.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/build/connect.js b/build/connect.js index b117d6c8e10..cc4bd8b23d7 100644 --- a/build/connect.js +++ b/build/connect.js @@ -45,6 +45,12 @@ module.exports = function (grunt) { middlewares.push(serveIndex(directory)); middlewares.push((req, res) => { + if (new RegExp(`/(?!${lang_regex})\\w{2}/\\w+.html`, 'g').test(req.url)) { + const en_pathname = req.url.split('?')[0].replace(new RegExp(`/(?!${lang_regex})\\w{2}/`), '/en/'); + const en_file_path = `${options.base[0]}${en_pathname}`; + require('fs').createReadStream(en_file_path).pipe(res); + return; + } const path_404 = `${options.base[0]}/404.html`; if (grunt.file.exists(path_404)) { require('fs').createReadStream(path_404).pipe(res); From 095462514fbd89aba0b681719ecfe7d85cca1975 Mon Sep 17 00:00:00 2001 From: maryia-deriv Date: Mon, 3 Jun 2024 23:20:02 +0300 Subject: [PATCH 02/10] feat: redirect to /en/ page if pathname contains an unsupported language --- build/connect.js | 2 +- src/javascript/_common/language.js | 5 ++++- src/javascript/app/base/binary_loader.js | 8 ++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/build/connect.js b/build/connect.js index cc4bd8b23d7..f93a5b36cf0 100644 --- a/build/connect.js +++ b/build/connect.js @@ -46,7 +46,7 @@ module.exports = function (grunt) { middlewares.push((req, res) => { if (new RegExp(`/(?!${lang_regex})\\w{2}/\\w+.html`, 'g').test(req.url)) { - const en_pathname = req.url.split('?')[0].replace(new RegExp(`/(?!${lang_regex})\\w{2}/`), '/en/'); + const en_pathname = req.url.split('?')[0].replace(new RegExp(`/(?!${lang_regex})\\w{2}/`), '/en/'); const en_file_path = `${options.base[0]}${en_pathname}`; require('fs').createReadStream(en_file_path).pipe(res); return; diff --git a/src/javascript/_common/language.js b/src/javascript/_common/language.js index aab72e7c05f..72262cd9327 100644 --- a/src/javascript/_common/language.js +++ b/src/javascript/_common/language.js @@ -37,7 +37,10 @@ const Language = (() => { const languageFromUrl = (custom_url) => { if (url_lang && !custom_url) return url_lang; const url_params = (custom_url || window.location.href).split('/').slice(3); - const language = (url_params.find(lang => lang_regex.test(lang)) || ''); + let language = (url_params.find(lang => lang_regex.test(lang)) || ''); + if (!Object.keys(all_languages).includes(language.toUpperCase())) { + language = 'en'; + } if (!custom_url) { url_lang = language; } diff --git a/src/javascript/app/base/binary_loader.js b/src/javascript/app/base/binary_loader.js index 63eb3411840..937234c19a4 100644 --- a/src/javascript/app/base/binary_loader.js +++ b/src/javascript/app/base/binary_loader.js @@ -11,6 +11,7 @@ const GTM = require('../../_common/base/gtm'); const Login = require('../../_common/base/login'); const LiveChat = require('../../_common/base/livechat'); const getElementById = require('../../_common/common_functions').getElementById; +const getAll = require('../../_common/language').getAll; const urlLang = require('../../_common/language').urlLang; const localizeForLang = require('../../_common/localize').forLang; const localize = require('../../_common/localize').localize; @@ -25,6 +26,13 @@ const BinaryLoader = (() => { let active_script = null; const init = () => { + const supported_langs_regex = Object.keys(getAll()).map(lang => lang.toLowerCase()).join('|'); + // redirect to /en/ page if pathname contains an unsupported language: + if (new RegExp(`/(?!${supported_langs_regex})\\w{2}/\\w+.html`, 'g').test(window.location.pathname)) { + const en_pathname = window.location.pathname.split('?')[0].replace(new RegExp(`/(?!${supported_langs_regex})\\w{2}/`), '/en/'); + const en_href = `${window.location.origin}${en_pathname}`; + window.history.replaceState({ url: en_href }, document.title, en_href); + } if (!isStorageSupported(localStorage) || !isStorageSupported(sessionStorage)) { Header.displayNotification({ key: 'storage_not_supported', title: 'Storage not supported', message: localize('Requires your browser\'s web storage to be enabled in order to function properly. Please enable it or exit private browsing mode.'), type: 'danger' }); From 3e1f884bbdf4e51aa3d99745186e47bffdc8320c Mon Sep 17 00:00:00 2001 From: maryia-deriv Date: Mon, 3 Jun 2024 23:26:28 +0300 Subject: [PATCH 03/10] test: fix urlLang --- src/javascript/_common/language.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/javascript/_common/language.js b/src/javascript/_common/language.js index 72262cd9327..e7402173cd8 100644 --- a/src/javascript/_common/language.js +++ b/src/javascript/_common/language.js @@ -38,7 +38,7 @@ const Language = (() => { if (url_lang && !custom_url) return url_lang; const url_params = (custom_url || window.location.href).split('/').slice(3); let language = (url_params.find(lang => lang_regex.test(lang)) || ''); - if (!Object.keys(all_languages).includes(language.toUpperCase())) { + if (language && !Object.keys(all_languages).includes(language.toUpperCase())) { language = 'en'; } if (!custom_url) { From 083a866f91873716fe6e5042b2472cad3cad8035 Mon Sep 17 00:00:00 2001 From: maryia-deriv Date: Tue, 4 Jun 2024 10:27:48 +0300 Subject: [PATCH 04/10] fix: allow 5 symbols as language --- build/connect.js | 7 ++++--- src/javascript/app/base/binary_loader.js | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/build/connect.js b/build/connect.js index f93a5b36cf0..20345641695 100644 --- a/build/connect.js +++ b/build/connect.js @@ -45,9 +45,10 @@ module.exports = function (grunt) { middlewares.push(serveIndex(directory)); middlewares.push((req, res) => { - if (new RegExp(`/(?!${lang_regex})\\w{2}/\\w+.html`, 'g').test(req.url)) { - const en_pathname = req.url.split('?')[0].replace(new RegExp(`/(?!${lang_regex})\\w{2}/`), '/en/'); - const en_file_path = `${options.base[0]}${en_pathname}`; + const pathname = req.url.split('?')[0]; + if (new RegExp(`/(?!${lang_regex})\\w{2,5}/\\w+`, 'g').test(pathname)) { + const en_pathname = pathname.replace(new RegExp(`/(?!${lang_regex})\\w{2,5}/`), '/en/'); + const en_file_path = `${options.base[0]}${en_pathname}${req.url.split('?')[1] || ''}`; require('fs').createReadStream(en_file_path).pipe(res); return; } diff --git a/src/javascript/app/base/binary_loader.js b/src/javascript/app/base/binary_loader.js index 937234c19a4..7335352181c 100644 --- a/src/javascript/app/base/binary_loader.js +++ b/src/javascript/app/base/binary_loader.js @@ -28,9 +28,9 @@ const BinaryLoader = (() => { const init = () => { const supported_langs_regex = Object.keys(getAll()).map(lang => lang.toLowerCase()).join('|'); // redirect to /en/ page if pathname contains an unsupported language: - if (new RegExp(`/(?!${supported_langs_regex})\\w{2}/\\w+.html`, 'g').test(window.location.pathname)) { - const en_pathname = window.location.pathname.split('?')[0].replace(new RegExp(`/(?!${supported_langs_regex})\\w{2}/`), '/en/'); - const en_href = `${window.location.origin}${en_pathname}`; + if (new RegExp(`/(?!${supported_langs_regex})\\w{2,5}/\\w+`, 'g').test(window.location.pathname)) { + const en_pathname = window.location.pathname.replace(new RegExp(`/(?!${supported_langs_regex})\\w{2,5}/`), '/en/'); + const en_href = `${window.location.origin}${en_pathname}${window.location.search || ''}`; window.history.replaceState({ url: en_href }, document.title, en_href); } From a96693d9baa5dc787141b7daf280d24412758aa6 Mon Sep 17 00:00:00 2001 From: maryia-deriv Date: Tue, 4 Jun 2024 10:46:00 +0300 Subject: [PATCH 05/10] fix: allow 5 symbols as language and keep search params --- build/connect.js | 4 ++-- src/javascript/app/base/binary_loader.js | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/build/connect.js b/build/connect.js index 20345641695..bf97334e719 100644 --- a/build/connect.js +++ b/build/connect.js @@ -46,9 +46,9 @@ module.exports = function (grunt) { middlewares.push((req, res) => { const pathname = req.url.split('?')[0]; - if (new RegExp(`/(?!${lang_regex})\\w{2,5}/\\w+`, 'g').test(pathname)) { + if (new RegExp(`^/(?!${lang_regex})\\w{2,5}/\\w+`).test(pathname)) { const en_pathname = pathname.replace(new RegExp(`/(?!${lang_regex})\\w{2,5}/`), '/en/'); - const en_file_path = `${options.base[0]}${en_pathname}${req.url.split('?')[1] || ''}`; + const en_file_path = `${options.base[0]}${en_pathname}`; require('fs').createReadStream(en_file_path).pipe(res); return; } diff --git a/src/javascript/app/base/binary_loader.js b/src/javascript/app/base/binary_loader.js index 7335352181c..f9704b159d3 100644 --- a/src/javascript/app/base/binary_loader.js +++ b/src/javascript/app/base/binary_loader.js @@ -28,9 +28,11 @@ const BinaryLoader = (() => { const init = () => { const supported_langs_regex = Object.keys(getAll()).map(lang => lang.toLowerCase()).join('|'); // redirect to /en/ page if pathname contains an unsupported language: - if (new RegExp(`/(?!${supported_langs_regex})\\w{2,5}/\\w+`, 'g').test(window.location.pathname)) { - const en_pathname = window.location.pathname.replace(new RegExp(`/(?!${supported_langs_regex})\\w{2,5}/`), '/en/'); - const en_href = `${window.location.origin}${en_pathname}${window.location.search || ''}`; + const pathname = window.location.pathname; + const search = window.location.search; + if (new RegExp(`^/(?!${supported_langs_regex})\\w{2,5}/\\w+`).test(pathname)) { + const en_pathname = pathname.replace(new RegExp(`^/(?!${supported_langs_regex})\\w{2,5}/`), '/en/'); + const en_href = `${window.location.origin}${en_pathname}${search ? `?${search}` : ''}`; window.history.replaceState({ url: en_href }, document.title, en_href); } From fd0a4d50818680733748d8ddde83b394f88fc4a1 Mon Sep 17 00:00:00 2001 From: maryia-deriv Date: Tue, 4 Jun 2024 10:59:28 +0300 Subject: [PATCH 06/10] fix: align flag icon with the lang in the url --- build/connect.js | 6 ++++-- src/javascript/_common/language.js | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/build/connect.js b/build/connect.js index bf97334e719..a9acd6ed4b2 100644 --- a/build/connect.js +++ b/build/connect.js @@ -49,8 +49,10 @@ module.exports = function (grunt) { if (new RegExp(`^/(?!${lang_regex})\\w{2,5}/\\w+`).test(pathname)) { const en_pathname = pathname.replace(new RegExp(`/(?!${lang_regex})\\w{2,5}/`), '/en/'); const en_file_path = `${options.base[0]}${en_pathname}`; - require('fs').createReadStream(en_file_path).pipe(res); - return; + if (grunt.file.exists(en_file_path)) { + require('fs').createReadStream(en_file_path).pipe(res); + return; + } } const path_404 = `${options.base[0]}/404.html`; if (grunt.file.exists(path_404)) { diff --git a/src/javascript/_common/language.js b/src/javascript/_common/language.js index e7402173cd8..402fc0970e1 100644 --- a/src/javascript/_common/language.js +++ b/src/javascript/_common/language.js @@ -60,6 +60,9 @@ const Language = (() => { } } } + if (current_lang && languageFromUrl() && current_lang !== languageFromUrl()) { + current_lang = languageFromUrl(); + } current_lang = (current_lang || (languageFromUrl() || Cookies.get('language') || default_language).toUpperCase()); return current_lang; }; From b43c3d59ec9d280cec5f39f9ecaa0736cd788e7a Mon Sep 17 00:00:00 2001 From: maryia-deriv Date: Tue, 4 Jun 2024 12:04:56 +0300 Subject: [PATCH 07/10] refactor: pattern --- build/connect.js | 5 +++-- src/javascript/app/base/binary_loader.js | 7 ++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/build/connect.js b/build/connect.js index a9acd6ed4b2..830be675402 100644 --- a/build/connect.js +++ b/build/connect.js @@ -46,8 +46,9 @@ module.exports = function (grunt) { middlewares.push((req, res) => { const pathname = req.url.split('?')[0]; - if (new RegExp(`^/(?!${lang_regex})\\w{2,5}/\\w+`).test(pathname)) { - const en_pathname = pathname.replace(new RegExp(`/(?!${lang_regex})\\w{2,5}/`), '/en/'); + const pattern = `/(?!${lang_regex})\\w{2,5}/`; + if (new RegExp(`^${pattern}\\w+`).test(pathname)) { + const en_pathname = pathname.replace(new RegExp(pattern), '/en/'); const en_file_path = `${options.base[0]}${en_pathname}`; if (grunt.file.exists(en_file_path)) { require('fs').createReadStream(en_file_path).pipe(res); diff --git a/src/javascript/app/base/binary_loader.js b/src/javascript/app/base/binary_loader.js index f9704b159d3..3d8825c9b99 100644 --- a/src/javascript/app/base/binary_loader.js +++ b/src/javascript/app/base/binary_loader.js @@ -27,11 +27,12 @@ const BinaryLoader = (() => { const init = () => { const supported_langs_regex = Object.keys(getAll()).map(lang => lang.toLowerCase()).join('|'); - // redirect to /en/ page if pathname contains an unsupported language: const pathname = window.location.pathname; const search = window.location.search; - if (new RegExp(`^/(?!${supported_langs_regex})\\w{2,5}/\\w+`).test(pathname)) { - const en_pathname = pathname.replace(new RegExp(`^/(?!${supported_langs_regex})\\w{2,5}/`), '/en/'); + const pattern = `/(?!${supported_langs_regex})\\w{2,5}/`; + // redirect to /en/ page if pathname contains an unsupported language: + if (new RegExp(`^${pattern}\\w+`).test(pathname)) { + const en_pathname = pathname.replace(new RegExp(pattern), '/en/'); const en_href = `${window.location.origin}${en_pathname}${search ? `?${search}` : ''}`; window.history.replaceState({ url: en_href }, document.title, en_href); } From c838408f4b58d36dd259a88e5b3432b69b21bb5b Mon Sep 17 00:00:00 2001 From: maryia-deriv Date: Tue, 4 Jun 2024 12:15:04 +0300 Subject: [PATCH 08/10] fix: search params include ? mark --- src/javascript/app/base/binary_loader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/javascript/app/base/binary_loader.js b/src/javascript/app/base/binary_loader.js index 3d8825c9b99..d689bc6c9c3 100644 --- a/src/javascript/app/base/binary_loader.js +++ b/src/javascript/app/base/binary_loader.js @@ -33,7 +33,7 @@ const BinaryLoader = (() => { // redirect to /en/ page if pathname contains an unsupported language: if (new RegExp(`^${pattern}\\w+`).test(pathname)) { const en_pathname = pathname.replace(new RegExp(pattern), '/en/'); - const en_href = `${window.location.origin}${en_pathname}${search ? `?${search}` : ''}`; + const en_href = `${window.location.origin}${en_pathname}${search || ''}`; window.history.replaceState({ url: en_href }, document.title, en_href); } From 350af1e6d0434ef4d7895f1bec946ce346dfd915 Mon Sep 17 00:00:00 2001 From: maryia-deriv Date: Thu, 6 Jun 2024 17:05:59 +0300 Subject: [PATCH 09/10] fix: for non-localhost links --- src/root_files/_common/404.html | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/root_files/_common/404.html b/src/root_files/_common/404.html index e02881f591d..ab525eef3f2 100644 --- a/src/root_files/_common/404.html +++ b/src/root_files/_common/404.html @@ -11,8 +11,7 @@ } var langs = { - // TODO: uncomment if we enable de again - // 'de': 'de|at|li', + 'de': 'de|at|li', 'es': 'ar|bo|cl|co|cr|cu|do|ec|sv|gt|hn|mx|ni|pa|py|pr|es|uy|ve', 'fr': 'fr|ad|bj|bf|cf|cg|ga|gn|ml|mc|ne|sn|tg', 'id': 'id', @@ -21,8 +20,7 @@ 'pl': 'po', 'pt': 'br|mz|ao|pt|gw|pg|cv|st', 'ru': 'ru|ua|by|kz', - // TODO: uncomment if we enable th again - // 'th': 'th', + 'th': 'th', 'vi': 'vn', 'zh_tw': 'tw|mo', 'zh_cn': 'cn', @@ -44,11 +42,11 @@ function redirect(lang) { var base = /^(.+?).github.io$/.test(window.location.hostname) ? window.location.origin + '/binary-static' : window.location.origin; - // TODO: REMOVE/CHANGE this after addition of DE or TH languages - var unsupported_languages = ['de', 'th']; - if (unsupported_languages.indexOf(lang.toLowerCase()) >= 0) { + // redirect to /en/ page if pathname contains an unsupported language: + const supported_langs_regex = Object.keys(langs).join('|'); + if (new RegExp(`^/(?!${supported_langs_regex})\\w{2,5}/\\w+`).test(window.location.pathname)) { lang = 'en'; - window.location.href = base + '/' + lang.toLowerCase() + '/' + 'trading.html'; + window.location.href = base + '/' + lang + '/' + 'trading.html'; return; } From 2e1ca651ec6deeab07e6ab4cdaae90ca7c79b750 Mon Sep 17 00:00:00 2001 From: maryia-deriv Date: Fri, 7 Jun 2024 15:11:21 +0300 Subject: [PATCH 10/10] fix: redirect to /en/ page from 404 for any unsupported langs + work with any lang case --- src/root_files/_common/404.html | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/root_files/_common/404.html b/src/root_files/_common/404.html index ab525eef3f2..23e90ca83f6 100644 --- a/src/root_files/_common/404.html +++ b/src/root_files/_common/404.html @@ -11,14 +11,15 @@ } var langs = { + // TODO: uncomment if we enable id, ko and pt languages + // 'id': 'id', + // 'ko': 'kr', + // 'pt': 'br|mz|ao|pt|gw|pg|cv|st', 'de': 'de|at|li', 'es': 'ar|bo|cl|co|cr|cu|do|ec|sv|gt|hn|mx|ni|pa|py|pr|es|uy|ve', 'fr': 'fr|ad|bj|bf|cf|cg|ga|gn|ml|mc|ne|sn|tg', - 'id': 'id', 'it': 'it', - 'ko': 'kr', 'pl': 'po', - 'pt': 'br|mz|ao|pt|gw|pg|cv|st', 'ru': 'ru|ua|by|kz', 'th': 'th', 'vi': 'vn', @@ -44,7 +45,7 @@ // redirect to /en/ page if pathname contains an unsupported language: const supported_langs_regex = Object.keys(langs).join('|'); - if (new RegExp(`^/(?!${supported_langs_regex})\\w{2,5}/\\w+`).test(window.location.pathname)) { + if (new RegExp(`^/(?!${supported_langs_regex})\\w{2,5}/\\w+`).test(window.location.pathname.toLowerCase())) { lang = 'en'; window.location.href = base + '/' + lang + '/' + 'trading.html'; return;