All files / app/utils dayjs.ts

97.56% Statements 40/41
100% Branches 3/3
100% Functions 1/1
97.5% Lines 39/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87          23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x   23x 23x 23x 23x 23x 23x 23x   23x 23x 23x 23x 23x 23x       23x                                                             35x 35x               2879x  
// Locale data for every app language so dayjs can render month/day names and
// relative/duration strings in the user's language (en is built in). Importing
// a locale only registers it; the locale is always specified at the formatting
// site (via MUI's adapterLocale, or `dayjs(x).locale(i18nToDayjsLocale(ln))`) —
// we never mutate dayjs's global locale.
import "dayjs/locale/ca";
import "dayjs/locale/cs";
import "dayjs/locale/de";
import "dayjs/locale/es";
import "dayjs/locale/fr";
import "dayjs/locale/fr-ca";
import "dayjs/locale/he";
import "dayjs/locale/hi";
import "dayjs/locale/hu";
import "dayjs/locale/it";
import "dayjs/locale/ja";
import "dayjs/locale/nb";
import "dayjs/locale/nl";
import "dayjs/locale/pl";
import "dayjs/locale/pt";
import "dayjs/locale/pt-br";
import "dayjs/locale/ru";
import "dayjs/locale/sv";
import "dayjs/locale/tr";
import "dayjs/locale/uk";
import "dayjs/locale/zh-cn";
import "dayjs/locale/zh-tw";
 
import dayjs, { Dayjs } from "dayjs";
import customParseFormat from "dayjs/plugin/customParseFormat";
import DurationPlugin from "dayjs/plugin/duration";
import LocalizedFormat from "dayjs/plugin/localizedFormat";
import RelativeTime from "dayjs/plugin/relativeTime";
import Timezone from "dayjs/plugin/timezone";
import utc from "dayjs/plugin/utc";
 
dayjs.extend(utc);
dayjs.extend(customParseFormat);
dayjs.extend(DurationPlugin);
dayjs.extend(RelativeTime);
dayjs.extend(Timezone);
dayjs.extend(LocalizedFormat);
 
// Maps an i18n language code to the matching dayjs locale name (they differ for
// some: e.g. "pt-BR" -> "pt-br", "zh-Hans" -> "zh-cn"). en is the built-in default.
const I18N_TO_DAYJS_LOCALE: Record<string, string> = {
  ca: "ca",
  cs: "cs",
  de: "de",
  en: "en",
  es: "es",
  "es-419": "es",
  fr: "fr",
  "fr-CA": "fr-ca",
  he: "he",
  hi: "hi",
  hu: "hu",
  it: "it",
  ja: "ja",
  "nb-NO": "nb",
  nl: "nl",
  pl: "pl",
  pt: "pt",
  "pt-BR": "pt-br",
  ru: "ru",
  sv: "sv",
  tr: "tr",
  uk: "uk",
  "zh-Hans": "zh-cn",
  "zh-Hant": "zh-tw",
};
 
/// Maps an i18n language code to a registered dayjs locale name, falling back to
/// the base language, then English, for unmapped codes. Use this for MUI's
/// LocalizationProvider `adapterLocale` and for call-site formatting, e.g.
/// `dayjs(x).locale(i18nToDayjsLocale(language)).format("LL")`.
export function i18nToDayjsLocale(language: string): string {
  return (
    I18N_TO_DAYJS_LOCALE[language] ??
    I18N_TO_DAYJS_LOCALE[language.split("-")[0]] ??
    "en"
  );
}
 
export { Dayjs };
export default dayjs;