Skip to content

Commit deb173a

Browse files
authored
fix: add fallback for CLDR dev use case (#2844)
1 parent b0db3f0 commit deb173a

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

packages/base/src/asset-registries/LocaleData.js

+36-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const localeDataMap = new Map();
77
const loaders = new Map();
88
const cldrPromises = new Map();
99
const reportedErrors = new Set();
10+
let warningShown = false;
1011

1112
const M_ISO639_OLD_TO_NEW = {
1213
"iw": "he",
@@ -15,6 +16,13 @@ const M_ISO639_OLD_TO_NEW = {
1516
"sh": "sr",
1617
};
1718

19+
const _showAssetsWarningOnce = localeId => {
20+
if (!warningShown) {
21+
console.warn(`[LocaleData] Supported locale "${localeId}" not configured, import the "Assets.js" module from the webcomponents package you are using.`); /* eslint-disable-line */
22+
warningShown = true;
23+
}
24+
};
25+
1826
const calcLocale = (language, region, script) => {
1927
// normalize language and handle special cases
2028
language = (language && M_ISO639_OLD_TO_NEW[language]) || language;
@@ -33,16 +41,32 @@ const calcLocale = (language, region, script) => {
3341

3442
// try language + region
3543
let localeId = `${language}_${region}`;
36-
if (!SUPPORTED_LOCALES.includes(localeId)) {
37-
// fallback to language only
38-
localeId = language;
44+
if (SUPPORTED_LOCALES.includes(localeId)) {
45+
if (loaders.has(localeId)) {
46+
// supported and has loader
47+
return localeId;
48+
}
49+
50+
// supported, no loader - fallback to default and warn
51+
_showAssetsWarningOnce(localeId);
52+
return DEFAULT_LOCALE;
3953
}
40-
if (!SUPPORTED_LOCALES.includes(localeId)) {
41-
// fallback to english
42-
localeId = DEFAULT_LOCALE;
54+
55+
// not supported, try language only
56+
localeId = language;
57+
if (SUPPORTED_LOCALES.includes(localeId)) {
58+
if (loaders.has(localeId)) {
59+
// supported and has loader
60+
return localeId;
61+
}
62+
63+
// supported, no loader - fallback to default and warn
64+
_showAssetsWarningOnce(localeId);
65+
return DEFAULT_LOCALE;
4366
}
4467

45-
return localeId;
68+
// not supported - fallback to default locale
69+
return DEFAULT_LOCALE;
4670
};
4771

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

5377
// external getSync
5478
const getLocaleData = localeId => {
79+
// if there is no loader, the default fallback was fetched and a warning was given - use default locale instead
80+
if (!loaders.has(localeId)) {
81+
localeId = DEFAULT_LOCALE;
82+
}
83+
5584
const content = localeDataMap.get(localeId);
5685
if (!content) {
5786
throw new Error(`CLDR data for locale ${localeId} is not loaded!`);

0 commit comments

Comments
 (0)