Skip to content

Commit d605b81

Browse files
committed
Update GleapTranslationManager.js
1 parent 4e850c8 commit d605b81

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

src/GleapTranslationManager.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ export default class GleapTranslationManager {
3939
}
4040

4141
static translateText(key) {
42+
if (!key) {
43+
return "";
44+
}
45+
4246
const instance = GleapTranslationManager.getInstance();
4347

4448
var language = "en";
@@ -49,23 +53,33 @@ export default class GleapTranslationManager {
4953
language = instance.overrideLanguage.toLowerCase();
5054
}
5155

52-
var customTranslation = {};
53-
const translationKeys = Object.keys(instance.customTranslation);
54-
for (var i = 0; i < translationKeys.length; i++) {
55-
const translationKey = translationKeys[i];
56-
if (language && translationKey && language === translationKey.toLowerCase()) {
57-
if (instance.customTranslation[translationKey]) {
58-
customTranslation = instance.customTranslation[translationKey];
56+
const searchForTranslationTable = (langKey) => {
57+
var customTranslation = null;
58+
const translationKeys = Object.keys(instance.customTranslation);
59+
for (var i = 0; i < translationKeys.length; i++) {
60+
const translationKey = translationKeys[i];
61+
if (langKey && translationKey && langKey === translationKey.toLowerCase()) {
62+
if (instance.customTranslation[translationKey]) {
63+
customTranslation = instance.customTranslation[translationKey];
64+
}
5965
}
6066
}
61-
}
6267

63-
if (customTranslation[key]) {
64-
return customTranslation[key];
68+
return customTranslation;
6569
}
6670

67-
if (!key) {
68-
return "";
71+
var customTranslation = searchForTranslationTable(language);
72+
73+
// Extended search for language match only.
74+
if (!customTranslation && language) {
75+
const langKeys = language.split("-");
76+
if (langKeys && langKeys.length > 1) {
77+
customTranslation = searchForTranslationTable(langKeys[0]);
78+
}
79+
}
80+
81+
if (customTranslation && customTranslation[key]) {
82+
return customTranslation[key];
6983
}
7084

7185
return key;

0 commit comments

Comments
 (0)