Skip to content

Commit f9b9ead

Browse files
authored
fix: fix JS error on setLanguage call (#2328)
When there is a component that uses CLDR and someone calls **setLanguage**, this causes the webcomponents to re-render and recalculate their state and it turns out that the CLDR for the newly set language is missing and JS error is thrown ("Error: CLDR data for locale de is not loaded"). Currently, this can be seen in our "RTL.html" page, if we add a DatePicker and then use the buttons on the page to change the language. This PR ensures that upon setLanguage, the CLDR is fetched/updated before the re-rendering. On the other hand, there is **applyDirection** API that also force re-rendering, that if used together with setLanguage, should wait for the setLanguage to complete to allow the message bundles (although they won't fail with JS error) and CLDR to load.
1 parent 72033bc commit f9b9ead

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { fetchJsonOnce } from "../util/FetchHelper.js";
2+
import { attachLanguageChange } from "../locale/languageChange.js";
3+
import getLocale from "../locale/getLocale.js";
24
import { getFeature } from "../FeaturesRegistry.js";
35
import { DEFAULT_LOCALE, SUPPORTED_LOCALES } from "../generated/AssetParameters.js";
46
import { getEffectiveAssetPath } from "../util/EffectiveAssetPath.js";
@@ -116,6 +118,13 @@ const _registerMappingFunction = mappingFn => {
116118
cldrMappingFn = mappingFn;
117119
};
118120

121+
// When the language changes dynamically (the user calls setLanguage),
122+
// re-fetch the required CDRD data.
123+
attachLanguageChange(() => {
124+
const locale = getLocale();
125+
return fetchCldr(locale.getLanguage(), locale.getRegion(), locale.getScript());
126+
});
127+
119128
export {
120129
fetchCldr,
121130
registerCldr,

packages/main/test/pages/RTL.html

+17-7
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
<ui5-checkbox text="This checkbox however defines dir=ltr" dir="ltr"></ui5-checkbox>
7676
</section>
7777

78-
78+
<ui5-date-picker></ui5-date-picker>
7979
<script>
8080

8181
// Utility function to change RTL and apply the changes
@@ -84,6 +84,18 @@
8484
window['sap-ui-webcomponents-bundle'].applyDirection();
8585
}
8686

87+
function setDirByLang(lang) {
88+
if (lang === "he" || lang === "ar") {
89+
setDir("rtl");
90+
} else {
91+
setDir("ltr");
92+
}
93+
}
94+
95+
function setLanguage(lang) {
96+
return window['sap-ui-webcomponents-bundle'].configuration.setLanguage(lang);
97+
}
98+
8799
document.getElementById("sw").addEventListener("click", function(e) {
88100
if (e.target.checked) {
89101
setDir("rtl");
@@ -94,12 +106,10 @@
94106

95107
document.getElementById("tb").addEventListener("selection-change", function(e) {
96108
var lang = e.detail.selectedButton.textContent.toLowerCase();
97-
if (lang === "he" || lang === "ar") {
98-
setDir("rtl");
99-
} else {
100-
setDir("ltr");
101-
}
102-
window['sap-ui-webcomponents-bundle'].configuration.setLanguage(lang);
109+
110+
setLanguage(lang).then(function() {
111+
setDirByLang(lang);
112+
});
103113
});
104114
</script>
105115

0 commit comments

Comments
 (0)