UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

78 lines (75 loc) • 2.52 kB
import { useEffect, useState } from "react"; import { FALLBACK_CALENDAR, FALLBACK_LOCALE } from "./map.mjs"; function localeFor(tag) { try { const parsed = new Intl.Locale(tag); const numbers = new Intl.NumberFormat(tag).formatToParts(1234567.89); const part = (type) => numbers.find((entry) => entry.type === type)?.value ?? null; const direction = parsed.textInfo?.direction; return { languageTag: parsed.baseName, languageCode: parsed.language ?? null, languageScriptCode: parsed.script ?? null, regionCode: parsed.region ?? null, languageRegionCode: parsed.region ?? null, currencyCode: null, currencySymbol: null, languageCurrencyCode: null, languageCurrencySymbol: null, decimalSeparator: part("decimal"), digitGroupingSeparator: part("group"), textDirection: direction === "rtl" ? "rtl" : "ltr", measurementSystem: null, temperatureUnit: null }; } catch { return { ...FALLBACK_LOCALE, languageTag: tag }; } } function getLocales() { if (typeof navigator === "undefined" || !navigator.languages?.length) { return [FALLBACK_LOCALE]; } const locales = [...navigator.languages].map(localeFor); return locales.length > 0 ? locales : [FALLBACK_LOCALE]; } function getCalendars() { if (typeof Intl === "undefined") return [FALLBACK_CALENDAR]; try { const options = new Intl.DateTimeFormat().resolvedOptions(); const parsed = new Intl.Locale(options.locale); const firstDay = parsed.weekInfo?.firstDay; return [{ calendar: options.calendar ?? null, timeZone: options.timeZone ?? null, uses24hourClock: parsed.hourCycle === void 0 ? null : parsed.hourCycle === "h23" || parsed.hourCycle === "h24", firstWeekday: firstDay === void 0 ? null : firstDay % 7 + 1 }]; } catch { return [FALLBACK_CALENDAR]; } } function subscribe(refresh) { if (typeof window === "undefined") return void 0; window.addEventListener("languagechange", refresh); return () => window.removeEventListener("languagechange", refresh); } function useLocales() { const [locales, setLocales] = useState(() => getLocales()); useEffect(() => subscribe(() => setLocales(getLocales())), []); return locales; } function useCalendars() { const [calendars, setCalendars] = useState(() => getCalendars()); useEffect(() => subscribe(() => setCalendars(getCalendars())), []); return calendars; } const Localization = Object.freeze({ getLocales, getCalendars }); export { Localization, useCalendars, useLocales }; //# sourceMappingURL=index.mjs.map