diff --git a/build/connect.js b/build/connect.js index b117d6c8e10..830be675402 100644 --- a/build/connect.js +++ b/build/connect.js @@ -45,6 +45,16 @@ module.exports = function (grunt) { middlewares.push(serveIndex(directory)); middlewares.push((req, res) => { + const pathname = req.url.split('?')[0]; + 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); + return; + } + } const path_404 = `${options.base[0]}/404.html`; if (grunt.file.exists(path_404)) { require('fs').createReadStream(path_404).pipe(res); diff --git a/src/javascript/_common/language.js b/src/javascript/_common/language.js index aab72e7c05f..402fc0970e1 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 (language && !Object.keys(all_languages).includes(language.toUpperCase())) { + language = 'en'; + } if (!custom_url) { url_lang = language; } @@ -57,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; }; diff --git a/src/javascript/app/base/binary_loader.js b/src/javascript/app/base/binary_loader.js index 63eb3411840..d689bc6c9c3 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,16 @@ const BinaryLoader = (() => { let active_script = null; const init = () => { + const supported_langs_regex = Object.keys(getAll()).map(lang => lang.toLowerCase()).join('|'); + const pathname = window.location.pathname; + const search = window.location.search; + 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 || ''}`; + 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' }); diff --git a/src/root_files/_common/404.html b/src/root_files/_common/404.html index e02881f591d..23e90ca83f6 100644 --- a/src/root_files/_common/404.html +++ b/src/root_files/_common/404.html @@ -11,18 +11,17 @@ } var langs = { - // TODO: uncomment if we enable de again - // 'de': 'de|at|li', + // 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', - // TODO: uncomment if we enable th again - // 'th': 'th', + 'th': 'th', 'vi': 'vn', 'zh_tw': 'tw|mo', 'zh_cn': 'cn', @@ -44,11 +43,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.toLowerCase())) { lang = 'en'; - window.location.href = base + '/' + lang.toLowerCase() + '/' + 'trading.html'; + window.location.href = base + '/' + lang + '/' + 'trading.html'; return; }