utils.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.97
  5. */
  6. import { getAssetPath } from "@stencil/core";
  7. import { getSupportedLocale } from "../../utils/locale";
  8. import { dateFromISO } from "../../utils/date";
  9. /**
  10. * CLDR cache.
  11. * Exported for testing purposes.
  12. *
  13. * @private
  14. */
  15. export const translationCache = {};
  16. /**
  17. * CLDR request cache.
  18. * Exported for testing purposes.
  19. *
  20. * @private
  21. */
  22. export const requestCache = {};
  23. /**
  24. * Fetch calendar data for a given locale from list of supported languages
  25. *
  26. * @param lang
  27. * @public
  28. */
  29. export async function getLocaleData(lang) {
  30. const locale = getSupportedLocale(lang);
  31. if (translationCache[locale]) {
  32. return translationCache[locale];
  33. }
  34. if (!requestCache[locale]) {
  35. requestCache[locale] = fetch(getAssetPath(`./assets/date-picker/nls/${locale}.json`))
  36. .then((resp) => resp.json())
  37. .catch(() => {
  38. console.error(`Translations for "${locale}" not found or invalid, falling back to english`);
  39. return getLocaleData("en");
  40. });
  41. }
  42. const data = await requestCache[locale];
  43. translationCache[locale] = data;
  44. return data;
  45. }
  46. /**
  47. * Maps value to valueAsDate
  48. *
  49. * @param value
  50. */
  51. export function getValueAsDateRange(value) {
  52. return value.map((v, index) => dateFromISO(v, index === 1));
  53. }