-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathgetLocaleData.ts
More file actions
30 lines (25 loc) · 1.21 KB
/
getLocaleData.ts
File metadata and controls
30 lines (25 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { fetchCldr } from "@ui5/webcomponents-base/dist/asset-registries/LocaleData.js";
import getLocale from "@ui5/webcomponents-base/dist/locale/getLocale.js";
import type LocaleT from "sap/ui/core/Locale";
import LocaleData from "../LocaleData.js";
const instances = new Map<string, LocaleData>();
/**
* Fetches and returns а LocaleData object for the required locale
* For more information on this object's API, please see:
* https://sdk.openui5.org/api/sap.ui.core.LocaleData
*
* @param { string } lang - if left empty, will use the configured/current locale
* @returns { Promise<LocaleData> }
*/
const getLocaleData = async (lang: string): Promise<LocaleData> => {
const locale = getLocale(lang);
const localeLang = locale.getLanguage();
if (!instances.has(localeLang)) {
await fetchCldr(locale.getLanguage(), locale.getRegion(), locale.getScript());
// @ts-expect-error - The LocaleData constructor expects a LocaleT, but we are passing a Locale. This is a known issue and can be ignored for now.
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
instances.set(localeLang, new LocaleData(locale as unknown as LocaleT));
}
return instances.get(localeLang)!;
};
export default getLocaleData;