From ad6ca301dfbebb4660a2e146eef042390256de7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ladislav=20Slez=C3=A1k?= Date: Fri, 11 Aug 2023 16:11:04 +0200 Subject: [PATCH] Support for special testing "xx" language --- web/src/L10nWrapper.jsx | 9 ++++++-- web/src/i18n.js | 46 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/web/src/L10nWrapper.jsx b/web/src/L10nWrapper.jsx index b2a0e2ed7a..8f35bab478 100644 --- a/web/src/L10nWrapper.jsx +++ b/web/src/L10nWrapper.jsx @@ -19,7 +19,7 @@ * find current contact information at www.suse.com. */ -// import React from "react"; +import cockpit from "./lib/cockpit"; /** * Helper function for storing the Cockpit language. @@ -60,7 +60,12 @@ export default function L10nWrapper({ children }) { if (langQuery) { // convert "pt_BR" to Cockpit compatible "pt-br" langQuery = langQuery.toLowerCase().replace("_", "-"); - if (langCookie !== langQuery) { + + // special handling for the testing "xx" language + if (langQuery === "xx" || langQuery === "xx-xx") { + // just activate the language, there are no real translations to load + cockpit.language = "xx"; + } else if (langCookie !== langQuery) { setLang(langQuery); reload(); } diff --git a/web/src/i18n.js b/web/src/i18n.js index c3bbd5d77e..2850a29277 100644 --- a/web/src/i18n.js +++ b/web/src/i18n.js @@ -27,6 +27,44 @@ import cockpit from "./lib/cockpit"; +/** + * Tests whether a special testing language is used. + * + * @returns {boolean} true if the testing language is set + */ +const isTestingLanguage = () => cockpit.language === "xx"; + +/** + * "Translate" the string to special "xx" testing language. + * It just replaces all alpha characters with "x". + * It keeps the percent placeholders like "%s" or "%d" unmodified. + * + * @param {string} str input string + * @returns {string} "translated" string + */ +const xTranslate = (str) => { + let result = ""; + + let wasPercent = false; + for (let index = 0; index < str.length; index++) { + const char = str[index]; + + if (wasPercent) { + result += char; + wasPercent = false; + } else { + if (char === "%") { + result += char; + wasPercent = true; + } else { + result += char.replace(/[a-z]/, "x").replace(/[A-Z]/, "X"); + } + } + } + + return result; +}; + /** * Returns a translated text in the current locale or the original text if the * translation is not found. @@ -34,7 +72,7 @@ import cockpit from "./lib/cockpit"; * @param {string} str the input string to translate * @return {string} translated or original text */ -const _ = (str) => cockpit.gettext(str); +const _ = (str) => isTestingLanguage() ? xTranslate(str) : cockpit.gettext(str); /** * Similar to the _() function. This variant returns singular or plural form @@ -47,7 +85,11 @@ const _ = (str) => cockpit.gettext(str); * singular or plural form * @return {string} translated or original text */ -const n_ = (str1, strN, n) => cockpit.ngettext(str1, strN, n); +const n_ = (str1, strN, n) => { + return isTestingLanguage() + ? xTranslate((n === 1) ? str1 : strN) + : cockpit.ngettext(str1, strN, n); +}; /** * This is a no-op function, it can be used only for marking the text for