/*! * All material copyright ESRI, All Rights Reserved, unless otherwise specified. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details. * v1.0.0-beta.97 */ import { getAssetPath } from "@stencil/core"; import { getSupportedLocale } from "../../utils/locale"; import { dateFromISO } from "../../utils/date"; /** * 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)); }