@dloizides/i18n 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,14 @@ All notable changes to `@dloizides/i18n` are documented here.
5
5
  The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project
6
6
  adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [Unreleased]
9
+
10
+ EXTRACT-TS-I18N-1 "Fold duplicated i18n and utils into @dloizides/i18n and @dloizides/utils".
11
+
12
+ ### Added
13
+
14
+ - `createFormatMessage(i18n)` builds the portal `FM(id, p1, p2, p3)` helper that 8 portals carried as a local copy.
15
+
8
16
  ## [1.1.0] — 2026-10-20
9
17
 
10
18
  D-PAT-15 "Move initI18n out of frontend-devtools so it is dev-only".
package/dist/index.d.mts CHANGED
@@ -182,4 +182,13 @@ interface InitI18nOptions {
182
182
  /** Wires react-i18next into the i18next singleton, runs `.init()` and returns the singleton. */
183
183
  declare function initI18n(options: InitI18nOptions): i18n;
184
184
 
185
- export { type Catalog, type Catalogs, DEFAULT_FALLBACK_LOCALE, DEFAULT_STORAGE_KEY, I18nProvider, type I18nProviderProps, type I18nValue, type InitI18nOptions, LanguageSwitcher, type LanguageSwitcherColors, type LanguageSwitcherProps, type LocaleCode, type LocaleOption, type NamedParams, type Translate, applyDocumentLocale, browserLanguages, createTranslator, initI18n, interpolateNamed, interpolatePositional, isSupported, lookup, primarySubtag, readStoredLocale, resolveInitialLocale, useI18n, writeStoredLocale };
185
+ /** The slice of an i18next instance that {@link createFormatMessage} needs. */
186
+ interface MessageTranslator {
187
+ t: (id: string, options: Record<string, string>) => string;
188
+ }
189
+ /** Positional-parameter message formatter: `FM(id, p1, p2, p3)` maps to `{{p1}}`..`{{p3}}`. */
190
+ type FormatMessage = (id: string, p1?: string, p2?: string, p3?: string) => string;
191
+ /** Builds the portal `FM()` helper over an initialised i18next instance. */
192
+ declare function createFormatMessage(translator: MessageTranslator): FormatMessage;
193
+
194
+ export { type Catalog, type Catalogs, DEFAULT_FALLBACK_LOCALE, DEFAULT_STORAGE_KEY, type FormatMessage, I18nProvider, type I18nProviderProps, type I18nValue, type InitI18nOptions, LanguageSwitcher, type LanguageSwitcherColors, type LanguageSwitcherProps, type LocaleCode, type LocaleOption, type MessageTranslator, type NamedParams, type Translate, applyDocumentLocale, browserLanguages, createFormatMessage, createTranslator, initI18n, interpolateNamed, interpolatePositional, isSupported, lookup, primarySubtag, readStoredLocale, resolveInitialLocale, useI18n, writeStoredLocale };
package/dist/index.d.ts CHANGED
@@ -182,4 +182,13 @@ interface InitI18nOptions {
182
182
  /** Wires react-i18next into the i18next singleton, runs `.init()` and returns the singleton. */
183
183
  declare function initI18n(options: InitI18nOptions): i18n;
184
184
 
185
- export { type Catalog, type Catalogs, DEFAULT_FALLBACK_LOCALE, DEFAULT_STORAGE_KEY, I18nProvider, type I18nProviderProps, type I18nValue, type InitI18nOptions, LanguageSwitcher, type LanguageSwitcherColors, type LanguageSwitcherProps, type LocaleCode, type LocaleOption, type NamedParams, type Translate, applyDocumentLocale, browserLanguages, createTranslator, initI18n, interpolateNamed, interpolatePositional, isSupported, lookup, primarySubtag, readStoredLocale, resolveInitialLocale, useI18n, writeStoredLocale };
185
+ /** The slice of an i18next instance that {@link createFormatMessage} needs. */
186
+ interface MessageTranslator {
187
+ t: (id: string, options: Record<string, string>) => string;
188
+ }
189
+ /** Positional-parameter message formatter: `FM(id, p1, p2, p3)` maps to `{{p1}}`..`{{p3}}`. */
190
+ type FormatMessage = (id: string, p1?: string, p2?: string, p3?: string) => string;
191
+ /** Builds the portal `FM()` helper over an initialised i18next instance. */
192
+ declare function createFormatMessage(translator: MessageTranslator): FormatMessage;
193
+
194
+ export { type Catalog, type Catalogs, DEFAULT_FALLBACK_LOCALE, DEFAULT_STORAGE_KEY, type FormatMessage, I18nProvider, type I18nProviderProps, type I18nValue, type InitI18nOptions, LanguageSwitcher, type LanguageSwitcherColors, type LanguageSwitcherProps, type LocaleCode, type LocaleOption, type MessageTranslator, type NamedParams, type Translate, applyDocumentLocale, browserLanguages, createFormatMessage, createTranslator, initI18n, interpolateNamed, interpolatePositional, isSupported, lookup, primarySubtag, readStoredLocale, resolveInitialLocale, useI18n, writeStoredLocale };
package/dist/index.js CHANGED
@@ -199,12 +199,24 @@ function initI18n(options) {
199
199
  return i18n__default.default;
200
200
  }
201
201
 
202
+ // src/createFormatMessage.ts
203
+ function createFormatMessage(translator) {
204
+ return (id, p1, p2, p3) => {
205
+ const options = {};
206
+ if (p1 !== void 0 && p1 !== null) options.p1 = p1;
207
+ if (p2 !== void 0 && p2 !== null) options.p2 = p2;
208
+ if (p3 !== void 0 && p3 !== null) options.p3 = p3;
209
+ return translator.t(id, options);
210
+ };
211
+ }
212
+
202
213
  exports.DEFAULT_FALLBACK_LOCALE = DEFAULT_FALLBACK_LOCALE;
203
214
  exports.DEFAULT_STORAGE_KEY = DEFAULT_STORAGE_KEY;
204
215
  exports.I18nProvider = I18nProvider;
205
216
  exports.LanguageSwitcher = LanguageSwitcher;
206
217
  exports.applyDocumentLocale = applyDocumentLocale;
207
218
  exports.browserLanguages = browserLanguages;
219
+ exports.createFormatMessage = createFormatMessage;
208
220
  exports.createTranslator = createTranslator;
209
221
  exports.initI18n = initI18n;
210
222
  exports.interpolateNamed = interpolateNamed;
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/createTranslator.ts","../src/detectLocale.ts","../src/I18nProvider.tsx","../src/LanguageSwitcher.tsx","../src/initI18n.ts"],"names":["createContext","useState","useCallback","useMemo","jsx","useContext","StyleSheet","View","Pressable","Text","i18n","initReactI18next"],"mappings":";;;;;;;;;;;;;AAOO,IAAM,uBAAA,GAA0B;AAGvC,SAAS,cAAc,KAAA,EAAsC;AAC3D,EAAA,OAAO,OAAO,UAAU,QAAA,IAAY,KAAA,KAAU,QAAQ,CAAC,KAAA,CAAM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AY,EAAE,EAAA,KAAO,EAAC;AAAA,IACjC,aAAA,EAAe;AAAA,MACb;AAAA;AACF,GACD,CAAA,CACA,KAAA,CAAM,mBAAmB,CAAA;AAE5B,EAAA,OAAOD,qBAAA;AACT","file":"index.js","sourcesContent":["/**\n * The pure core: catalog lookup + interpolation. No React, no storage, no side effects — so it is\n * trivially unit-testable and reusable outside a component tree (formatters, tests, server render).\n */\nimport type { Catalog, Catalogs, LocaleCode, NamedParams, Translate } from './types';\n\n/** The locale every catalog falls back to, per key. English is authoritative. */\nexport const DEFAULT_FALLBACK_LOCALE = 'en';\n\n/** True for a plain object of named params (as opposed to a positional string/number). */\nfunction isNamedParams(value: unknown): value is NamedParams {\n return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n\n/**\n * Fill `{name}` placeholders from a params object. An unmatched placeholder is left intact rather\n * than blanked, so a template/param mismatch is visible instead of silently losing text.\n */\nexport function interpolateNamed(template: string, params: NamedParams): string {\n return template.replace(/\\{(\\w+)\\}/g, (match, name: string) =>\n Object.prototype.hasOwnProperty.call(params, name) ? String(params[name]) : match,\n );\n}\n\n/** Fill `{0}`, `{1}`, `{2}` from positional args (the shared UI kit's calling convention). */\nexport function interpolatePositional(\n template: string,\n params: ReadonlyArray<string | number | undefined>,\n): string {\n return params.reduce<string>(\n (acc, param, index) => (param === undefined ? acc : acc.split(`{${index}}`).join(String(param))),\n template,\n );\n}\n\n/**\n * Look a key up in `locale`, then in `fallbackLocale`, then give back the key itself.\n *\n * The fallback is **per key**, not per catalog: a partially translated locale still shows its\n * translated keys and only falls back for the ones it is missing. That is what lets a new locale\n * ship incrementally instead of all-or-nothing.\n */\nexport function lookup(\n catalogs: Catalogs,\n locale: LocaleCode,\n key: string,\n fallbackLocale: LocaleCode = DEFAULT_FALLBACK_LOCALE,\n): string {\n const active: Catalog | undefined = catalogs[locale];\n const fallback: Catalog | undefined = catalogs[fallbackLocale];\n return active?.[key] ?? fallback?.[key] ?? key;\n}\n\n/**\n * Build a {@link Translate} bound to one locale. Supports BOTH interpolation styles — see the\n * `Translate` docs for why (positional = the UI kit; named = hand-written app catalogs).\n */\nexport function createTranslator(\n catalogs: Catalogs,\n locale: LocaleCode,\n fallbackLocale: LocaleCode = DEFAULT_FALLBACK_LOCALE,\n): Translate {\n return (key, p1, p2, p3) => {\n const template = lookup(catalogs, locale, key, fallbackLocale);\n if (isNamedParams(p1)) return interpolateNamed(template, p1);\n return interpolatePositional(template, [p1, p2, p3]);\n };\n}\n","/**\n * Locale resolution + persistence.\n *\n * The resolution order mirrors the v1 AML console (`shared/i18n.js`): a previously stored choice\n * wins, else the browser's preferred language (primary subtag only — `de-AT` → `de`), else the\n * fallback. Storage is best-effort: private mode / a native runtime with no `localStorage` must not\n * throw, it just means the choice does not persist.\n */\nimport type { LocaleCode, LocaleOption } from './types';\n\n/** Default persistence key. Override per app so two apps on one origin don't fight. */\nexport const DEFAULT_STORAGE_KEY = 'dloizides_locale';\n\n/** True when `code` is one of the app's supported locales. Pure. */\nexport function isSupported(locales: readonly LocaleOption[], code: string): boolean {\n return locales.some((l) => l.code === code);\n}\n\n/** `de-AT` → `de`; `EN` → `en`. Pure. */\nexport function primarySubtag(tag: string): string {\n return String(tag).toLowerCase().split('-')[0] ?? '';\n}\n\n/**\n * Pick the locale to start in: stored → browser-preferred → fallback. Pure — the caller supplies\n * the stored value and the browser's language list, which keeps this testable with no globals.\n */\nexport function resolveInitialLocale(\n locales: readonly LocaleOption[],\n stored: string | null,\n preferred: readonly string[],\n fallback: LocaleCode,\n): LocaleCode {\n if (stored && isSupported(locales, stored)) return stored;\n for (const tag of preferred) {\n const primary = primarySubtag(tag);\n if (isSupported(locales, primary)) return primary;\n }\n return fallback;\n}\n\n/** The browser's preferred languages, most-preferred first. `[]` off-web. */\nexport function browserLanguages(): string[] {\n if (typeof navigator === 'undefined') return [];\n const nav = navigator as Navigator & { userLanguage?: string };\n const list = Array.isArray(nav.languages) && nav.languages.length > 0 ? nav.languages : [];\n const single = nav.language ?? nav.userLanguage;\n return list.length > 0 ? [...list] : single ? [single] : [];\n}\n\n/** Read the persisted locale. Never throws (private mode, native, SSR). */\nexport function readStoredLocale(storageKey: string): string | null {\n try {\n if (typeof localStorage === 'undefined') return null;\n return localStorage.getItem(storageKey);\n } catch {\n return null;\n }\n}\n\n/** Persist the locale. Never throws — a failure just means it won't be remembered. */\nexport function writeStoredLocale(storageKey: string, code: LocaleCode): void {\n try {\n if (typeof localStorage === 'undefined') return;\n localStorage.setItem(storageKey, code);\n } catch {\n /* private mode / no storage — the choice simply does not persist */\n }\n}\n\n/**\n * Reflect the locale on the document so CSS can react (`:lang()`, `[dir=\"rtl\"]`). Web-only no-op\n * elsewhere. This is the RTL hook: ship an `rtl: true` locale and the layout can mirror itself\n * without any component changes.\n */\nexport function applyDocumentLocale(locales: readonly LocaleOption[], code: LocaleCode): void {\n if (typeof document === 'undefined') return;\n const option = locales.find((l) => l.code === code);\n document.documentElement.lang = code;\n document.documentElement.dir = option?.rtl ? 'rtl' : 'ltr';\n}\n","/**\n * `I18nProvider` — holds the active locale, exposes a memoized {@link Translate}, persists the\n * user's choice, and reflects `lang`/`dir` on the document.\n *\n * Wiring it to the rest of the shared kit is one line, because {@link Translate} is\n * signature-compatible with the kit's `t` contract:\n *\n * ```tsx\n * <I18nProvider catalogs={CATALOGS} locales={LOCALES}>\n * <KitBridge /> // const { t } = useI18n(); return <UiProvider theme={theme} t={t}>…\n * </I18nProvider>\n * ```\n */\nimport React, { createContext, useCallback, useContext, useMemo, useState } from 'react';\n\nimport { createTranslator, DEFAULT_FALLBACK_LOCALE } from './createTranslator';\nimport {\n applyDocumentLocale,\n browserLanguages,\n DEFAULT_STORAGE_KEY,\n readStoredLocale,\n resolveInitialLocale,\n writeStoredLocale,\n} from './detectLocale';\nimport type { Catalogs, LocaleCode, LocaleOption, Translate } from './types';\n\nexport interface I18nValue {\n /** Resolve a key in the active locale (positional or named interpolation). */\n t: Translate;\n /** The active locale code. */\n locale: LocaleCode;\n /** Switch locale: re-renders consumers, persists the choice, updates `lang`/`dir`. */\n setLocale: (code: LocaleCode) => void;\n /** Every locale the app offers (for a switcher). */\n locales: readonly LocaleOption[];\n}\n\nconst I18nContext = createContext<I18nValue | null>(null);\n\nexport interface I18nProviderProps {\n /** Every locale's catalog. The fallback locale's catalog is required. */\n catalogs: Catalogs;\n /** The locales offered to the user, in display order. */\n locales: readonly LocaleOption[];\n /** Per-key fallback locale. Default `en`. */\n fallbackLocale?: LocaleCode;\n /** localStorage key for the persisted choice. Default `dloizides_locale`. */\n storageKey?: string;\n /** Force a locale (tests / SSR). When set, detection and persistence are skipped. */\n initialLocale?: LocaleCode;\n children: React.ReactNode;\n}\n\nexport function I18nProvider({\n catalogs,\n locales,\n fallbackLocale = DEFAULT_FALLBACK_LOCALE,\n storageKey = DEFAULT_STORAGE_KEY,\n initialLocale,\n children,\n}: I18nProviderProps): React.ReactElement {\n // Resolved once, lazily: stored choice → browser preference → fallback. Reading storage and\n // `navigator` inside the initializer keeps the first paint on the right locale (no flash of\n // English before a `useEffect` corrects it).\n const [locale, setLocaleState] = useState<LocaleCode>(() => {\n if (initialLocale) return initialLocale;\n const resolved = resolveInitialLocale(\n locales,\n readStoredLocale(storageKey),\n browserLanguages(),\n fallbackLocale,\n );\n applyDocumentLocale(locales, resolved);\n return resolved;\n });\n\n const setLocale = useCallback(\n (code: LocaleCode) => {\n setLocaleState(code);\n writeStoredLocale(storageKey, code);\n applyDocumentLocale(locales, code);\n },\n [locales, storageKey],\n );\n\n const t = useMemo(\n () => createTranslator(catalogs, locale, fallbackLocale),\n [catalogs, locale, fallbackLocale],\n );\n\n const value = useMemo<I18nValue>(\n () => ({ t, locale, setLocale, locales }),\n [t, locale, setLocale, locales],\n );\n\n return <I18nContext.Provider value={value}>{children}</I18nContext.Provider>;\n}\n\n/** Access the active locale + translate function. Throws outside an {@link I18nProvider}. */\nexport function useI18n(): I18nValue {\n const value = useContext(I18nContext);\n if (value === null) throw new Error('useI18n must be used inside an <I18nProvider>.');\n return value;\n}\n","/**\n * `LanguageSwitcher` — a compact segmented control of locale codes (EN · DE · ES · FR · PT), the\n * same shape as the v1 AML console's `#lang-switcher`.\n *\n * Colours come in as PROPS rather than from a theme context on purpose: it keeps this package\n * dependency-free (no peer dep on the UI kit or a design-token package), so any app — themed or\n * not — can drop it in. Themed apps pass their own tokens, e.g.\n * `colors={{ text: colors.textSecondary, activeText: '#fff', activeBackground: colors.primary }}`.\n */\nimport React, { useCallback } from 'react';\nimport { Pressable, StyleSheet, Text, View, type PressableStateCallbackType } from 'react-native';\n\nimport { useI18n } from './I18nProvider';\nimport type { LocaleCode } from './types';\n\nconst BORDER_RADIUS = 8;\nconst PADDING_H = 9;\nconst PADDING_V = 5;\nconst FONT_SIZE = 12;\nconst GAP = 2;\n/** WCAG 2.5.5: an interactive target must be at least 44×44 px. */\nconst MIN_TARGET = 44;\n\nexport interface LanguageSwitcherColors {\n /** Inactive label colour. */\n text: string;\n /** Active label colour. */\n activeText: string;\n /** Active pill background. */\n activeBackground: string;\n /** Hover/press background for an inactive option. Optional. */\n hoverBackground?: string;\n}\n\nexport interface LanguageSwitcherProps {\n colors: LanguageSwitcherColors;\n /** Accessible name for the group. Default `Language`. */\n accessibilityLabel?: string;\n testID?: string;\n}\n\n/**\n * react-native-web adds `hovered` to the Pressable style-callback state; the core react-native\n * types do not model it. Widen the state rather than casting the whole style callback.\n */\ntype PressableState = PressableStateCallbackType & { hovered?: boolean };\n\nconst styles = StyleSheet.create({\n row: { flexDirection: 'row', alignItems: 'center', gap: GAP },\n option: {\n paddingHorizontal: PADDING_H,\n paddingVertical: PADDING_V,\n borderRadius: BORDER_RADIUS,\n minWidth: MIN_TARGET,\n minHeight: MIN_TARGET,\n alignItems: 'center',\n justifyContent: 'center',\n },\n label: { fontSize: FONT_SIZE, fontWeight: '600' },\n});\n\nexport function LanguageSwitcher({\n colors,\n accessibilityLabel = 'Language',\n testID = 'language-switcher',\n}: LanguageSwitcherProps): React.ReactElement {\n const { locale, setLocale, locales } = useI18n();\n\n const select = useCallback((code: LocaleCode) => () => setLocale(code), [setLocale]);\n\n return (\n <View accessibilityRole=\"radiogroup\" aria-label={accessibilityLabel} style={styles.row} testID={testID}>\n {locales.map((option) => {\n const isActive = option.code === locale;\n return (\n <Pressable\n key={option.code}\n accessibilityHint={`Show the interface in ${option.label}`}\n accessibilityLabel={option.label}\n accessibilityRole=\"radio\"\n accessibilityState={{ selected: isActive }}\n aria-checked={isActive}\n style={({ hovered }: PressableState) => [\n styles.option,\n isActive\n ? { backgroundColor: colors.activeBackground }\n : hovered && colors.hoverBackground\n ? { backgroundColor: colors.hoverBackground }\n : null,\n ]}\n testID={`${testID}-${option.code}`}\n onPress={select(option.code)}\n >\n <Text style={[styles.label, { color: isActive ? colors.activeText : colors.text }]}>\n {option.label}\n </Text>\n </Pressable>\n );\n })}\n </View>\n );\n}\n","import i18n, { type i18n as I18nInstance, type Resource } from 'i18next';\nimport { initReactI18next } from 'react-i18next';\n\n/** Options for {@link initI18n}: the app's resources and languages; the package owns the init chain. */\nexport interface InitI18nOptions {\n resources: Resource;\n lng?: string;\n fallbackLng?: string;\n escapeValue?: boolean;\n defaultNS?: string | readonly string[];\n ns?: string | readonly string[];\n}\n\nconst DEFAULT_FALLBACK_LNG = 'en';\n\nconst ignoreInitRejection = (): void => undefined;\n\n/** Wires react-i18next into the i18next singleton, runs `.init()` and returns the singleton. */\nexport function initI18n(options: InitI18nOptions): I18nInstance {\n const {\n resources,\n lng,\n fallbackLng = DEFAULT_FALLBACK_LNG,\n escapeValue = false,\n defaultNS,\n ns,\n } = options;\n\n i18n\n .use(initReactI18next)\n .init({\n lng: lng ?? fallbackLng,\n fallbackLng,\n resources,\n ...(defaultNS !== undefined ? { defaultNS } : {}),\n ...(ns !== undefined ? { ns } : {}),\n interpolation: {\n escapeValue,\n },\n })\n .catch(ignoreInitRejection);\n\n return i18n;\n}\n"]}
1
+ {"version":3,"sources":["../src/createTranslator.ts","../src/detectLocale.ts","../src/I18nProvider.tsx","../src/LanguageSwitcher.tsx","../src/initI18n.ts","../src/createFormatMessage.ts"],"names":["createContext","useState","useCallback","useMemo","jsx","useContext","StyleSheet","View","Pressable","Text","i18n","initReactI18next"],"mappings":";;;;;;;;;;;;;AAOO,IAAM,uBAAA,GAA0B;AAGvC,SAAS,cAAc,KAAA,EAAsC;AAC3D,EAAA,OAAO,OAAO,UAAU,QAAA,IAAY,KAAA,KAAU,QAAQ,CAAC,KAAA,CAAM,QAAQ,KAAK,CAAA;AAC5E;AAMO,SAAS,gBAAA,CAAiB,UAAkB,MAAA,EAA6B;AAC9E,EAAA,OAAO,QAAA,CAAS,OAAA;AAAA,IAAQ,YAAA;AAAA,IAAc,CAAC,KAAA,EAAO,IAAA,KAC5C,MAAA,CAAO,UAAU,cAAA,CAAe,IAAA,CAAK,MAAA,EAAQ,IAAI,CAAA,GAAI,MAAA,CAAO,MAAA,CAAO,IAAI,CAAC,CAAA,GAAI;AAAA,GAC9E;AACF;AAGO,SAAS,qBAAA,CACd,UACA,MAAA,EACQ;AACR,EAAA,OAAO,MAAA,CAAO,MAAA;AAAA,IACZ,CAAC,GAAA,EAAK,KAAA,EAAO,KAAA,KAAW,KAAA,KAAU,SAAY,GAAA,GAAM,GAAA,CAAI,KAAA,CAAM,CAAA,CAAA,EAAI,KAAK,CAAA,CAAA,CAAG,CAAA,CAAE,IAAA,CAAK,MAAA,CAAO,KAAK,CAAC,CAAA;AAAA,IAC9F;AAAA,GACF;AACF;AASO,SAAS,MAAA,CACd,QAAA,EACA,MAAA,EACA,GAAA,EACA,iBAA6B,uBAAA,EACrB;AACR,EAAA,MAAM,MAAA,GAA8B,SAAS,MAAM,CAAA;AACnD,EAAA,MAAM,QAAA,GAAgC,SAAS,cAAc,CAAA;AAC7D,EAAA,OAAO,MAAA,GAAS,GAAG,CAAA,IAAK,QAAA,GAAW,GAAG,CAAA,IAAK,GAAA;AAC7C;AAMO,SAAS,gBAAA,CACd,QAAA,EACA,MAAA,EACA,cAAA,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ACd,SAAA;AAAA,IACA;AAAA,GACF,GAAI,OAAA;AAEJ,EAAAC,qBAAA,CACG,GAAA,CAAIC,6BAAgB,CAAA,CACpB,IAAA,CAAK;AAAA,IACJ,KAAK,GAAA,IAAO,WAAA;AAAA,IACZ,WAAA;AAAA,IACA,SAAA;AAAA,IACA,GAAI,SAAA,KAAc,MAAA,GAAY,EAAE,SAAA,KAAc,EAAC;AAAA,IAC/C,GAAI,EAAA,KAAO,MAAA,GAAY,EAAE,EAAA,KAAO,EAAC;AAAA,IACjC,aAAA,EAAe;AAAA,MACb;AAAA;AACF,GACD,CAAA,CACA,KAAA,CAAM,mBAAmB,CAAA;AAE5B,EAAA,OAAOD,qBAAA;AACT;;;AClCO,SAAS,oBAAoB,UAAA,EAA8C;AAChF,EAAA,OAAO,CAAC,EAAA,EAAI,EAAA,EAAI,EAAA,EAAI,EAAA,KAAO;AACzB,IAAA,MAAM,UAAkC,EAAC;AACzC,IAAA,IAAI,EAAA,KAAO,MAAA,IAAa,EAAA,KAAO,IAAA,UAAc,EAAA,GAAK,EAAA;AAClD,IAAA,IAAI,EAAA,KAAO,MAAA,IAAa,EAAA,KAAO,IAAA,UAAc,EAAA,GAAK,EAAA;AAClD,IAAA,IAAI,EAAA,KAAO,MAAA,IAAa,EAAA,KAAO,IAAA,UAAc,EAAA,GAAK,EAAA;AAClD,IAAA,OAAO,UAAA,CAAW,CAAA,CAAE,EAAA,EAAI,OAAO,CAAA;AAAA,EACjC,CAAA;AACF","file":"index.js","sourcesContent":["/**\n * The pure core: catalog lookup + interpolation. No React, no storage, no side effects — so it is\n * trivially unit-testable and reusable outside a component tree (formatters, tests, server render).\n */\nimport type { Catalog, Catalogs, LocaleCode, NamedParams, Translate } from './types';\n\n/** The locale every catalog falls back to, per key. English is authoritative. */\nexport const DEFAULT_FALLBACK_LOCALE = 'en';\n\n/** True for a plain object of named params (as opposed to a positional string/number). */\nfunction isNamedParams(value: unknown): value is NamedParams {\n return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n\n/**\n * Fill `{name}` placeholders from a params object. An unmatched placeholder is left intact rather\n * than blanked, so a template/param mismatch is visible instead of silently losing text.\n */\nexport function interpolateNamed(template: string, params: NamedParams): string {\n return template.replace(/\\{(\\w+)\\}/g, (match, name: string) =>\n Object.prototype.hasOwnProperty.call(params, name) ? String(params[name]) : match,\n );\n}\n\n/** Fill `{0}`, `{1}`, `{2}` from positional args (the shared UI kit's calling convention). */\nexport function interpolatePositional(\n template: string,\n params: ReadonlyArray<string | number | undefined>,\n): string {\n return params.reduce<string>(\n (acc, param, index) => (param === undefined ? acc : acc.split(`{${index}}`).join(String(param))),\n template,\n );\n}\n\n/**\n * Look a key up in `locale`, then in `fallbackLocale`, then give back the key itself.\n *\n * The fallback is **per key**, not per catalog: a partially translated locale still shows its\n * translated keys and only falls back for the ones it is missing. That is what lets a new locale\n * ship incrementally instead of all-or-nothing.\n */\nexport function lookup(\n catalogs: Catalogs,\n locale: LocaleCode,\n key: string,\n fallbackLocale: LocaleCode = DEFAULT_FALLBACK_LOCALE,\n): string {\n const active: Catalog | undefined = catalogs[locale];\n const fallback: Catalog | undefined = catalogs[fallbackLocale];\n return active?.[key] ?? fallback?.[key] ?? key;\n}\n\n/**\n * Build a {@link Translate} bound to one locale. Supports BOTH interpolation styles — see the\n * `Translate` docs for why (positional = the UI kit; named = hand-written app catalogs).\n */\nexport function createTranslator(\n catalogs: Catalogs,\n locale: LocaleCode,\n fallbackLocale: LocaleCode = DEFAULT_FALLBACK_LOCALE,\n): Translate {\n return (key, p1, p2, p3) => {\n const template = lookup(catalogs, locale, key, fallbackLocale);\n if (isNamedParams(p1)) return interpolateNamed(template, p1);\n return interpolatePositional(template, [p1, p2, p3]);\n };\n}\n","/**\n * Locale resolution + persistence.\n *\n * The resolution order mirrors the v1 AML console (`shared/i18n.js`): a previously stored choice\n * wins, else the browser's preferred language (primary subtag only — `de-AT` → `de`), else the\n * fallback. Storage is best-effort: private mode / a native runtime with no `localStorage` must not\n * throw, it just means the choice does not persist.\n */\nimport type { LocaleCode, LocaleOption } from './types';\n\n/** Default persistence key. Override per app so two apps on one origin don't fight. */\nexport const DEFAULT_STORAGE_KEY = 'dloizides_locale';\n\n/** True when `code` is one of the app's supported locales. Pure. */\nexport function isSupported(locales: readonly LocaleOption[], code: string): boolean {\n return locales.some((l) => l.code === code);\n}\n\n/** `de-AT` → `de`; `EN` → `en`. Pure. */\nexport function primarySubtag(tag: string): string {\n return String(tag).toLowerCase().split('-')[0] ?? '';\n}\n\n/**\n * Pick the locale to start in: stored → browser-preferred → fallback. Pure — the caller supplies\n * the stored value and the browser's language list, which keeps this testable with no globals.\n */\nexport function resolveInitialLocale(\n locales: readonly LocaleOption[],\n stored: string | null,\n preferred: readonly string[],\n fallback: LocaleCode,\n): LocaleCode {\n if (stored && isSupported(locales, stored)) return stored;\n for (const tag of preferred) {\n const primary = primarySubtag(tag);\n if (isSupported(locales, primary)) return primary;\n }\n return fallback;\n}\n\n/** The browser's preferred languages, most-preferred first. `[]` off-web. */\nexport function browserLanguages(): string[] {\n if (typeof navigator === 'undefined') return [];\n const nav = navigator as Navigator & { userLanguage?: string };\n const list = Array.isArray(nav.languages) && nav.languages.length > 0 ? nav.languages : [];\n const single = nav.language ?? nav.userLanguage;\n return list.length > 0 ? [...list] : single ? [single] : [];\n}\n\n/** Read the persisted locale. Never throws (private mode, native, SSR). */\nexport function readStoredLocale(storageKey: string): string | null {\n try {\n if (typeof localStorage === 'undefined') return null;\n return localStorage.getItem(storageKey);\n } catch {\n return null;\n }\n}\n\n/** Persist the locale. Never throws — a failure just means it won't be remembered. */\nexport function writeStoredLocale(storageKey: string, code: LocaleCode): void {\n try {\n if (typeof localStorage === 'undefined') return;\n localStorage.setItem(storageKey, code);\n } catch {\n /* private mode / no storage — the choice simply does not persist */\n }\n}\n\n/**\n * Reflect the locale on the document so CSS can react (`:lang()`, `[dir=\"rtl\"]`). Web-only no-op\n * elsewhere. This is the RTL hook: ship an `rtl: true` locale and the layout can mirror itself\n * without any component changes.\n */\nexport function applyDocumentLocale(locales: readonly LocaleOption[], code: LocaleCode): void {\n if (typeof document === 'undefined') return;\n const option = locales.find((l) => l.code === code);\n document.documentElement.lang = code;\n document.documentElement.dir = option?.rtl ? 'rtl' : 'ltr';\n}\n","/**\n * `I18nProvider` — holds the active locale, exposes a memoized {@link Translate}, persists the\n * user's choice, and reflects `lang`/`dir` on the document.\n *\n * Wiring it to the rest of the shared kit is one line, because {@link Translate} is\n * signature-compatible with the kit's `t` contract:\n *\n * ```tsx\n * <I18nProvider catalogs={CATALOGS} locales={LOCALES}>\n * <KitBridge /> // const { t } = useI18n(); return <UiProvider theme={theme} t={t}>…\n * </I18nProvider>\n * ```\n */\nimport React, { createContext, useCallback, useContext, useMemo, useState } from 'react';\n\nimport { createTranslator, DEFAULT_FALLBACK_LOCALE } from './createTranslator';\nimport {\n applyDocumentLocale,\n browserLanguages,\n DEFAULT_STORAGE_KEY,\n readStoredLocale,\n resolveInitialLocale,\n writeStoredLocale,\n} from './detectLocale';\nimport type { Catalogs, LocaleCode, LocaleOption, Translate } from './types';\n\nexport interface I18nValue {\n /** Resolve a key in the active locale (positional or named interpolation). */\n t: Translate;\n /** The active locale code. */\n locale: LocaleCode;\n /** Switch locale: re-renders consumers, persists the choice, updates `lang`/`dir`. */\n setLocale: (code: LocaleCode) => void;\n /** Every locale the app offers (for a switcher). */\n locales: readonly LocaleOption[];\n}\n\nconst I18nContext = createContext<I18nValue | null>(null);\n\nexport interface I18nProviderProps {\n /** Every locale's catalog. The fallback locale's catalog is required. */\n catalogs: Catalogs;\n /** The locales offered to the user, in display order. */\n locales: readonly LocaleOption[];\n /** Per-key fallback locale. Default `en`. */\n fallbackLocale?: LocaleCode;\n /** localStorage key for the persisted choice. Default `dloizides_locale`. */\n storageKey?: string;\n /** Force a locale (tests / SSR). When set, detection and persistence are skipped. */\n initialLocale?: LocaleCode;\n children: React.ReactNode;\n}\n\nexport function I18nProvider({\n catalogs,\n locales,\n fallbackLocale = DEFAULT_FALLBACK_LOCALE,\n storageKey = DEFAULT_STORAGE_KEY,\n initialLocale,\n children,\n}: I18nProviderProps): React.ReactElement {\n // Resolved once, lazily: stored choice → browser preference → fallback. Reading storage and\n // `navigator` inside the initializer keeps the first paint on the right locale (no flash of\n // English before a `useEffect` corrects it).\n const [locale, setLocaleState] = useState<LocaleCode>(() => {\n if (initialLocale) return initialLocale;\n const resolved = resolveInitialLocale(\n locales,\n readStoredLocale(storageKey),\n browserLanguages(),\n fallbackLocale,\n );\n applyDocumentLocale(locales, resolved);\n return resolved;\n });\n\n const setLocale = useCallback(\n (code: LocaleCode) => {\n setLocaleState(code);\n writeStoredLocale(storageKey, code);\n applyDocumentLocale(locales, code);\n },\n [locales, storageKey],\n );\n\n const t = useMemo(\n () => createTranslator(catalogs, locale, fallbackLocale),\n [catalogs, locale, fallbackLocale],\n );\n\n const value = useMemo<I18nValue>(\n () => ({ t, locale, setLocale, locales }),\n [t, locale, setLocale, locales],\n );\n\n return <I18nContext.Provider value={value}>{children}</I18nContext.Provider>;\n}\n\n/** Access the active locale + translate function. Throws outside an {@link I18nProvider}. */\nexport function useI18n(): I18nValue {\n const value = useContext(I18nContext);\n if (value === null) throw new Error('useI18n must be used inside an <I18nProvider>.');\n return value;\n}\n","/**\n * `LanguageSwitcher` — a compact segmented control of locale codes (EN · DE · ES · FR · PT), the\n * same shape as the v1 AML console's `#lang-switcher`.\n *\n * Colours come in as PROPS rather than from a theme context on purpose: it keeps this package\n * dependency-free (no peer dep on the UI kit or a design-token package), so any app — themed or\n * not — can drop it in. Themed apps pass their own tokens, e.g.\n * `colors={{ text: colors.textSecondary, activeText: '#fff', activeBackground: colors.primary }}`.\n */\nimport React, { useCallback } from 'react';\nimport { Pressable, StyleSheet, Text, View, type PressableStateCallbackType } from 'react-native';\n\nimport { useI18n } from './I18nProvider';\nimport type { LocaleCode } from './types';\n\nconst BORDER_RADIUS = 8;\nconst PADDING_H = 9;\nconst PADDING_V = 5;\nconst FONT_SIZE = 12;\nconst GAP = 2;\n/** WCAG 2.5.5: an interactive target must be at least 44×44 px. */\nconst MIN_TARGET = 44;\n\nexport interface LanguageSwitcherColors {\n /** Inactive label colour. */\n text: string;\n /** Active label colour. */\n activeText: string;\n /** Active pill background. */\n activeBackground: string;\n /** Hover/press background for an inactive option. Optional. */\n hoverBackground?: string;\n}\n\nexport interface LanguageSwitcherProps {\n colors: LanguageSwitcherColors;\n /** Accessible name for the group. Default `Language`. */\n accessibilityLabel?: string;\n testID?: string;\n}\n\n/**\n * react-native-web adds `hovered` to the Pressable style-callback state; the core react-native\n * types do not model it. Widen the state rather than casting the whole style callback.\n */\ntype PressableState = PressableStateCallbackType & { hovered?: boolean };\n\nconst styles = StyleSheet.create({\n row: { flexDirection: 'row', alignItems: 'center', gap: GAP },\n option: {\n paddingHorizontal: PADDING_H,\n paddingVertical: PADDING_V,\n borderRadius: BORDER_RADIUS,\n minWidth: MIN_TARGET,\n minHeight: MIN_TARGET,\n alignItems: 'center',\n justifyContent: 'center',\n },\n label: { fontSize: FONT_SIZE, fontWeight: '600' },\n});\n\nexport function LanguageSwitcher({\n colors,\n accessibilityLabel = 'Language',\n testID = 'language-switcher',\n}: LanguageSwitcherProps): React.ReactElement {\n const { locale, setLocale, locales } = useI18n();\n\n const select = useCallback((code: LocaleCode) => () => setLocale(code), [setLocale]);\n\n return (\n <View accessibilityRole=\"radiogroup\" aria-label={accessibilityLabel} style={styles.row} testID={testID}>\n {locales.map((option) => {\n const isActive = option.code === locale;\n return (\n <Pressable\n key={option.code}\n accessibilityHint={`Show the interface in ${option.label}`}\n accessibilityLabel={option.label}\n accessibilityRole=\"radio\"\n accessibilityState={{ selected: isActive }}\n aria-checked={isActive}\n style={({ hovered }: PressableState) => [\n styles.option,\n isActive\n ? { backgroundColor: colors.activeBackground }\n : hovered && colors.hoverBackground\n ? { backgroundColor: colors.hoverBackground }\n : null,\n ]}\n testID={`${testID}-${option.code}`}\n onPress={select(option.code)}\n >\n <Text style={[styles.label, { color: isActive ? colors.activeText : colors.text }]}>\n {option.label}\n </Text>\n </Pressable>\n );\n })}\n </View>\n );\n}\n","import i18n, { type i18n as I18nInstance, type Resource } from 'i18next';\nimport { initReactI18next } from 'react-i18next';\n\n/** Options for {@link initI18n}: the app's resources and languages; the package owns the init chain. */\nexport interface InitI18nOptions {\n resources: Resource;\n lng?: string;\n fallbackLng?: string;\n escapeValue?: boolean;\n defaultNS?: string | readonly string[];\n ns?: string | readonly string[];\n}\n\nconst DEFAULT_FALLBACK_LNG = 'en';\n\nconst ignoreInitRejection = (): void => undefined;\n\n/** Wires react-i18next into the i18next singleton, runs `.init()` and returns the singleton. */\nexport function initI18n(options: InitI18nOptions): I18nInstance {\n const {\n resources,\n lng,\n fallbackLng = DEFAULT_FALLBACK_LNG,\n escapeValue = false,\n defaultNS,\n ns,\n } = options;\n\n i18n\n .use(initReactI18next)\n .init({\n lng: lng ?? fallbackLng,\n fallbackLng,\n resources,\n ...(defaultNS !== undefined ? { defaultNS } : {}),\n ...(ns !== undefined ? { ns } : {}),\n interpolation: {\n escapeValue,\n },\n })\n .catch(ignoreInitRejection);\n\n return i18n;\n}\n","/** The slice of an i18next instance that {@link createFormatMessage} needs. */\nexport interface MessageTranslator {\n t: (id: string, options: Record<string, string>) => string;\n}\n\n/** Positional-parameter message formatter: `FM(id, p1, p2, p3)` maps to `{{p1}}`..`{{p3}}`. */\nexport type FormatMessage = (id: string, p1?: string, p2?: string, p3?: string) => string;\n\n/** Builds the portal `FM()` helper over an initialised i18next instance. */\nexport function createFormatMessage(translator: MessageTranslator): FormatMessage {\n return (id, p1, p2, p3) => {\n const options: Record<string, string> = {};\n if (p1 !== undefined && p1 !== null) options.p1 = p1;\n if (p2 !== undefined && p2 !== null) options.p2 = p2;\n if (p3 !== undefined && p3 !== null) options.p3 = p3;\n return translator.t(id, options);\n };\n}\n"]}
package/dist/index.mjs CHANGED
@@ -193,6 +193,17 @@ function initI18n(options) {
193
193
  return i18n;
194
194
  }
195
195
 
196
- export { DEFAULT_FALLBACK_LOCALE, DEFAULT_STORAGE_KEY, I18nProvider, LanguageSwitcher, applyDocumentLocale, browserLanguages, createTranslator, initI18n, interpolateNamed, interpolatePositional, isSupported, lookup, primarySubtag, readStoredLocale, resolveInitialLocale, useI18n, writeStoredLocale };
196
+ // src/createFormatMessage.ts
197
+ function createFormatMessage(translator) {
198
+ return (id, p1, p2, p3) => {
199
+ const options = {};
200
+ if (p1 !== void 0 && p1 !== null) options.p1 = p1;
201
+ if (p2 !== void 0 && p2 !== null) options.p2 = p2;
202
+ if (p3 !== void 0 && p3 !== null) options.p3 = p3;
203
+ return translator.t(id, options);
204
+ };
205
+ }
206
+
207
+ export { DEFAULT_FALLBACK_LOCALE, DEFAULT_STORAGE_KEY, I18nProvider, LanguageSwitcher, applyDocumentLocale, browserLanguages, createFormatMessage, createTranslator, initI18n, interpolateNamed, interpolatePositional, isSupported, lookup, primarySubtag, readStoredLocale, resolveInitialLocale, useI18n, writeStoredLocale };
197
208
  //# sourceMappingURL=index.mjs.map
198
209
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/createTranslator.ts","../src/detectLocale.ts","../src/I18nProvider.tsx","../src/LanguageSwitcher.tsx","../src/initI18n.ts"],"names":["useCallback","jsx"],"mappings":";;;;;;;AAOO,IAAM,uBAAA,GAA0B;AAGvC,SAAS,cAAc,KAAA,EAAsC;AAC3D,EAAA,OAAO,OAAO,UAAU,QAAA,IAAY,KAAA,KAAU,QAAQ,CAAC,KAAA,CAAM,QAAQ,KAAK,CAAA;AAC5E;AAMO,SAAS,gBAAA,CAAiB,UAAkB,MAAA,EAA6B;AAC9E,EAAA,OAAO,QAAA,CAAS,OAAA;AAAA,IAAQ,YAAA;AAAA,IAAc,CAAC,KAAA,EAAO,IAAA,KAC5C,MAAA,CAAO,UAAU,cAAA,CAAe,IAAA,CAAK,MAAA,EAAQ,IAAI,CAAA,GAAI,MAAA,CAAO,MAAA,CAAO,IAAI,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AAAA,QACA,MAAA,EAAQ,CAAA,EAAG,MAAM,CAAA,CAAA,EAAI,OAAO,IAAI,CAAA,CAAA;AAAA,QAChC,OAAA,EAAS,MAAA,CAAO,MAAA,CAAO,IAAI,CAAA;AAAA,QAE3B,0BAAAA,GAAAA,CAAC,IAAA,EAAA,EAAK,KAAA,EAAO,CAAC,OAAO,KAAA,EAAO,EAAE,KAAA,EAAO,QAAA,GAAW,OAAO,UAAA,GAAa,MAAA,CAAO,MAAM,CAAA,EAC9E,iBAAO,KAAA,EACV;AAAA,OAAA;AAAA,MAnBK,MAAA,CAAO;AAAA,KAoBd;AAAA,EAEJ,CAAC,CAAA,EACH,CAAA;AAEJ;ACxFA,IAAM,oBAAA,GAAuB,IAAA;AAE7B,IAAM,sBAAsB,MAAY,MAAA;AAGjC,SAAS,SAAS,OAAA,EAAwC;AAC/D,EAAA,MAAM;AAAA,IACJ,SAAA;AAAA,IACA,GAAA;AAAA,IACA,WAAA,GAAc,oBAAA;AAAA,IACd,WAAA,GAAc,KAAA;AAAA,IACd,SAAA;AAAA,IACA;AAAA,GACF,GAAI,OAAA;AAEJ,EAAA,IAAA,CACG,GAAA,CAAI,gBAAgB,CAAA,CACpB,IAAA,CAAK;AAAA,IACJ,KAAK,GAAA,IAAO,WAAA;AAAA,IACZ,WAAA;AAAA,IACA,SAAA;AAAA,IACA,GAAI,SAAA,KAAc,MAAA,GAAY,EAAE,SAAA,KAAc,EAAC;AAAA,IAC/C,GAAI,EAAA,KAAO,MAAA,GAAY,EAAE,EAAA,KAAO,EAAC;AAAA,IACjC,aAAA,EAAe;AAAA,MACb;AAAA;AACF,GACD,CAAA,CACA,KAAA,CAAM,mBAAmB,CAAA;AAE5B,EAAA,OAAO,IAAA;AACT","file":"index.mjs","sourcesContent":["/**\n * The pure core: catalog lookup + interpolation. No React, no storage, no side effects — so it is\n * trivially unit-testable and reusable outside a component tree (formatters, tests, server render).\n */\nimport type { Catalog, Catalogs, LocaleCode, NamedParams, Translate } from './types';\n\n/** The locale every catalog falls back to, per key. English is authoritative. */\nexport const DEFAULT_FALLBACK_LOCALE = 'en';\n\n/** True for a plain object of named params (as opposed to a positional string/number). */\nfunction isNamedParams(value: unknown): value is NamedParams {\n return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n\n/**\n * Fill `{name}` placeholders from a params object. An unmatched placeholder is left intact rather\n * than blanked, so a template/param mismatch is visible instead of silently losing text.\n */\nexport function interpolateNamed(template: string, params: NamedParams): string {\n return template.replace(/\\{(\\w+)\\}/g, (match, name: string) =>\n Object.prototype.hasOwnProperty.call(params, name) ? String(params[name]) : match,\n );\n}\n\n/** Fill `{0}`, `{1}`, `{2}` from positional args (the shared UI kit's calling convention). */\nexport function interpolatePositional(\n template: string,\n params: ReadonlyArray<string | number | undefined>,\n): string {\n return params.reduce<string>(\n (acc, param, index) => (param === undefined ? acc : acc.split(`{${index}}`).join(String(param))),\n template,\n );\n}\n\n/**\n * Look a key up in `locale`, then in `fallbackLocale`, then give back the key itself.\n *\n * The fallback is **per key**, not per catalog: a partially translated locale still shows its\n * translated keys and only falls back for the ones it is missing. That is what lets a new locale\n * ship incrementally instead of all-or-nothing.\n */\nexport function lookup(\n catalogs: Catalogs,\n locale: LocaleCode,\n key: string,\n fallbackLocale: LocaleCode = DEFAULT_FALLBACK_LOCALE,\n): string {\n const active: Catalog | undefined = catalogs[locale];\n const fallback: Catalog | undefined = catalogs[fallbackLocale];\n return active?.[key] ?? fallback?.[key] ?? key;\n}\n\n/**\n * Build a {@link Translate} bound to one locale. Supports BOTH interpolation styles — see the\n * `Translate` docs for why (positional = the UI kit; named = hand-written app catalogs).\n */\nexport function createTranslator(\n catalogs: Catalogs,\n locale: LocaleCode,\n fallbackLocale: LocaleCode = DEFAULT_FALLBACK_LOCALE,\n): Translate {\n return (key, p1, p2, p3) => {\n const template = lookup(catalogs, locale, key, fallbackLocale);\n if (isNamedParams(p1)) return interpolateNamed(template, p1);\n return interpolatePositional(template, [p1, p2, p3]);\n };\n}\n","/**\n * Locale resolution + persistence.\n *\n * The resolution order mirrors the v1 AML console (`shared/i18n.js`): a previously stored choice\n * wins, else the browser's preferred language (primary subtag only — `de-AT` → `de`), else the\n * fallback. Storage is best-effort: private mode / a native runtime with no `localStorage` must not\n * throw, it just means the choice does not persist.\n */\nimport type { LocaleCode, LocaleOption } from './types';\n\n/** Default persistence key. Override per app so two apps on one origin don't fight. */\nexport const DEFAULT_STORAGE_KEY = 'dloizides_locale';\n\n/** True when `code` is one of the app's supported locales. Pure. */\nexport function isSupported(locales: readonly LocaleOption[], code: string): boolean {\n return locales.some((l) => l.code === code);\n}\n\n/** `de-AT` → `de`; `EN` → `en`. Pure. */\nexport function primarySubtag(tag: string): string {\n return String(tag).toLowerCase().split('-')[0] ?? '';\n}\n\n/**\n * Pick the locale to start in: stored → browser-preferred → fallback. Pure — the caller supplies\n * the stored value and the browser's language list, which keeps this testable with no globals.\n */\nexport function resolveInitialLocale(\n locales: readonly LocaleOption[],\n stored: string | null,\n preferred: readonly string[],\n fallback: LocaleCode,\n): LocaleCode {\n if (stored && isSupported(locales, stored)) return stored;\n for (const tag of preferred) {\n const primary = primarySubtag(tag);\n if (isSupported(locales, primary)) return primary;\n }\n return fallback;\n}\n\n/** The browser's preferred languages, most-preferred first. `[]` off-web. */\nexport function browserLanguages(): string[] {\n if (typeof navigator === 'undefined') return [];\n const nav = navigator as Navigator & { userLanguage?: string };\n const list = Array.isArray(nav.languages) && nav.languages.length > 0 ? nav.languages : [];\n const single = nav.language ?? nav.userLanguage;\n return list.length > 0 ? [...list] : single ? [single] : [];\n}\n\n/** Read the persisted locale. Never throws (private mode, native, SSR). */\nexport function readStoredLocale(storageKey: string): string | null {\n try {\n if (typeof localStorage === 'undefined') return null;\n return localStorage.getItem(storageKey);\n } catch {\n return null;\n }\n}\n\n/** Persist the locale. Never throws — a failure just means it won't be remembered. */\nexport function writeStoredLocale(storageKey: string, code: LocaleCode): void {\n try {\n if (typeof localStorage === 'undefined') return;\n localStorage.setItem(storageKey, code);\n } catch {\n /* private mode / no storage — the choice simply does not persist */\n }\n}\n\n/**\n * Reflect the locale on the document so CSS can react (`:lang()`, `[dir=\"rtl\"]`). Web-only no-op\n * elsewhere. This is the RTL hook: ship an `rtl: true` locale and the layout can mirror itself\n * without any component changes.\n */\nexport function applyDocumentLocale(locales: readonly LocaleOption[], code: LocaleCode): void {\n if (typeof document === 'undefined') return;\n const option = locales.find((l) => l.code === code);\n document.documentElement.lang = code;\n document.documentElement.dir = option?.rtl ? 'rtl' : 'ltr';\n}\n","/**\n * `I18nProvider` — holds the active locale, exposes a memoized {@link Translate}, persists the\n * user's choice, and reflects `lang`/`dir` on the document.\n *\n * Wiring it to the rest of the shared kit is one line, because {@link Translate} is\n * signature-compatible with the kit's `t` contract:\n *\n * ```tsx\n * <I18nProvider catalogs={CATALOGS} locales={LOCALES}>\n * <KitBridge /> // const { t } = useI18n(); return <UiProvider theme={theme} t={t}>…\n * </I18nProvider>\n * ```\n */\nimport React, { createContext, useCallback, useContext, useMemo, useState } from 'react';\n\nimport { createTranslator, DEFAULT_FALLBACK_LOCALE } from './createTranslator';\nimport {\n applyDocumentLocale,\n browserLanguages,\n DEFAULT_STORAGE_KEY,\n readStoredLocale,\n resolveInitialLocale,\n writeStoredLocale,\n} from './detectLocale';\nimport type { Catalogs, LocaleCode, LocaleOption, Translate } from './types';\n\nexport interface I18nValue {\n /** Resolve a key in the active locale (positional or named interpolation). */\n t: Translate;\n /** The active locale code. */\n locale: LocaleCode;\n /** Switch locale: re-renders consumers, persists the choice, updates `lang`/`dir`. */\n setLocale: (code: LocaleCode) => void;\n /** Every locale the app offers (for a switcher). */\n locales: readonly LocaleOption[];\n}\n\nconst I18nContext = createContext<I18nValue | null>(null);\n\nexport interface I18nProviderProps {\n /** Every locale's catalog. The fallback locale's catalog is required. */\n catalogs: Catalogs;\n /** The locales offered to the user, in display order. */\n locales: readonly LocaleOption[];\n /** Per-key fallback locale. Default `en`. */\n fallbackLocale?: LocaleCode;\n /** localStorage key for the persisted choice. Default `dloizides_locale`. */\n storageKey?: string;\n /** Force a locale (tests / SSR). When set, detection and persistence are skipped. */\n initialLocale?: LocaleCode;\n children: React.ReactNode;\n}\n\nexport function I18nProvider({\n catalogs,\n locales,\n fallbackLocale = DEFAULT_FALLBACK_LOCALE,\n storageKey = DEFAULT_STORAGE_KEY,\n initialLocale,\n children,\n}: I18nProviderProps): React.ReactElement {\n // Resolved once, lazily: stored choice → browser preference → fallback. Reading storage and\n // `navigator` inside the initializer keeps the first paint on the right locale (no flash of\n // English before a `useEffect` corrects it).\n const [locale, setLocaleState] = useState<LocaleCode>(() => {\n if (initialLocale) return initialLocale;\n const resolved = resolveInitialLocale(\n locales,\n readStoredLocale(storageKey),\n browserLanguages(),\n fallbackLocale,\n );\n applyDocumentLocale(locales, resolved);\n return resolved;\n });\n\n const setLocale = useCallback(\n (code: LocaleCode) => {\n setLocaleState(code);\n writeStoredLocale(storageKey, code);\n applyDocumentLocale(locales, code);\n },\n [locales, storageKey],\n );\n\n const t = useMemo(\n () => createTranslator(catalogs, locale, fallbackLocale),\n [catalogs, locale, fallbackLocale],\n );\n\n const value = useMemo<I18nValue>(\n () => ({ t, locale, setLocale, locales }),\n [t, locale, setLocale, locales],\n );\n\n return <I18nContext.Provider value={value}>{children}</I18nContext.Provider>;\n}\n\n/** Access the active locale + translate function. Throws outside an {@link I18nProvider}. */\nexport function useI18n(): I18nValue {\n const value = useContext(I18nContext);\n if (value === null) throw new Error('useI18n must be used inside an <I18nProvider>.');\n return value;\n}\n","/**\n * `LanguageSwitcher` — a compact segmented control of locale codes (EN · DE · ES · FR · PT), the\n * same shape as the v1 AML console's `#lang-switcher`.\n *\n * Colours come in as PROPS rather than from a theme context on purpose: it keeps this package\n * dependency-free (no peer dep on the UI kit or a design-token package), so any app — themed or\n * not — can drop it in. Themed apps pass their own tokens, e.g.\n * `colors={{ text: colors.textSecondary, activeText: '#fff', activeBackground: colors.primary }}`.\n */\nimport React, { useCallback } from 'react';\nimport { Pressable, StyleSheet, Text, View, type PressableStateCallbackType } from 'react-native';\n\nimport { useI18n } from './I18nProvider';\nimport type { LocaleCode } from './types';\n\nconst BORDER_RADIUS = 8;\nconst PADDING_H = 9;\nconst PADDING_V = 5;\nconst FONT_SIZE = 12;\nconst GAP = 2;\n/** WCAG 2.5.5: an interactive target must be at least 44×44 px. */\nconst MIN_TARGET = 44;\n\nexport interface LanguageSwitcherColors {\n /** Inactive label colour. */\n text: string;\n /** Active label colour. */\n activeText: string;\n /** Active pill background. */\n activeBackground: string;\n /** Hover/press background for an inactive option. Optional. */\n hoverBackground?: string;\n}\n\nexport interface LanguageSwitcherProps {\n colors: LanguageSwitcherColors;\n /** Accessible name for the group. Default `Language`. */\n accessibilityLabel?: string;\n testID?: string;\n}\n\n/**\n * react-native-web adds `hovered` to the Pressable style-callback state; the core react-native\n * types do not model it. Widen the state rather than casting the whole style callback.\n */\ntype PressableState = PressableStateCallbackType & { hovered?: boolean };\n\nconst styles = StyleSheet.create({\n row: { flexDirection: 'row', alignItems: 'center', gap: GAP },\n option: {\n paddingHorizontal: PADDING_H,\n paddingVertical: PADDING_V,\n borderRadius: BORDER_RADIUS,\n minWidth: MIN_TARGET,\n minHeight: MIN_TARGET,\n alignItems: 'center',\n justifyContent: 'center',\n },\n label: { fontSize: FONT_SIZE, fontWeight: '600' },\n});\n\nexport function LanguageSwitcher({\n colors,\n accessibilityLabel = 'Language',\n testID = 'language-switcher',\n}: LanguageSwitcherProps): React.ReactElement {\n const { locale, setLocale, locales } = useI18n();\n\n const select = useCallback((code: LocaleCode) => () => setLocale(code), [setLocale]);\n\n return (\n <View accessibilityRole=\"radiogroup\" aria-label={accessibilityLabel} style={styles.row} testID={testID}>\n {locales.map((option) => {\n const isActive = option.code === locale;\n return (\n <Pressable\n key={option.code}\n accessibilityHint={`Show the interface in ${option.label}`}\n accessibilityLabel={option.label}\n accessibilityRole=\"radio\"\n accessibilityState={{ selected: isActive }}\n aria-checked={isActive}\n style={({ hovered }: PressableState) => [\n styles.option,\n isActive\n ? { backgroundColor: colors.activeBackground }\n : hovered && colors.hoverBackground\n ? { backgroundColor: colors.hoverBackground }\n : null,\n ]}\n testID={`${testID}-${option.code}`}\n onPress={select(option.code)}\n >\n <Text style={[styles.label, { color: isActive ? colors.activeText : colors.text }]}>\n {option.label}\n </Text>\n </Pressable>\n );\n })}\n </View>\n );\n}\n","import i18n, { type i18n as I18nInstance, type Resource } from 'i18next';\nimport { initReactI18next } from 'react-i18next';\n\n/** Options for {@link initI18n}: the app's resources and languages; the package owns the init chain. */\nexport interface InitI18nOptions {\n resources: Resource;\n lng?: string;\n fallbackLng?: string;\n escapeValue?: boolean;\n defaultNS?: string | readonly string[];\n ns?: string | readonly string[];\n}\n\nconst DEFAULT_FALLBACK_LNG = 'en';\n\nconst ignoreInitRejection = (): void => undefined;\n\n/** Wires react-i18next into the i18next singleton, runs `.init()` and returns the singleton. */\nexport function initI18n(options: InitI18nOptions): I18nInstance {\n const {\n resources,\n lng,\n fallbackLng = DEFAULT_FALLBACK_LNG,\n escapeValue = false,\n defaultNS,\n ns,\n } = options;\n\n i18n\n .use(initReactI18next)\n .init({\n lng: lng ?? fallbackLng,\n fallbackLng,\n resources,\n ...(defaultNS !== undefined ? { defaultNS } : {}),\n ...(ns !== undefined ? { ns } : {}),\n interpolation: {\n escapeValue,\n },\n })\n .catch(ignoreInitRejection);\n\n return i18n;\n}\n"]}
1
+ {"version":3,"sources":["../src/createTranslator.ts","../src/detectLocale.ts","../src/I18nProvider.tsx","../src/LanguageSwitcher.tsx","../src/initI18n.ts","../src/createFormatMessage.ts"],"names":["useCallback","jsx"],"mappings":";;;;;;;AAOO,IAAM,uBAAA,GAA0B;AAGvC,SAAS,cAAc,KAAA,EAAsC;AAC3D,EAAA,OAAO,OAAO,UAAU,QAAA,IAAY,KAAA,KAAU,QAAQ,CAAC,KAAA,CAAM,QAAQ,KAAK,CAAA;AAC5E;AAMO,SAAS,gBAAA,CAAiB,UAAkB,MAAA,E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AAA,CAAM,mBAAmB,CAAA;AAE5B,EAAA,OAAO,IAAA;AACT;;;AClCO,SAAS,oBAAoB,UAAA,EAA8C;AAChF,EAAA,OAAO,CAAC,EAAA,EAAI,EAAA,EAAI,EAAA,EAAI,EAAA,KAAO;AACzB,IAAA,MAAM,UAAkC,EAAC;AACzC,IAAA,IAAI,EAAA,KAAO,MAAA,IAAa,EAAA,KAAO,IAAA,UAAc,EAAA,GAAK,EAAA;AAClD,IAAA,IAAI,EAAA,KAAO,MAAA,IAAa,EAAA,KAAO,IAAA,UAAc,EAAA,GAAK,EAAA;AAClD,IAAA,IAAI,EAAA,KAAO,MAAA,IAAa,EAAA,KAAO,IAAA,UAAc,EAAA,GAAK,EAAA;AAClD,IAAA,OAAO,UAAA,CAAW,CAAA,CAAE,EAAA,EAAI,OAAO,CAAA;AAAA,EACjC,CAAA;AACF","file":"index.mjs","sourcesContent":["/**\n * The pure core: catalog lookup + interpolation. No React, no storage, no side effects — so it is\n * trivially unit-testable and reusable outside a component tree (formatters, tests, server render).\n */\nimport type { Catalog, Catalogs, LocaleCode, NamedParams, Translate } from './types';\n\n/** The locale every catalog falls back to, per key. English is authoritative. */\nexport const DEFAULT_FALLBACK_LOCALE = 'en';\n\n/** True for a plain object of named params (as opposed to a positional string/number). */\nfunction isNamedParams(value: unknown): value is NamedParams {\n return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n\n/**\n * Fill `{name}` placeholders from a params object. An unmatched placeholder is left intact rather\n * than blanked, so a template/param mismatch is visible instead of silently losing text.\n */\nexport function interpolateNamed(template: string, params: NamedParams): string {\n return template.replace(/\\{(\\w+)\\}/g, (match, name: string) =>\n Object.prototype.hasOwnProperty.call(params, name) ? String(params[name]) : match,\n );\n}\n\n/** Fill `{0}`, `{1}`, `{2}` from positional args (the shared UI kit's calling convention). */\nexport function interpolatePositional(\n template: string,\n params: ReadonlyArray<string | number | undefined>,\n): string {\n return params.reduce<string>(\n (acc, param, index) => (param === undefined ? acc : acc.split(`{${index}}`).join(String(param))),\n template,\n );\n}\n\n/**\n * Look a key up in `locale`, then in `fallbackLocale`, then give back the key itself.\n *\n * The fallback is **per key**, not per catalog: a partially translated locale still shows its\n * translated keys and only falls back for the ones it is missing. That is what lets a new locale\n * ship incrementally instead of all-or-nothing.\n */\nexport function lookup(\n catalogs: Catalogs,\n locale: LocaleCode,\n key: string,\n fallbackLocale: LocaleCode = DEFAULT_FALLBACK_LOCALE,\n): string {\n const active: Catalog | undefined = catalogs[locale];\n const fallback: Catalog | undefined = catalogs[fallbackLocale];\n return active?.[key] ?? fallback?.[key] ?? key;\n}\n\n/**\n * Build a {@link Translate} bound to one locale. Supports BOTH interpolation styles — see the\n * `Translate` docs for why (positional = the UI kit; named = hand-written app catalogs).\n */\nexport function createTranslator(\n catalogs: Catalogs,\n locale: LocaleCode,\n fallbackLocale: LocaleCode = DEFAULT_FALLBACK_LOCALE,\n): Translate {\n return (key, p1, p2, p3) => {\n const template = lookup(catalogs, locale, key, fallbackLocale);\n if (isNamedParams(p1)) return interpolateNamed(template, p1);\n return interpolatePositional(template, [p1, p2, p3]);\n };\n}\n","/**\n * Locale resolution + persistence.\n *\n * The resolution order mirrors the v1 AML console (`shared/i18n.js`): a previously stored choice\n * wins, else the browser's preferred language (primary subtag only — `de-AT` → `de`), else the\n * fallback. Storage is best-effort: private mode / a native runtime with no `localStorage` must not\n * throw, it just means the choice does not persist.\n */\nimport type { LocaleCode, LocaleOption } from './types';\n\n/** Default persistence key. Override per app so two apps on one origin don't fight. */\nexport const DEFAULT_STORAGE_KEY = 'dloizides_locale';\n\n/** True when `code` is one of the app's supported locales. Pure. */\nexport function isSupported(locales: readonly LocaleOption[], code: string): boolean {\n return locales.some((l) => l.code === code);\n}\n\n/** `de-AT` → `de`; `EN` → `en`. Pure. */\nexport function primarySubtag(tag: string): string {\n return String(tag).toLowerCase().split('-')[0] ?? '';\n}\n\n/**\n * Pick the locale to start in: stored → browser-preferred → fallback. Pure — the caller supplies\n * the stored value and the browser's language list, which keeps this testable with no globals.\n */\nexport function resolveInitialLocale(\n locales: readonly LocaleOption[],\n stored: string | null,\n preferred: readonly string[],\n fallback: LocaleCode,\n): LocaleCode {\n if (stored && isSupported(locales, stored)) return stored;\n for (const tag of preferred) {\n const primary = primarySubtag(tag);\n if (isSupported(locales, primary)) return primary;\n }\n return fallback;\n}\n\n/** The browser's preferred languages, most-preferred first. `[]` off-web. */\nexport function browserLanguages(): string[] {\n if (typeof navigator === 'undefined') return [];\n const nav = navigator as Navigator & { userLanguage?: string };\n const list = Array.isArray(nav.languages) && nav.languages.length > 0 ? nav.languages : [];\n const single = nav.language ?? nav.userLanguage;\n return list.length > 0 ? [...list] : single ? [single] : [];\n}\n\n/** Read the persisted locale. Never throws (private mode, native, SSR). */\nexport function readStoredLocale(storageKey: string): string | null {\n try {\n if (typeof localStorage === 'undefined') return null;\n return localStorage.getItem(storageKey);\n } catch {\n return null;\n }\n}\n\n/** Persist the locale. Never throws — a failure just means it won't be remembered. */\nexport function writeStoredLocale(storageKey: string, code: LocaleCode): void {\n try {\n if (typeof localStorage === 'undefined') return;\n localStorage.setItem(storageKey, code);\n } catch {\n /* private mode / no storage — the choice simply does not persist */\n }\n}\n\n/**\n * Reflect the locale on the document so CSS can react (`:lang()`, `[dir=\"rtl\"]`). Web-only no-op\n * elsewhere. This is the RTL hook: ship an `rtl: true` locale and the layout can mirror itself\n * without any component changes.\n */\nexport function applyDocumentLocale(locales: readonly LocaleOption[], code: LocaleCode): void {\n if (typeof document === 'undefined') return;\n const option = locales.find((l) => l.code === code);\n document.documentElement.lang = code;\n document.documentElement.dir = option?.rtl ? 'rtl' : 'ltr';\n}\n","/**\n * `I18nProvider` — holds the active locale, exposes a memoized {@link Translate}, persists the\n * user's choice, and reflects `lang`/`dir` on the document.\n *\n * Wiring it to the rest of the shared kit is one line, because {@link Translate} is\n * signature-compatible with the kit's `t` contract:\n *\n * ```tsx\n * <I18nProvider catalogs={CATALOGS} locales={LOCALES}>\n * <KitBridge /> // const { t } = useI18n(); return <UiProvider theme={theme} t={t}>…\n * </I18nProvider>\n * ```\n */\nimport React, { createContext, useCallback, useContext, useMemo, useState } from 'react';\n\nimport { createTranslator, DEFAULT_FALLBACK_LOCALE } from './createTranslator';\nimport {\n applyDocumentLocale,\n browserLanguages,\n DEFAULT_STORAGE_KEY,\n readStoredLocale,\n resolveInitialLocale,\n writeStoredLocale,\n} from './detectLocale';\nimport type { Catalogs, LocaleCode, LocaleOption, Translate } from './types';\n\nexport interface I18nValue {\n /** Resolve a key in the active locale (positional or named interpolation). */\n t: Translate;\n /** The active locale code. */\n locale: LocaleCode;\n /** Switch locale: re-renders consumers, persists the choice, updates `lang`/`dir`. */\n setLocale: (code: LocaleCode) => void;\n /** Every locale the app offers (for a switcher). */\n locales: readonly LocaleOption[];\n}\n\nconst I18nContext = createContext<I18nValue | null>(null);\n\nexport interface I18nProviderProps {\n /** Every locale's catalog. The fallback locale's catalog is required. */\n catalogs: Catalogs;\n /** The locales offered to the user, in display order. */\n locales: readonly LocaleOption[];\n /** Per-key fallback locale. Default `en`. */\n fallbackLocale?: LocaleCode;\n /** localStorage key for the persisted choice. Default `dloizides_locale`. */\n storageKey?: string;\n /** Force a locale (tests / SSR). When set, detection and persistence are skipped. */\n initialLocale?: LocaleCode;\n children: React.ReactNode;\n}\n\nexport function I18nProvider({\n catalogs,\n locales,\n fallbackLocale = DEFAULT_FALLBACK_LOCALE,\n storageKey = DEFAULT_STORAGE_KEY,\n initialLocale,\n children,\n}: I18nProviderProps): React.ReactElement {\n // Resolved once, lazily: stored choice → browser preference → fallback. Reading storage and\n // `navigator` inside the initializer keeps the first paint on the right locale (no flash of\n // English before a `useEffect` corrects it).\n const [locale, setLocaleState] = useState<LocaleCode>(() => {\n if (initialLocale) return initialLocale;\n const resolved = resolveInitialLocale(\n locales,\n readStoredLocale(storageKey),\n browserLanguages(),\n fallbackLocale,\n );\n applyDocumentLocale(locales, resolved);\n return resolved;\n });\n\n const setLocale = useCallback(\n (code: LocaleCode) => {\n setLocaleState(code);\n writeStoredLocale(storageKey, code);\n applyDocumentLocale(locales, code);\n },\n [locales, storageKey],\n );\n\n const t = useMemo(\n () => createTranslator(catalogs, locale, fallbackLocale),\n [catalogs, locale, fallbackLocale],\n );\n\n const value = useMemo<I18nValue>(\n () => ({ t, locale, setLocale, locales }),\n [t, locale, setLocale, locales],\n );\n\n return <I18nContext.Provider value={value}>{children}</I18nContext.Provider>;\n}\n\n/** Access the active locale + translate function. Throws outside an {@link I18nProvider}. */\nexport function useI18n(): I18nValue {\n const value = useContext(I18nContext);\n if (value === null) throw new Error('useI18n must be used inside an <I18nProvider>.');\n return value;\n}\n","/**\n * `LanguageSwitcher` — a compact segmented control of locale codes (EN · DE · ES · FR · PT), the\n * same shape as the v1 AML console's `#lang-switcher`.\n *\n * Colours come in as PROPS rather than from a theme context on purpose: it keeps this package\n * dependency-free (no peer dep on the UI kit or a design-token package), so any app — themed or\n * not — can drop it in. Themed apps pass their own tokens, e.g.\n * `colors={{ text: colors.textSecondary, activeText: '#fff', activeBackground: colors.primary }}`.\n */\nimport React, { useCallback } from 'react';\nimport { Pressable, StyleSheet, Text, View, type PressableStateCallbackType } from 'react-native';\n\nimport { useI18n } from './I18nProvider';\nimport type { LocaleCode } from './types';\n\nconst BORDER_RADIUS = 8;\nconst PADDING_H = 9;\nconst PADDING_V = 5;\nconst FONT_SIZE = 12;\nconst GAP = 2;\n/** WCAG 2.5.5: an interactive target must be at least 44×44 px. */\nconst MIN_TARGET = 44;\n\nexport interface LanguageSwitcherColors {\n /** Inactive label colour. */\n text: string;\n /** Active label colour. */\n activeText: string;\n /** Active pill background. */\n activeBackground: string;\n /** Hover/press background for an inactive option. Optional. */\n hoverBackground?: string;\n}\n\nexport interface LanguageSwitcherProps {\n colors: LanguageSwitcherColors;\n /** Accessible name for the group. Default `Language`. */\n accessibilityLabel?: string;\n testID?: string;\n}\n\n/**\n * react-native-web adds `hovered` to the Pressable style-callback state; the core react-native\n * types do not model it. Widen the state rather than casting the whole style callback.\n */\ntype PressableState = PressableStateCallbackType & { hovered?: boolean };\n\nconst styles = StyleSheet.create({\n row: { flexDirection: 'row', alignItems: 'center', gap: GAP },\n option: {\n paddingHorizontal: PADDING_H,\n paddingVertical: PADDING_V,\n borderRadius: BORDER_RADIUS,\n minWidth: MIN_TARGET,\n minHeight: MIN_TARGET,\n alignItems: 'center',\n justifyContent: 'center',\n },\n label: { fontSize: FONT_SIZE, fontWeight: '600' },\n});\n\nexport function LanguageSwitcher({\n colors,\n accessibilityLabel = 'Language',\n testID = 'language-switcher',\n}: LanguageSwitcherProps): React.ReactElement {\n const { locale, setLocale, locales } = useI18n();\n\n const select = useCallback((code: LocaleCode) => () => setLocale(code), [setLocale]);\n\n return (\n <View accessibilityRole=\"radiogroup\" aria-label={accessibilityLabel} style={styles.row} testID={testID}>\n {locales.map((option) => {\n const isActive = option.code === locale;\n return (\n <Pressable\n key={option.code}\n accessibilityHint={`Show the interface in ${option.label}`}\n accessibilityLabel={option.label}\n accessibilityRole=\"radio\"\n accessibilityState={{ selected: isActive }}\n aria-checked={isActive}\n style={({ hovered }: PressableState) => [\n styles.option,\n isActive\n ? { backgroundColor: colors.activeBackground }\n : hovered && colors.hoverBackground\n ? { backgroundColor: colors.hoverBackground }\n : null,\n ]}\n testID={`${testID}-${option.code}`}\n onPress={select(option.code)}\n >\n <Text style={[styles.label, { color: isActive ? colors.activeText : colors.text }]}>\n {option.label}\n </Text>\n </Pressable>\n );\n })}\n </View>\n );\n}\n","import i18n, { type i18n as I18nInstance, type Resource } from 'i18next';\nimport { initReactI18next } from 'react-i18next';\n\n/** Options for {@link initI18n}: the app's resources and languages; the package owns the init chain. */\nexport interface InitI18nOptions {\n resources: Resource;\n lng?: string;\n fallbackLng?: string;\n escapeValue?: boolean;\n defaultNS?: string | readonly string[];\n ns?: string | readonly string[];\n}\n\nconst DEFAULT_FALLBACK_LNG = 'en';\n\nconst ignoreInitRejection = (): void => undefined;\n\n/** Wires react-i18next into the i18next singleton, runs `.init()` and returns the singleton. */\nexport function initI18n(options: InitI18nOptions): I18nInstance {\n const {\n resources,\n lng,\n fallbackLng = DEFAULT_FALLBACK_LNG,\n escapeValue = false,\n defaultNS,\n ns,\n } = options;\n\n i18n\n .use(initReactI18next)\n .init({\n lng: lng ?? fallbackLng,\n fallbackLng,\n resources,\n ...(defaultNS !== undefined ? { defaultNS } : {}),\n ...(ns !== undefined ? { ns } : {}),\n interpolation: {\n escapeValue,\n },\n })\n .catch(ignoreInitRejection);\n\n return i18n;\n}\n","/** The slice of an i18next instance that {@link createFormatMessage} needs. */\nexport interface MessageTranslator {\n t: (id: string, options: Record<string, string>) => string;\n}\n\n/** Positional-parameter message formatter: `FM(id, p1, p2, p3)` maps to `{{p1}}`..`{{p3}}`. */\nexport type FormatMessage = (id: string, p1?: string, p2?: string, p3?: string) => string;\n\n/** Builds the portal `FM()` helper over an initialised i18next instance. */\nexport function createFormatMessage(translator: MessageTranslator): FormatMessage {\n return (id, p1, p2, p3) => {\n const options: Record<string, string> = {};\n if (p1 !== undefined && p1 !== null) options.p1 = p1;\n if (p2 !== undefined && p2 !== null) options.p2 = p2;\n if (p3 !== undefined && p3 !== null) options.p3 = p3;\n return translator.t(id, options);\n };\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dloizides/i18n",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Tiny, dependency-free i18n runtime for React / React Native (RN-web) apps — locale detection, persisted choice, per-key English fallback, positional AND named interpolation, and a themable LanguageSwitcher. Its translate function matches the @dloizides UI-kit's `t` contract, so dropping it into UiProvider localizes every shared component for free. Part of the dloizides.com shared kit.",
5
5
  "keywords": [
6
6
  "react",