Skip to content

Commit c9253a6

Browse files
authored
feat(framework): create getLocaleData API (#1269)
1 parent e4c1dba commit c9253a6

File tree

11 files changed

+49
-20
lines changed

11 files changed

+49
-20
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import "../shims/Core-shim.js";
2-
import { getLanguage } from "../LocaleProvider.js";
2+
import getLocale from "../locale/getLocale.js";
33
import { fetchJsonOnce } from "../util/FetchHelper.js";
44
import { normalizeLocale, nextFallbackLocale } from "../util/normalizeLocale.js";
55

@@ -48,7 +48,7 @@ const fetchI18nBundle = async packageName => {
4848
return;
4949
}
5050

51-
const language = getLanguage();
51+
const language = getLocale().getLanguage();
5252

5353
let localeId = normalizeLocale(language);
5454
while (!bundlesForPackage[localeId]) {
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Locale from "./Locale.js";
2-
import detectNavigatorLanguage from "./util/detectNavigatorLanguage.js";
3-
import { getLanguage as getConfigLanguage } from "./config/Language.js";
2+
import detectNavigatorLanguage from "../util/detectNavigatorLanguage.js";
3+
import { getLanguage as getConfigLanguage } from "../config/Language.js";
44

55
const convertToLocaleOrNull = lang => {
66
try {
@@ -13,22 +13,19 @@ const convertToLocaleOrNull = lang => {
1313
};
1414

1515
/**
16-
* Returns the locale based on the configured language Configuration#getLanguage
16+
* Returns the locale based on the parameter or configured language Configuration#getLanguage
1717
* If no language has been configured - a new locale based on browser language is returned
1818
*/
19-
const getLocale = () => {
19+
const getLocale = lang => {
20+
if (lang) {
21+
return convertToLocaleOrNull(lang);
22+
}
23+
2024
if (getConfigLanguage()) {
2125
return new Locale(getConfigLanguage());
2226
}
2327

2428
return convertToLocaleOrNull(detectNavigatorLanguage());
2529
};
2630

27-
/**
28-
* Returns the language of #getLocale return value
29-
*/
30-
const getLanguage = () => {
31-
return getLocale().sLanguage;
32-
};
33-
34-
export { getLocale, getLanguage };
31+
export default getLocale;
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import LocaleData from "@ui5/webcomponents-utils/dist/sap/ui/core/LocaleData.js";
2+
import getLocale from "./getLocale.js";
3+
import { fetchCldr } from "../asset-registries/LocaleData.js";
4+
5+
const instances = new Map();
6+
7+
/**
8+
* Fetches and returns а LocaleData object for the required locale
9+
* For more information on this object's API, please see:
10+
* https://ui5.sap.com/#/api/sap.ui.core.LocaleData
11+
*
12+
* @param lang - if left empty, will use the configured/current locale
13+
* @returns {LocaleData}
14+
*/
15+
const getLocaleData = async lang => {
16+
const locale = getLocale(lang);
17+
const localeLang = locale.getLanguage();
18+
19+
if (!instances.has(localeLang)) {
20+
await fetchCldr(locale.getLanguage(), locale.getRegion(), locale.getScript());
21+
instances.set(localeLang, LocaleData.getInstance(locale));
22+
}
23+
24+
return instances.get(localeLang);
25+
};
26+
27+
export default getLocaleData;

packages/base/src/shims/Core-shim.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { inject as injectCore } from "@ui5/webcomponents-utils/dist/sap/ui/core/Core.js";
2-
import { getLocale } from "../LocaleProvider.js";
2+
import getLocale from "../locale/getLocale.js";
33
import { getLanguage } from "../config/Language.js";
44
import { getCalendarType } from "../config/CalendarType.js";
55
import getDesigntimePropertyAsArray from "../util/getDesigntimePropertyAsArray.js";

packages/main/bundle.esm.js

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import "@ui5/webcomponents-base/dist/features/calendar/Persian.js";
1212
// ESM bundle targets Edge + browsers with native support
1313
import "@ui5/webcomponents-base/dist/features/browsersupport/Edge.js";
1414

15+
// CLDR
16+
import getLocaleData from "@ui5/webcomponents-base/dist/locale/getLocaleData.js";
17+
1518
// Icons
1619
import "@ui5/webcomponents-icons/dist/Assets.js";
1720

@@ -69,6 +72,7 @@ import CustomListItem from "./dist/CustomListItem.js";
6972
import GroupHeaderListItem from "./dist/GroupHeaderListItem.js";
7073

7174

75+
7276
// used in test pages
7377
import RenderScheduler from "@ui5/webcomponents-base/dist/RenderScheduler.js";
7478
window.RenderScheduler = RenderScheduler;
@@ -93,4 +97,5 @@ window["sap-ui-webcomponents-bundle"] = {
9397
getFirstDayOfWeek,
9498
},
9599
getIconNames,
100+
getLocaleData,
96101
};

packages/main/src/Calendar.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
33
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
44
import { fetchCldr } from "@ui5/webcomponents-base/dist/asset-registries/LocaleData.js";
55
import { getCalendarType } from "@ui5/webcomponents-base/dist/config/CalendarType.js";
6-
import { getLocale } from "@ui5/webcomponents-base/dist/LocaleProvider.js";
6+
import getLocale from "@ui5/webcomponents-base/dist/locale/getLocale.js";
77
import DateFormat from "@ui5/webcomponents-utils/dist/sap/ui/core/format/DateFormat.js";
88
import LocaleData from "@ui5/webcomponents-utils/dist/sap/ui/core/LocaleData.js";
99
import CalendarDate from "@ui5/webcomponents-base/dist/dates/CalendarDate.js";

packages/main/src/DatePicker.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
33
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
44
import { fetchCldr } from "@ui5/webcomponents-base/dist/asset-registries/LocaleData.js";
55
import { getCalendarType } from "@ui5/webcomponents-base/dist/config/CalendarType.js";
6-
import { getLocale } from "@ui5/webcomponents-base/dist/LocaleProvider.js";
6+
import getLocale from "@ui5/webcomponents-base/dist/locale/getLocale.js";
77
import { getFeature } from "@ui5/webcomponents-base/dist/FeaturesRegistry.js";
88
import LocaleData from "@ui5/webcomponents-utils/dist/sap/ui/core/LocaleData.js";
99
import DateFormat from "@ui5/webcomponents-utils/dist/sap/ui/core/format/DateFormat.js";

packages/main/src/DayPicker.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
22
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
33
import { fetchCldr } from "@ui5/webcomponents-base/dist/asset-registries/LocaleData.js";
4-
import { getLocale } from "@ui5/webcomponents-base/dist/LocaleProvider.js";
4+
import getLocale from "@ui5/webcomponents-base/dist/locale/getLocale.js";
55
import { getFirstDayOfWeek } from "@ui5/webcomponents-base/dist/config/FormatSettings.js";
66
import DateFormat from "@ui5/webcomponents-utils/dist/sap/ui/core/format/DateFormat.js";
77
import { getCalendarType } from "@ui5/webcomponents-base/dist/config/CalendarType.js";

packages/main/src/MonthPicker.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import ItemNavigation from "@ui5/webcomponents-base/dist/delegate/ItemNavigation
66
import Integer from "@ui5/webcomponents-base/dist/types/Integer.js";
77
import { isSpace, isEnter } from "@ui5/webcomponents-base/dist/events/PseudoEvents.js";
88
import LocaleData from "@ui5/webcomponents-utils/dist/sap/ui/core/LocaleData.js";
9-
import { getLocale } from "@ui5/webcomponents-base/dist/LocaleProvider.js";
9+
import getLocale from "@ui5/webcomponents-base/dist/locale/getLocale.js";
1010
import CalendarType from "@ui5/webcomponents-base/dist/dates/CalendarType.js";
1111
import CalendarDate from "@ui5/webcomponents-base/dist/dates/CalendarDate.js";
1212
import ItemNavigationBehavior from "@ui5/webcomponents-base/dist/types/ItemNavigationBehavior.js";

packages/main/src/YearPicker.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import LocaleData from "@ui5/webcomponents-utils/dist/sap/ui/core/LocaleData.js"
44
import { getCalendarType } from "@ui5/webcomponents-base/dist/config/CalendarType.js";
55
import { isEnter, isSpace } from "@ui5/webcomponents-base/dist/events/PseudoEvents.js";
66
import ItemNavigation from "@ui5/webcomponents-base/dist/delegate/ItemNavigation.js";
7-
import { getLocale } from "@ui5/webcomponents-base/dist/LocaleProvider.js";
7+
import getLocale from "@ui5/webcomponents-base/dist/locale/getLocale.js";
88
import Integer from "@ui5/webcomponents-base/dist/types/Integer.js";
99
import DateFormat from "@ui5/webcomponents-utils/dist/sap/ui/core/format/DateFormat.js";
1010
import CalendarType from "@ui5/webcomponents-base/dist/dates/CalendarType.js";

0 commit comments

Comments
 (0)