From e18d2b2f8403703d21fd77b9f7bfa11b52c0d9cc Mon Sep 17 00:00:00 2001 From: Tomas Bures Date: Sun, 18 Nov 2018 07:05:27 +0100 Subject: [PATCH] Added fake language option to the client to ease localization --- client/src/lib/i18n.js | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/client/src/lib/i18n.js b/client/src/lib/i18n.js index 3f994196..c9ee794c 100644 --- a/client/src/lib/i18n.js +++ b/client/src/lib/i18n.js @@ -6,6 +6,43 @@ import {getUrl} from "./urls"; import commonEn from "../../../locales/common/en"; +function convertToFake(dict) { + function convertValueToFakeLang(str) { + let from = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+\\|`~[{]};:'\",<.>/?"; + let to = "ɐqɔpǝɟƃɥıɾʞʅɯuodbɹsʇnʌʍxʎz∀ԐↃᗡƎℲ⅁HIſӼ⅂WNOԀÒᴚS⊥∩ɅMX⅄Z0123456789¡@#$%ᵥ⅋⁎()-_=+\\|,~[{]};:,„´<.>/¿"; + + return str.replace(/(\{\{[^\}]+\}\}|%s)/g, '\x00\x04$1\x00').split('\x00').map(c => { + if (c.charAt(0) === '\x04') { + return c; + } + let r = ''; + for (let i = 0, len = c.length; i < len; i++) { + let pos = from.indexOf(c.charAt(i)); + if (pos < 0) { + r += c.charAt(i); + } else { + r += to.charAt(pos); + } + } + return r; + }).join('\x00').replace(/[\x00\x04]/g, ''); + } + + function _convertToFake(dict) { + for (const key in dict) { + const val = dict[key]; + + if (typeof val === 'string') { + dict[key] = convertValueToFakeLang(val); + } else { + _convertToFake(val); + } + } + } + + return _convertToFake(dict); +} + i18n .use(LanguageDetector) .init({ @@ -13,6 +50,9 @@ i18n resources: { en: { common: commonEn + }, + en_fake: { + common: convertToFake(commonEn) } }, @@ -27,6 +67,14 @@ i18n wait: true }, + detection: { + order: ['querystring', 'cookie', 'localStorage', 'navigator'], + lookupQuerystring: 'language', + lookupCookie: 'i18next', + lookupLocalStorage: 'i18nextLng', + caches: ['localStorage'] + }, + debug: true })