@opentf/afw-i18n 1.34.0 → 1.35.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.
Files changed (3) hide show
  1. package/i18n.js +0 -0
  2. package/index.d.ts +68 -0
  3. package/package.json +10 -5
package/i18n.js CHANGED
Binary file
package/index.d.ts ADDED
@@ -0,0 +1,68 @@
1
+ // Types for @opentf/afw-i18n: ICU messages, Intl formatters and the <T> component.
2
+
3
+ /** A locale's messages: flat keys, or nested objects read with dotted keys. */
4
+ export interface Catalog {
5
+ [key: string]: string | Catalog;
6
+ }
7
+
8
+ /** A locale loader's result: the catalog, or a module whose default export is one. */
9
+ export type CatalogModule = Catalog | { default: Catalog };
10
+
11
+ export interface I18nOptions {
12
+ /** The supported locales; defaults to the keys of `messages`. */
13
+ locales?: string[];
14
+ /** The fallback locale; defaults to the first locale, then `"en"`. */
15
+ defaultLocale?: string;
16
+ /** Catalogs loaded up front: `{ en: { … }, fr: { … } }`. */
17
+ messages?: Record<string, Catalog>;
18
+ /** Load a locale's catalog on demand, for per-locale code splitting. */
19
+ load?: (locale: string) => CatalogModule | Promise<CatalogModule>;
20
+ }
21
+
22
+ /** The store `createI18n` returns (mostly for tests). */
23
+ export interface I18nStore {
24
+ locales: string[];
25
+ defaultLocale: string;
26
+ catalogs: Map<string, Catalog>;
27
+ load: ((locale: string) => CatalogModule | Promise<CatalogModule>) | null;
28
+ pending: Map<string, Promise<void>>;
29
+ }
30
+
31
+ /** ICU argument values for `t()` and `<T>`. */
32
+ export type MessageValues = Record<string, unknown>;
33
+
34
+ /** Register the app's catalogs and locales. */
35
+ export function createI18n(options?: I18nOptions): I18nStore;
36
+ /** The active locale: the router's locale, else the default locale, else `"en"`. */
37
+ export function getLocale(): string;
38
+ /** Load and cache a locale's catalog through the registered `load`. */
39
+ export function loadLocale(locale: string): Promise<void>;
40
+ /** The configured locales and default, or `null` before `createI18n` runs. */
41
+ export function locales(): { locales: string[]; defaultLocale: string } | null;
42
+ /** Set a locale's catalog directly (SSR/SSG preload and tests). */
43
+ export function setCatalog(locale: string, catalog: Catalog | null | undefined): void;
44
+ /**
45
+ * Translate `key`, ICU-formatting it with `values`. Reactive on the active locale.
46
+ * Falls back to the default locale, then to the key itself.
47
+ */
48
+ export function t(key: string, values?: MessageValues): string;
49
+
50
+ /** Locale-aware formatters over `Intl`, following the active locale. */
51
+ export const fmt: {
52
+ number(value: number | bigint, options?: Intl.NumberFormatOptions): string;
53
+ currency(value: number | bigint, currency: string, options?: Intl.NumberFormatOptions): string;
54
+ percent(value: number | bigint, options?: Intl.NumberFormatOptions): string;
55
+ date(value: Date | number, options?: Intl.DateTimeFormatOptions): string;
56
+ relativeTime(value: number, unit: Intl.RelativeTimeFormatUnit, options?: Intl.RelativeTimeFormatOptions): string;
57
+ plural(value: number, options?: Intl.PluralRulesOptions): Intl.LDMLPluralRule;
58
+ list(values: Iterable<string>, options?: Intl.ListFormatOptions): string;
59
+ };
60
+
61
+ export interface TProps {
62
+ /** The message key. */
63
+ id: string;
64
+ /** ICU argument values. */
65
+ values?: MessageValues;
66
+ }
67
+ /** `<T id="greeting" values={{ name }} />`: the translated message in a `<span>`. */
68
+ export function T(props: TProps): Node;
package/package.json CHANGED
@@ -1,18 +1,23 @@
1
1
  {
2
2
  "name": "@opentf/afw-i18n",
3
- "version": "1.34.0",
3
+ "version": "1.35.0",
4
4
  "description": "ICU messages, Intl formatters and locale routing for AFW.",
5
5
  "main": "index.js",
6
6
  "module": "index.js",
7
+ "types": "./index.d.ts",
7
8
  "type": "module",
8
9
  "scripts": {
9
10
  "build": "cp ../../LICENSE LICENSE"
10
11
  },
11
12
  "exports": {
12
- ".": "./index.js"
13
+ ".": {
14
+ "types": "./index.d.ts",
15
+ "default": "./index.js"
16
+ }
13
17
  },
14
18
  "files": [
15
19
  "index.js",
20
+ "index.d.ts",
16
21
  "i18n.js",
17
22
  "format.js",
18
23
  "T.jsx",
@@ -39,13 +44,13 @@
39
44
  "intl-messageformat": "^10.7.0"
40
45
  },
41
46
  "peerDependencies": {
42
- "@opentf/afw": "0.39.0"
47
+ "@opentf/afw": "0.40.0"
43
48
  },
44
49
  "publishConfig": {
45
50
  "access": "public"
46
51
  },
47
52
  "devDependencies": {
48
- "@opentf/afw": "0.39.0",
49
- "@opentf/afw-test": "1.34.0"
53
+ "@opentf/afw": "0.40.0",
54
+ "@opentf/afw-test": "1.36.0"
50
55
  }
51
56
  }