Skip to content

Commit

Permalink
fix: add fallback for CLDR dev use case (#2844)
Browse files Browse the repository at this point in the history
  • Loading branch information
pskelin authored Feb 23, 2021
1 parent b0db3f0 commit deb173a
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions packages/base/src/asset-registries/LocaleData.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const localeDataMap = new Map();
const loaders = new Map();
const cldrPromises = new Map();
const reportedErrors = new Set();
let warningShown = false;

const M_ISO639_OLD_TO_NEW = {
"iw": "he",
Expand All @@ -15,6 +16,13 @@ const M_ISO639_OLD_TO_NEW = {
"sh": "sr",
};

const _showAssetsWarningOnce = localeId => {
if (!warningShown) {
console.warn(`[LocaleData] Supported locale "${localeId}" not configured, import the "Assets.js" module from the webcomponents package you are using.`); /* eslint-disable-line */
warningShown = true;
}
};

const calcLocale = (language, region, script) => {
// normalize language and handle special cases
language = (language && M_ISO639_OLD_TO_NEW[language]) || language;
Expand All @@ -33,16 +41,32 @@ const calcLocale = (language, region, script) => {

// try language + region
let localeId = `${language}_${region}`;
if (!SUPPORTED_LOCALES.includes(localeId)) {
// fallback to language only
localeId = language;
if (SUPPORTED_LOCALES.includes(localeId)) {
if (loaders.has(localeId)) {
// supported and has loader
return localeId;
}

// supported, no loader - fallback to default and warn
_showAssetsWarningOnce(localeId);
return DEFAULT_LOCALE;
}
if (!SUPPORTED_LOCALES.includes(localeId)) {
// fallback to english
localeId = DEFAULT_LOCALE;

// not supported, try language only
localeId = language;
if (SUPPORTED_LOCALES.includes(localeId)) {
if (loaders.has(localeId)) {
// supported and has loader
return localeId;
}

// supported, no loader - fallback to default and warn
_showAssetsWarningOnce(localeId);
return DEFAULT_LOCALE;
}

return localeId;
// not supported - fallback to default locale
return DEFAULT_LOCALE;
};

// internal set data
Expand All @@ -52,6 +76,11 @@ const setLocaleData = (localeId, content) => {

// external getSync
const getLocaleData = localeId => {
// if there is no loader, the default fallback was fetched and a warning was given - use default locale instead
if (!loaders.has(localeId)) {
localeId = DEFAULT_LOCALE;
}

const content = localeDataMap.get(localeId);
if (!content) {
throw new Error(`CLDR data for locale ${localeId} is not loaded!`);
Expand Down

0 comments on commit deb173a

Please sign in to comment.