Skip to content

Commit

Permalink
add fixes for invalid localization (#7564)
Browse files Browse the repository at this point in the history
Co-authored-by: PhotoNomad0 <bruce.mclean@unfoldingword.org>
  • Loading branch information
PhotoNomad0 and PhotoNomad0 committed Mar 9, 2024
1 parent ccdbe5f commit 04921d4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
"string-punctuation-tokenizer": "^2.2.0",
"sudo-prompt": "6.2.1",
"tc-electron-env": "0.10.0",
"tc-source-content-updater": "1.4.22",
"tc-source-content-updater": "1.4.23",
"tc-strings": "0.1.7",
"tc-tool": "4.1.0",
"tc-ui-toolkit": "6.2.8",
Expand Down
20 changes: 14 additions & 6 deletions src/js/actions/LocaleActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export const loadLocalization = (localeDir, appLanguage = null, initialize, addT
if (!file.endsWith('.json')) {
// don't warn if readme or NonTranslatable.js
if (!file.endsWith('.md') && !file.endsWith('.js')) {
console.warn(`Skipping invalid localization file ${file}`);
console.warn(`loadLocalization() - Skipping invalid localization file ${file}`);
}
continue;
}
Expand All @@ -148,7 +148,7 @@ export const loadLocalization = (localeDir, appLanguage = null, initialize, addT
translations[shortLangCode] = translation;
}
} catch (e) {
console.error(`Failed to load localization ${localeFile}: ${e}`);
console.error(`loadLocalization() - Failed to load localization ${localeFile}: ${e}`);
}
}
return Promise.resolve({ languages, translations });
Expand All @@ -172,9 +172,17 @@ export const loadLocalization = (localeDir, appLanguage = null, initialize, addT
let languageCode = appLanguage;

if (!translations[languageCode] && languageCode) {
console.log(`loadLocalization() - No exact match found for ${languageCode}`);
const shortLocale = languageCode.split('_')[0];
const equivalentLocale = translations[shortLocale]['_']['locale'];
languageCode = equivalentLocale;
const equivalentLocale = translations[shortLocale]?.['_']?.['locale'];

if (equivalentLocale) {
console.log(`loadLocalization() - Falling back to ${equivalentLocale}`);
languageCode = equivalentLocale;
} else {
languageCode = 'en'; // default to `en` if shortLocale match not found
console.log(`loadLocalization() - No short match found for ${shortLocale}, Falling back to ${languageCode}`);
}
}

if (languageCode) {
Expand All @@ -190,7 +198,7 @@ export const loadLocalization = (localeDir, appLanguage = null, initialize, addT

if (appLanguage) {
// set selected locale
console.log(`Saved locale: ${appLanguage}`);
console.log(`loadLocalization() - Saved locale: ${appLanguage}`);

if (!setActiveLanguageSafely(dispatch, appLanguage, languages, translations, setActiveLanguage, addTranslationForLanguage)) {
// fall back to system locale
Expand All @@ -203,7 +211,7 @@ export const loadLocalization = (localeDir, appLanguage = null, initialize, addT
}).then(() => {
dispatch(setLocaleLoaded());
}).catch(err => {
console.log('Failed to initialize localization', err);
console.error('loadLocalization() - Failed to initialize localization', err);
});
};

Expand Down

0 comments on commit 04921d4

Please sign in to comment.