@krizaka/i18n 0.1.0-beta.0 → 0.1.0-beta.1

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 (2) hide show
  1. package/dist/react.d.ts +2 -70
  2. package/package.json +2 -2
package/dist/react.d.ts CHANGED
@@ -1,75 +1,7 @@
1
1
  import * as react from 'react';
2
2
  import { ReactNode } from 'react';
3
- import { PluralForms } from '@krizaka/intl/plural';
4
-
5
- /**
6
- * The types of a message catalogue. `en.json` (the reference) gives the shape; every other locale has the same one.
7
- * A key is the dotted path to a leaf (`"auction.phase.SOLD"`); a leaf is a message string or a set of plural forms.
8
- */
9
-
10
- /** A value a message may hold: a string, a plural set, a nested branch, or a list (long structured content). */
11
- type MessageValue = string | Dictionary | readonly MessageValue[];
12
- /** A message catalogue, as `import en from "./messages/en.json"` types it. */
13
- interface Dictionary {
14
- readonly [key: string]: MessageValue;
15
- }
16
- /** A value a placeholder accepts. */
17
- type PlaceholderValue = string | number;
18
- /** The values handed to `format` / `t`: `{ count: 3, name: "Ada" }`. */
19
- type Values = Readonly<Record<string, PlaceholderValue | null | undefined>>;
20
- /** The plural categories a plural set may carry (`zero` is the explicit "exactly 0" form). */
21
- type PluralCategory = keyof PluralForms;
22
- type Join<P extends string, K extends string> = P extends "" ? K : `${P}.${K}`;
23
- /** True when T is a plural set: an object whose keys are plural categories, `other` included. */
24
- type IsPlural<T> = T extends {
25
- other: string;
26
- } ? Exclude<keyof T, PluralCategory> extends never ? true : false : false;
27
- /**
28
- * Every key of a catalogue: the dotted path to each string and each plural set. Lists are not keys (read them as a
29
- * branch with `getDictionary`). `MessageKey<typeof en>` → `"home.title" | "auction.bids" | …`.
30
- */
31
- type MessageKey<D, P extends string = ""> = D extends readonly unknown[] ? never : {
32
- [K in keyof D & string]: D[K] extends string ? Join<P, K> : IsPlural<D[K]> extends true ? Join<P, K> : D[K] extends readonly unknown[] ? never : D[K] extends object ? MessageKey<D[K], Join<P, K>> : never;
33
- }[keyof D & string];
34
- /** The message at a key: `MessageAt<typeof en, "home.title">`. */
35
- type MessageAt<D, K extends string> = K extends `${infer H}.${infer R}` ? H extends keyof D ? MessageAt<D[H], R> : never : K extends keyof D ? D[K] : never;
36
- /** The `{placeholders}` of a literal message type: `Placeholders<"{count} of {total}">` → `"count" | "total"`. */
37
- type Placeholders<S extends string> = S extends `${string}{${infer N}}${infer R}` ? N extends `${string}${" " | "{"}${string}` ? Placeholders<R> : N | Placeholders<R> : never;
38
- /**
39
- * The arguments `t` takes after the key. A message typed as a literal (a catalogue written `as const`) requires its
40
- * placeholders; a plural set requires `count`; a message typed `string` (a JSON import widens literals) takes any values.
41
- */
42
- type ValuesArgs<M> = IsPlural<M> extends true ? [values: Values & {
43
- readonly count: number;
44
- }] : M extends string ? string extends M ? [values?: Values] : [Placeholders<M>] extends [never] ? [values?: Values] : [values: Values & {
45
- readonly [N in Placeholders<M>]: PlaceholderValue;
46
- }] : [values?: Values];
47
- /** A translator bound to one locale: `t("auction.bids", { count: 3 })`. */
48
- type Translate<D> = <K extends MessageKey<D>>(key: K, ...values: ValuesArgs<MessageAt<D, K>>) => string;
49
-
50
- /**
51
- * Replaces the `{name}` placeholders of a message: `format("{count} repositories", { count: 23 })` → `"23 repositories"`.
52
- * A placeholder without a value (missing, `null` or `undefined`) stays as written, so a gap is visible, never silent.
53
- */
54
- declare function format(message: string, values?: Values): string;
55
-
56
- interface I18n<D extends Dictionary, L extends string = string> {
57
- /** The supported locales, in the order the catalogues were given. */
58
- readonly locales: readonly L[];
59
- readonly defaultLocale: L;
60
- /** True when the value is a supported locale. */
61
- isLocale(value: unknown): value is L;
62
- /** Narrows any value (a route segment, a cookie) to a supported locale: the default one for anything else. */
63
- asLocale(value: unknown): L;
64
- /** The whole catalogue of a locale (the default one for an unknown locale) — for Server Components and branches. */
65
- getDictionary(locale: unknown): D;
66
- /** The translator bound to a locale: `translator("fr")("auction.bids", { count: 3 })`. */
67
- translator(locale: unknown): Translate<D>;
68
- /** The translator of the default locale. */
69
- t: Translate<D>;
70
- /** `{placeholder}` replacement, the same function as the package's `format`. */
71
- format: typeof format;
72
- }
3
+ import { Dictionary, Translate, format, I18n } from './index.js';
4
+ import '@krizaka/intl/plural';
73
5
 
74
6
  /** What `useI18n()` returns. */
75
7
  interface I18nContextValue<D extends Dictionary, L extends string> {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@krizaka/i18n",
3
- "version": "0.1.0-beta.0",
3
+ "version": "0.1.0-beta.1",
4
4
  "description": "The i18n engine of every Krizaka app: typed message keys and placeholders from en.json, format(), plurals through @krizaka/intl, <Rich> (<b>, <a>), a React provider, and the krizaka-i18n CLI that checks catalogues and finds hard-coded strings.",
5
5
  "keywords": [
6
6
  "krizaka",
@@ -123,6 +123,6 @@
123
123
  "typecheck": "tsc --noEmit",
124
124
  "test": "vitest run --coverage",
125
125
  "size": "size-limit",
126
- "publint": "publint && attw --pack . --profile esm-only"
126
+ "publint": "publint && attw --pack . --profile esm-only && tsc -p test/consumer/tsconfig.json"
127
127
  }
128
128
  }