Skip to content

Commit 83e9fe1

Browse files
authored
Refactor _() function to improve translation handling and case preservation (sugarlabs#4476)
* fix localization strings for motion-y and get-protein in Arabic * Refactor text cleaning and translation retrieval in utils.js * Fix comment formatting in getTranslationWithCase function
1 parent ed2c4e9 commit 83e9fe1

File tree

1 file changed

+34
-66
lines changed

1 file changed

+34
-66
lines changed

js/utils/utils.js

Lines changed: 34 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -71,82 +71,50 @@ function _(text) {
7171
}
7272

7373
try {
74-
// Replace certain characters from the input text
75-
const replace = [
76-
",",
77-
"(",
78-
")",
79-
"?",
80-
"¿",
81-
"<",
82-
">",
83-
".",
84-
"\n",
85-
'"',
86-
":",
87-
"%s",
88-
"%d",
89-
"/",
90-
"'",
91-
";",
92-
"×",
93-
"!",
94-
"¡"
95-
];
96-
97-
let replaced = text;
98-
99-
// to remove unwanted characters
100-
for (let p = 0; p < replace.length; p++) {
101-
replaced = replaced.split(replace[p]).join(""); // Efficient replacement
102-
}
103-
104-
//the replaced version is the version WITHOUT the unwanted characters.
105-
replaced = replaced.replace(/ /g, "-");
74+
// Remove unwanted characters using a single regex
75+
const unwantedChars = /[,\()?¿<>.\n":%s%d\/';×!¡]/g;
76+
const cleanedText = text.replace(unwantedChars, "").replace(/ /g, "-");
10677

10778
if (localStorage.kanaPreference === "kana") {
10879
const lang = document.webL10n.getLanguage();
10980
if (lang === "ja") {
110-
replaced = "kana-" + replaced;
81+
cleanedText = "kana-" + cleanedText;
11182
}
11283
}
11384

114-
// first, we actually tried to find out if there was an existing translation with SAME case
115-
let translated = document.webL10n.get(text);
116-
117-
// Takes, for example
118-
if ((!translated || translated === text) && replaced !== text) {
119-
translated = document.webL10n.get(replaced);
120-
}
121-
122-
// If still no translation is found, try the lowercase version
123-
if (!translated || translated === text) {
124-
translated = document.webL10n.get(text.toLowerCase());
125-
}
85+
// Helper function to get translation with case matching
86+
function getTranslationWithCase(inputText, caseType) {
87+
let translatedText = document.webL10n.get(inputText);
88+
if (!translatedText || translatedText === inputText) {
89+
return null; // No translation found
90+
}
91+
92+
switch (caseType) {
93+
case "upper":
94+
return translatedText.toUpperCase();
95+
case "lower":
96+
return translatedText.toLowerCase();
97+
case "title":
98+
return translatedText.charAt(0).toUpperCase() + translatedText.slice(1).toLowerCase();
99+
default:
100+
return translatedText;
101+
}
102+
}
126103

127-
//if still no translation is found, try the initial caps translation too
128-
if (!translated || translated === text) {
129-
const initialCaps = text.charAt(0).toUpperCase() + text.slice(1).toLowerCase();
130-
translated = document.webL10n.get(initialCaps);
131-
}
104+
// Try translations in order of priority
105+
const translations = [
106+
getTranslationWithCase(text, "original"), // Original case
107+
getTranslationWithCase(cleanedText, "original"), // Cleaned text
108+
getTranslationWithCase(text.toLowerCase(), "lower"), // Lowercase
109+
getTranslationWithCase(
110+
text.charAt(0).toUpperCase() + text.slice(1).toLowerCase(),
111+
"title" // Title case
112+
),
113+
];
132114

133-
//function returns the ORIGINAL case without any translation if no translation exists
134-
translated = translated || text;
135-
136-
// this if ensures Correct LETTER CASING, for example, "Search" -> "Buscar" and "SEARCH" -> "BUSCAR" and "search" -> "buscar"
137-
if (text === text.toUpperCase()) {
138-
//if the input is all uppercase, then we will return the translation in uppercase
139-
return translated.toUpperCase();
140-
} else if (text === text.toLowerCase()) {
141-
//if the input is all lowercase,then return the translation in lowercase
142-
return translated.toLowerCase();
143-
} else if (text.charAt(0).toUpperCase() + text.slice(1).toLowerCase() === text) {
144-
//if the input is in title case, then return the translation in title case
145-
return translated.charAt(0).toUpperCase() + translated.slice(1).toLowerCase();
146-
}
115+
const validTranslation = translations.find(t => t !== null);
116+
return validTranslation || text;
147117

148-
// return the translation as is if any of the case does not match
149-
return translated;
150118
} catch (e) {
151119
// console.debug("i18n error: " + text);
152120
return text;

0 commit comments

Comments
 (0)