@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
54 lines (53 loc) • 1.42 kB
JavaScript
/*!
* All material copyright ESRI, All Rights Reserved, unless otherwise specified.
* See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
* v1.5.0-next.4
*/
import { getAssetPath } from "@stencil/core";
import { dateFromISO } from "../../utils/date";
import { getSupportedLocale } from "../../utils/locale";
/**
* CLDR cache.
* Exported for testing purposes.
*
* @private
*/
export const translationCache = {};
/**
* CLDR request cache.
* Exported for testing purposes.
*
* @private
*/
export const requestCache = {};
/**
* Fetch calendar data for a given locale from list of supported languages
*
* @param lang
* @public
*/
export async function getLocaleData(lang) {
const locale = getSupportedLocale(lang);
if (translationCache[locale]) {
return translationCache[locale];
}
if (!requestCache[locale]) {
requestCache[locale] = fetch(getAssetPath(`./assets/date-picker/nls/${locale}.json`))
.then((resp) => resp.json())
.catch(() => {
console.error(`Translations for "${locale}" not found or invalid, falling back to english`);
return getLocaleData("en");
});
}
const data = await requestCache[locale];
translationCache[locale] = data;
return data;
}
/**
* Maps value to valueAsDate
*
* @param value
*/
export function getValueAsDateRange(value) {
return value.map((v, index) => dateFromISO(v, index === 1));
}