Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for special testing "xx" language #702

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
46 changes: 44 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,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
Expand Down
Loading