Skip to content

Commit

Permalink
Merge pull request #654 from maryia-deriv/maryia/DTRA-1323/redirect-t…
Browse files Browse the repository at this point in the history
…o-en-from-unsupp-langs

Maryia/DTRA-1323/Redirect to /en/ page when pathname contains an unsupported language
  • Loading branch information
balakrishna-deriv authored Jun 11, 2024
2 parents 4af07b1 + 2e1ca65 commit e510550
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
10 changes: 10 additions & 0 deletions build/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion src/javascript/_common/language.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
};
Expand Down
11 changes: 11 additions & 0 deletions src/javascript/app/base/binary_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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' });
Expand Down
21 changes: 10 additions & 11 deletions src/root_files/_common/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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;
}

Expand Down

0 comments on commit e510550

Please sign in to comment.