Skip to content

Commit ea748ae

Browse files
authored
enhancement: utils.JS Translation Method (#4265)
* enhancement/utilsJSTranslationMethod * removed extra spacings
1 parent acc63a2 commit ea748ae

File tree

1 file changed

+77
-40
lines changed

1 file changed

+77
-40
lines changed

js/utils/utils.js

Lines changed: 77 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -58,58 +58,95 @@ const changeImage = (imgElement, from, to) => {
5858
};
5959

6060
/**
61-
* A simple localization function for translating strings.
61+
* Enhanced _() method to handle case variations for translations
62+
* prioritize exact matches and preserve the case of the input text.
6263
* @function
6364
* @param {string} text - The input text to be translated.
6465
* @returns {string} The translated text.
6566
*/
6667
function _(text) {
67-
if (text === null) {
68+
if (text === null || text === undefined) {
6869
// console.debug("null string passed to _");
6970
return "";
7071
}
7172

72-
let replaced = text;
73-
const replace = [
74-
",",
75-
"(",
76-
")",
77-
"?",
78-
"¿",
79-
"<",
80-
">",
81-
".",
82-
"\n",
83-
'"',
84-
":",
85-
"%s",
86-
"%d",
87-
"/",
88-
"'",
89-
";",
90-
"×",
91-
"!",
92-
"¡"
93-
];
94-
for (let p = 0; p < replace.length; p++) {
95-
replaced = replaced.replace(replace[p], "");
96-
}
97-
98-
replaced = replaced.replace(/ /g, "-");
99-
100-
if (localStorage.kanaPreference === "kana") {
101-
const lang = document.webL10n.getLanguage();
102-
if (lang === "ja") {
103-
replaced = "kana-" + replaced;
73+
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
104102
}
105-
}
103+
104+
//the replaced version is the version WITHOUT the unwanted characters.
105+
replaced = replaced.replace(/ /g, "-");
106106

107-
try {
108-
let translation = document.webL10n.get(replaced);
109-
if (translation === "") {
110-
translation = text;
107+
if (localStorage.kanaPreference === "kana") {
108+
const lang = document.webL10n.getLanguage();
109+
if (lang === "ja") {
110+
replaced = "kana-" + replaced;
111+
}
112+
}
113+
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);
111120
}
112-
return translation;
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+
}
126+
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+
}
132+
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+
}
147+
148+
// return the translation as is if any of the case does not match
149+
return translated;
113150
} catch (e) {
114151
// console.debug("i18n error: " + text);
115152
return text;

0 commit comments

Comments
 (0)