Skip to content

Commit

Permalink
Support for special testing "xx" language
Browse files Browse the repository at this point in the history
  • Loading branch information
lslezak committed Aug 11, 2023
1 parent fff5ddf commit 2dd6a36
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
9 changes: 7 additions & 2 deletions web/src/L10nWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}
Expand Down
48 changes: 46 additions & 2 deletions web/src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,52 @@

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.
*
* @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
Expand All @@ -47,7 +85,13 @@ 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) => {
if (!isTestingLanguage())
return cockpit.ngettext(str1, strN, n);

const text = (n === 1) ? str1 : strN;
return xTranslate(text);
};

/**
* This is a no-op function, it can be used only for marking the text for
Expand Down

0 comments on commit 2dd6a36

Please sign in to comment.