UNPKG

one

Version:

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

101 lines (100 loc) • 3.52 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var localization_exports = {}; __export(localization_exports, { Localization: () => Localization, useCalendars: () => useCalendars, useLocales: () => useLocales }); module.exports = __toCommonJS(localization_exports); var import_react = require("react"); var import_map = require("./map.cjs"); 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 { ...import_map.FALLBACK_LOCALE, languageTag: tag }; } } function getLocales() { if (typeof navigator === "undefined" || !navigator.languages?.length) { return [import_map.FALLBACK_LOCALE]; } const locales = [...navigator.languages].map(localeFor); return locales.length > 0 ? locales : [import_map.FALLBACK_LOCALE]; } function getCalendars() { if (typeof Intl === "undefined") return [import_map.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 [import_map.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] = (0, import_react.useState)(() => getLocales()); (0, import_react.useEffect)(() => subscribe(() => setLocales(getLocales())), []); return locales; } function useCalendars() { const [calendars, setCalendars] = (0, import_react.useState)(() => getCalendars()); (0, import_react.useEffect)(() => subscribe(() => setCalendars(getCalendars())), []); return calendars; } const Localization = Object.freeze({ getLocales, getCalendars });