if Key does n't present in the set language file, get it from the default language bundle.
Hi,
I've two language resource files(en_US, de_DE.json) & mentioned default language as "en_US" in the i18n configure file.
Configuration
i18n.configure({
locales:['en_US', 'de_DE'],
defaultLocale: 'en_US',
});
en_US.json { "name": "hello david", "age": 20 }
de_DE.json
{
"age": 30
}
I am setting a locale as "de_DE" using by setLocale method. i18n.setLocale('de_DE');
Translate Message ('name'); or req.('name');
Here the "name" key doesn't present in de_DE language file. we're expecting to get it from the default language file, because it was present in the en_US file & file/key not present in the application, it will get it from the default language file.
How do we achieve/get it? if Key does n't present in the set language file, get it from the default language bundle.
Please provide an answer with full information to us.
As a workaround, I modify the language object by merging the default language into it, for each language. Using lodash here.
function setFallbackLang(fallbackLang) {
const i18nCatalog = i18n.getCatalog();
_.each(i18nCatalog, (translations, lang) => {
i18nCatalog[lang] = _.mergeWith({}, i18nCatalog[fallbackLang], i18nCatalog[lang], function(value, srcValue, key, object, src) {
// use default language value if srcValue is undefined or empty string
if (srcValue === '')
return value;
});
});
}
i18n.configure(config.i18n);
setFallbackLang('en');
app.use(i18n.init);