Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function loadTranslations(store, locale, defaultTranslations = {}) {
const parentLocale = locale.split('-')[0];
// react-intl provides things like pt-BR.
// lokalise provides things like pt_BR.
// so we have to translate '-' to '_' because the translation libraries
// don't know how to talk to each other. sheesh.
const region = locale.replace('-', '_');
// Update dir- and lang-attributes on the HTML element
// when the locale changes
document.documentElement.setAttribute('lang', parentLocale);
document.documentElement.setAttribute('dir', rtlDetect.getLangDir(locale));
// Set locale for Moment.js (en is not importable as it is not stored separately)
if (parentLocale === 'en') moment.locale(parentLocale);
else {
import(`moment/locale/${parentLocale}`).then(() => {
moment.locale(parentLocale);
}).catch(e => {
// eslint-disable-next-line no-console
console.error(`Error loading locale ${parentLocale} for Moment.js`, e);
});
}
return import(`react-intl/locale-data/${parentLocale}`)
.then(intlData => addLocaleData(intlData.default || intlData))
// fetch the region-specific translations, e.g. pt-BR, if available.
// fall back to the generic locale, e.g. pt, if not available.