meowjsx 0.3.0-alpha.1 → 0.3.0-alpha.2
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/README.md +159 -0
- package/dist/src/context.d.ts +14 -0
- package/dist/src/context.js +36 -0
- package/dist/src/i18n-audit.d.ts +127 -0
- package/dist/src/i18n-audit.js +906 -0
- package/dist/src/i18n-message.d.ts +42 -0
- package/dist/src/i18n-message.js +252 -0
- package/dist/src/i18n.d.ts +151 -0
- package/dist/src/i18n.js +346 -0
- package/dist/src/keyed.js +7 -1
- package/dist/src/reactive.js +7 -6
- package/dist/src/registry.js +2 -2
- package/package.json +9 -1
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The message format parser shared by `meowjsx/i18n` (run time) and `meowjsx/i18n/audit`
|
|
3
|
+
* (build time), so the two cannot disagree about what a template contains. Internal: it is
|
|
4
|
+
* not a package export. DOM-free, IO-free and independent of the renderer.
|
|
5
|
+
*/
|
|
6
|
+
export declare const PLURAL_CATEGORIES: ReadonlySet<string>;
|
|
7
|
+
export type MessageNode = {
|
|
8
|
+
readonly type: "text";
|
|
9
|
+
readonly value: string;
|
|
10
|
+
} | {
|
|
11
|
+
readonly type: "arg";
|
|
12
|
+
readonly name: string;
|
|
13
|
+
} | {
|
|
14
|
+
readonly type: "pound";
|
|
15
|
+
} | {
|
|
16
|
+
readonly type: "plural";
|
|
17
|
+
readonly name: string;
|
|
18
|
+
/** `selectordinal` selects with ordinal rules. */
|
|
19
|
+
readonly ordinal: boolean;
|
|
20
|
+
readonly exact: ReadonlyMap<number, readonly MessageNode[]>;
|
|
21
|
+
readonly categories: ReadonlyMap<string, readonly MessageNode[]>;
|
|
22
|
+
} | {
|
|
23
|
+
readonly type: "select";
|
|
24
|
+
readonly name: string;
|
|
25
|
+
readonly branches: ReadonlyMap<string, readonly MessageNode[]>;
|
|
26
|
+
} | {
|
|
27
|
+
readonly type: "tag";
|
|
28
|
+
readonly name: string;
|
|
29
|
+
readonly children: readonly MessageNode[];
|
|
30
|
+
};
|
|
31
|
+
export declare class ParseFailure extends Error {
|
|
32
|
+
}
|
|
33
|
+
export interface ParseOptions {
|
|
34
|
+
/** Interpret `<name>...</name>` as a rich tag. Without it `<` is ordinary text. */
|
|
35
|
+
readonly rich?: boolean;
|
|
36
|
+
}
|
|
37
|
+
export declare function parseTemplate(template: string, options?: ParseOptions): readonly MessageNode[];
|
|
38
|
+
/** Argument names and rich tag names a parsed template uses, each sorted and unique. */
|
|
39
|
+
export declare function describeNodes(nodes: readonly MessageNode[]): {
|
|
40
|
+
names: string[];
|
|
41
|
+
tags: string[];
|
|
42
|
+
};
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The message format parser shared by `meowjsx/i18n` (run time) and `meowjsx/i18n/audit`
|
|
3
|
+
* (build time), so the two cannot disagree about what a template contains. Internal: it is
|
|
4
|
+
* not a package export. DOM-free, IO-free and independent of the renderer.
|
|
5
|
+
*/
|
|
6
|
+
export const PLURAL_CATEGORIES = new Set([
|
|
7
|
+
"zero",
|
|
8
|
+
"one",
|
|
9
|
+
"two",
|
|
10
|
+
"few",
|
|
11
|
+
"many",
|
|
12
|
+
"other",
|
|
13
|
+
]);
|
|
14
|
+
export class ParseFailure extends Error {
|
|
15
|
+
}
|
|
16
|
+
const TAG_NAME = /^[A-Za-z_][A-Za-z0-9_-]*/;
|
|
17
|
+
export function parseTemplate(template, options = {}) {
|
|
18
|
+
const rich = options.rich === true;
|
|
19
|
+
let index = 0;
|
|
20
|
+
const fail = (reason) => {
|
|
21
|
+
throw new ParseFailure(`${reason} at position ${index}`);
|
|
22
|
+
};
|
|
23
|
+
const skipSpace = () => {
|
|
24
|
+
while (index < template.length && /\s/.test(template[index]))
|
|
25
|
+
index += 1;
|
|
26
|
+
};
|
|
27
|
+
const readToken = (stop) => {
|
|
28
|
+
const start = index;
|
|
29
|
+
while (index < template.length && !stop.test(template[index]))
|
|
30
|
+
index += 1;
|
|
31
|
+
return template.slice(start, index);
|
|
32
|
+
};
|
|
33
|
+
/** The tag name when `<name>` or `</name>` starts at `index`; `undefined` for plain text. */
|
|
34
|
+
const tagAt = () => {
|
|
35
|
+
if (template[index] !== "<")
|
|
36
|
+
return undefined;
|
|
37
|
+
const closing = template[index + 1] === "/";
|
|
38
|
+
const start = index + (closing ? 2 : 1);
|
|
39
|
+
const name = TAG_NAME.exec(template.slice(start))?.[0];
|
|
40
|
+
if (name === undefined || template[start + name.length] !== ">")
|
|
41
|
+
return undefined;
|
|
42
|
+
return { closing, name, length: start + name.length + 1 - index };
|
|
43
|
+
};
|
|
44
|
+
function parseNodes(inPlural, nested, closing) {
|
|
45
|
+
const nodes = [];
|
|
46
|
+
let text = "";
|
|
47
|
+
const flush = () => {
|
|
48
|
+
if (text)
|
|
49
|
+
nodes.push({ type: "text", value: text });
|
|
50
|
+
text = "";
|
|
51
|
+
};
|
|
52
|
+
while (index < template.length) {
|
|
53
|
+
const char = template[index];
|
|
54
|
+
if (char === "'") {
|
|
55
|
+
const next = template[index + 1];
|
|
56
|
+
if (next === "'") {
|
|
57
|
+
text += "'";
|
|
58
|
+
index += 2;
|
|
59
|
+
}
|
|
60
|
+
else if (next === "{" || next === "}" || next === "#") {
|
|
61
|
+
index += 1;
|
|
62
|
+
// A quoted section runs to the next apostrophe, or to the end of the text.
|
|
63
|
+
while (index < template.length) {
|
|
64
|
+
if (template[index] === "'") {
|
|
65
|
+
if (template[index + 1] === "'") {
|
|
66
|
+
text += "'";
|
|
67
|
+
index += 2;
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
index += 1;
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
text += template[index];
|
|
74
|
+
index += 1;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
text += "'";
|
|
79
|
+
index += 1;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
else if (char === "{") {
|
|
83
|
+
flush();
|
|
84
|
+
index += 1;
|
|
85
|
+
nodes.push(parseArgument(inPlural));
|
|
86
|
+
}
|
|
87
|
+
else if (char === "}") {
|
|
88
|
+
if (!nested)
|
|
89
|
+
fail("Unmatched }");
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
else if (char === "#" && inPlural) {
|
|
93
|
+
flush();
|
|
94
|
+
nodes.push({ type: "pound" });
|
|
95
|
+
index += 1;
|
|
96
|
+
}
|
|
97
|
+
else if (rich && char === "<" && tagAt()) {
|
|
98
|
+
const tag = tagAt();
|
|
99
|
+
if (tag.closing) {
|
|
100
|
+
if (tag.name !== closing) {
|
|
101
|
+
fail(closing === undefined
|
|
102
|
+
? `Unexpected closing tag </${tag.name}>`
|
|
103
|
+
: `Closing tag </${tag.name}> does not match <${closing}>`);
|
|
104
|
+
}
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
flush();
|
|
108
|
+
index += tag.length;
|
|
109
|
+
const children = parseNodes(inPlural, nested, tag.name);
|
|
110
|
+
const end = tagAt();
|
|
111
|
+
if (!end || !end.closing || end.name !== tag.name)
|
|
112
|
+
fail(`Unclosed tag <${tag.name}>`);
|
|
113
|
+
index += end.length;
|
|
114
|
+
nodes.push({ type: "tag", name: tag.name, children });
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
text += char;
|
|
118
|
+
index += 1;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
flush();
|
|
122
|
+
return nodes;
|
|
123
|
+
}
|
|
124
|
+
function parseArgument(inPlural) {
|
|
125
|
+
skipSpace();
|
|
126
|
+
const name = readToken(/[\s,{}]/);
|
|
127
|
+
if (!name)
|
|
128
|
+
fail("Missing argument name");
|
|
129
|
+
skipSpace();
|
|
130
|
+
if (template[index] === "}") {
|
|
131
|
+
index += 1;
|
|
132
|
+
return { type: "arg", name };
|
|
133
|
+
}
|
|
134
|
+
if (template[index] !== ",")
|
|
135
|
+
return fail("Expected , or }");
|
|
136
|
+
index += 1;
|
|
137
|
+
skipSpace();
|
|
138
|
+
const kind = readToken(/[\s,{}]/);
|
|
139
|
+
if (kind !== "plural" && kind !== "selectordinal" && kind !== "select") {
|
|
140
|
+
return fail(`Unsupported argument type "${kind}"`);
|
|
141
|
+
}
|
|
142
|
+
skipSpace();
|
|
143
|
+
if (template[index] !== ",")
|
|
144
|
+
return fail(`Expected , after ${kind}`);
|
|
145
|
+
index += 1;
|
|
146
|
+
return kind === "select"
|
|
147
|
+
? parseSelectBranches(name, inPlural)
|
|
148
|
+
: parsePluralBranches(name, kind === "selectordinal");
|
|
149
|
+
}
|
|
150
|
+
function parseBranchBody(inPlural, kind) {
|
|
151
|
+
index += 1;
|
|
152
|
+
const body = parseNodes(inPlural, true);
|
|
153
|
+
if (template[index] !== "}")
|
|
154
|
+
return fail(`Unclosed ${kind} branch`);
|
|
155
|
+
index += 1;
|
|
156
|
+
return body;
|
|
157
|
+
}
|
|
158
|
+
function parsePluralBranches(name, ordinal) {
|
|
159
|
+
const kind = ordinal ? "selectordinal" : "plural";
|
|
160
|
+
const exact = new Map();
|
|
161
|
+
const categories = new Map();
|
|
162
|
+
for (;;) {
|
|
163
|
+
skipSpace();
|
|
164
|
+
if (index >= template.length)
|
|
165
|
+
return fail(`Unclosed ${kind} argument`);
|
|
166
|
+
if (template[index] === "}") {
|
|
167
|
+
index += 1;
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
const selector = readToken(/[\s{}]/);
|
|
171
|
+
skipSpace();
|
|
172
|
+
if (!selector || template[index] !== "{")
|
|
173
|
+
return fail("Expected a selector and {");
|
|
174
|
+
const body = parseBranchBody(true, kind);
|
|
175
|
+
if (selector.startsWith("=")) {
|
|
176
|
+
if (!/^=\d+$/.test(selector))
|
|
177
|
+
return fail(`Invalid selector "${selector}"`);
|
|
178
|
+
const value = Number(selector.slice(1));
|
|
179
|
+
if (exact.has(value))
|
|
180
|
+
return fail(`Duplicate selector "${selector}"`);
|
|
181
|
+
exact.set(value, body);
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
if (!PLURAL_CATEGORIES.has(selector))
|
|
185
|
+
return fail(`Invalid selector "${selector}"`);
|
|
186
|
+
if (categories.has(selector))
|
|
187
|
+
return fail(`Duplicate selector "${selector}"`);
|
|
188
|
+
categories.set(selector, body);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if (!categories.has("other"))
|
|
192
|
+
return fail(`A ${kind} needs an "other" branch`);
|
|
193
|
+
return { type: "plural", name, ordinal, exact, categories };
|
|
194
|
+
}
|
|
195
|
+
function parseSelectBranches(name, inPlural) {
|
|
196
|
+
const branches = new Map();
|
|
197
|
+
for (;;) {
|
|
198
|
+
skipSpace();
|
|
199
|
+
if (index >= template.length)
|
|
200
|
+
return fail("Unclosed select argument");
|
|
201
|
+
if (template[index] === "}") {
|
|
202
|
+
index += 1;
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
const selector = readToken(/[\s{}]/);
|
|
206
|
+
skipSpace();
|
|
207
|
+
if (!selector || template[index] !== "{")
|
|
208
|
+
return fail("Expected a selector and {");
|
|
209
|
+
// A # in a select branch inside a plural still means the outer plural's number.
|
|
210
|
+
const body = parseBranchBody(inPlural, "select");
|
|
211
|
+
if (branches.has(selector))
|
|
212
|
+
return fail(`Duplicate selector "${selector}"`);
|
|
213
|
+
branches.set(selector, body);
|
|
214
|
+
}
|
|
215
|
+
if (!branches.has("other"))
|
|
216
|
+
return fail('A select needs an "other" branch');
|
|
217
|
+
return { type: "select", name, branches };
|
|
218
|
+
}
|
|
219
|
+
const nodes = parseNodes(false, false);
|
|
220
|
+
if (index < template.length)
|
|
221
|
+
fail("Unexpected text");
|
|
222
|
+
return nodes;
|
|
223
|
+
}
|
|
224
|
+
/** Argument names and rich tag names a parsed template uses, each sorted and unique. */
|
|
225
|
+
export function describeNodes(nodes) {
|
|
226
|
+
const names = new Set();
|
|
227
|
+
const tags = new Set();
|
|
228
|
+
const visit = (list) => {
|
|
229
|
+
for (const node of list) {
|
|
230
|
+
if (node.type === "arg")
|
|
231
|
+
names.add(node.name);
|
|
232
|
+
else if (node.type === "plural") {
|
|
233
|
+
names.add(node.name);
|
|
234
|
+
for (const branch of node.exact.values())
|
|
235
|
+
visit(branch);
|
|
236
|
+
for (const branch of node.categories.values())
|
|
237
|
+
visit(branch);
|
|
238
|
+
}
|
|
239
|
+
else if (node.type === "select") {
|
|
240
|
+
names.add(node.name);
|
|
241
|
+
for (const branch of node.branches.values())
|
|
242
|
+
visit(branch);
|
|
243
|
+
}
|
|
244
|
+
else if (node.type === "tag") {
|
|
245
|
+
tags.add(node.name);
|
|
246
|
+
visit(node.children);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
visit(nodes);
|
|
251
|
+
return { names: [...names].sort(), tags: [...tags].sort() };
|
|
252
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import type { Child, MaybeSignal, ReadableSignal, WritableSignal } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Internationalization for MeowJSX. DOM-free: it uses only ECMAScript and `Intl`.
|
|
4
|
+
* The source sentence is the message identity; a message is a `ReadableSignal`
|
|
5
|
+
* that follows the locale, the catalogs and any Signal parameter.
|
|
6
|
+
*/
|
|
7
|
+
/** A translation, or translations of one source keyed by context (`""` = no context). */
|
|
8
|
+
export type CatalogEntry = string | {
|
|
9
|
+
readonly [context: string]: string;
|
|
10
|
+
};
|
|
11
|
+
/** Flat map from source text to its translation in one locale. */
|
|
12
|
+
export type Catalog = {
|
|
13
|
+
readonly [source: string]: CatalogEntry;
|
|
14
|
+
};
|
|
15
|
+
/** A message value; reading `.value` yields the current string. */
|
|
16
|
+
export type Message = ReadableSignal<string>;
|
|
17
|
+
export type MessageParam = MaybeSignal<string | number>;
|
|
18
|
+
export type MessageParams = {
|
|
19
|
+
readonly [name: string]: MessageParam;
|
|
20
|
+
};
|
|
21
|
+
export interface TranslateOptions {
|
|
22
|
+
/** Part of the message identity; separates homographs. */
|
|
23
|
+
readonly context?: string;
|
|
24
|
+
/** A note for translators. Never part of the identity and ignored at run time. */
|
|
25
|
+
readonly note?: string;
|
|
26
|
+
}
|
|
27
|
+
export interface MissingMessage {
|
|
28
|
+
readonly source: string;
|
|
29
|
+
readonly context?: string;
|
|
30
|
+
readonly locale: string;
|
|
31
|
+
}
|
|
32
|
+
export interface CreateI18nOptions {
|
|
33
|
+
/** Language the source sentences are written in. */
|
|
34
|
+
readonly source: string;
|
|
35
|
+
/** Initial locale, or a Signal to adopt as `locale`. Defaults to `source`. */
|
|
36
|
+
readonly locale?: string | WritableSignal<string>;
|
|
37
|
+
readonly catalogs?: {
|
|
38
|
+
readonly [locale: string]: Catalog;
|
|
39
|
+
};
|
|
40
|
+
/** Loads the catalog of a locale that `setLocale` has not seen yet. */
|
|
41
|
+
readonly load?: (locale: string) => Promise<Catalog>;
|
|
42
|
+
/** Default time zone of `date()`. */
|
|
43
|
+
readonly timeZone?: string;
|
|
44
|
+
/** Called once per (source, context, locale) that has no translation. */
|
|
45
|
+
readonly onMissing?: (miss: MissingMessage) => void;
|
|
46
|
+
}
|
|
47
|
+
declare const looseParams: unique symbol;
|
|
48
|
+
type Loose = typeof looseParams;
|
|
49
|
+
/**
|
|
50
|
+
* Parameter names of a template. Only a template without `,` inside braces, without
|
|
51
|
+
* a nested brace, without a space inside a name and without an apostrophe is
|
|
52
|
+
* analyzed; any other template is `Loose`.
|
|
53
|
+
*/
|
|
54
|
+
type ParamNames<S extends string, Found extends string = never> = S extends `${string}'${string}` ? Loose : S extends `${string}{${infer Inner}}${infer Rest}` ? Inner extends `${string}${"," | "{" | " "}${string}` ? Loose : ParamNames<Rest, Found | Inner> : Found;
|
|
55
|
+
type TranslateArguments<S extends string> = [ParamNames<S>] extends [never] ? [params?: {
|
|
56
|
+
readonly [name: string]: never;
|
|
57
|
+
} | undefined, options?: TranslateOptions] : [ParamNames<S>] extends [Loose] ? [params?: MessageParams, options?: TranslateOptions] : [params: {
|
|
58
|
+
readonly [Name in ParamNames<S>]: MessageParam;
|
|
59
|
+
}, options?: TranslateOptions];
|
|
60
|
+
/** A literal source, or `never` for a value of type `string`. */
|
|
61
|
+
type Literal<S extends string> = {} extends Record<S, 1> ? never : S;
|
|
62
|
+
/**
|
|
63
|
+
* Tag names of a rich template: `<link>` and `<b>` give `"link" | "b"`. Closing tags, and
|
|
64
|
+
* angle brackets that cannot be a tag (a space inside), are not names.
|
|
65
|
+
*/
|
|
66
|
+
type TagNames<S extends string, Found extends string = never> = S extends `${string}<${infer Name}>${infer Rest}` ? Name extends `/${string}` | `${string}${" " | "<" | ">"}${string}` | "" ? TagNames<Rest, Found> : TagNames<Rest, Found | Name> : Found;
|
|
67
|
+
/** Wraps the formatted content of one tag; the result replaces `<tag>...</tag>`. */
|
|
68
|
+
export type RichPart = (children: Child) => Child;
|
|
69
|
+
export type RichParts<S extends string = string> = {
|
|
70
|
+
readonly [Name in TagNames<S>]: RichPart;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* A message declared away from its use. It carries its own context and note; `t` accepts it,
|
|
74
|
+
* or a Signal of it, where it accepts a literal.
|
|
75
|
+
*/
|
|
76
|
+
export interface MessageDescriptor<S extends string = string> {
|
|
77
|
+
readonly source: S;
|
|
78
|
+
readonly context?: string;
|
|
79
|
+
readonly note?: string;
|
|
80
|
+
}
|
|
81
|
+
/** One entry of a `defineMessages` table. */
|
|
82
|
+
export type MessageSpec = string | MessageDescriptor;
|
|
83
|
+
type SpecSource<V> = V extends string ? V : V extends {
|
|
84
|
+
readonly source: infer S;
|
|
85
|
+
} ? S : never;
|
|
86
|
+
/** A spec whose source has type `string` becomes `never`, so the table must use literals. */
|
|
87
|
+
type CheckedSpec<V> = [SpecSource<V>] extends [never] ? never : SpecSource<V> extends infer S extends string ? {} extends Record<S, 1> ? never : V : never;
|
|
88
|
+
export type MessageTable<T> = {
|
|
89
|
+
readonly [Key in keyof T]: MessageDescriptor<Extract<SpecSource<T[Key]>, string>>;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* `t(source, params?, options?)`. The source must be written as a literal: a value of
|
|
93
|
+
* type `string`, or a template literal with a runtime part, is refused. Named parameters are
|
|
94
|
+
* required when the literal is simple. A descriptor from `defineMessages`, or a Signal of one,
|
|
95
|
+
* is accepted in place of the literal; its parameters are not inferred.
|
|
96
|
+
*/
|
|
97
|
+
export interface Translate {
|
|
98
|
+
<S extends string>(source: Literal<S>, ...rest: TranslateArguments<S>): Message;
|
|
99
|
+
(message: MessageDescriptor | ReadableSignal<MessageDescriptor>, params?: MessageParams, options?: TranslateOptions): Message;
|
|
100
|
+
/**
|
|
101
|
+
* A message with named tags: `<link>the guide</link>`. `parts` maps each tag to a function
|
|
102
|
+
* that wraps its content. Tags carry no attributes, so a translation can move or reorder a
|
|
103
|
+
* tag but cannot introduce markup.
|
|
104
|
+
*/
|
|
105
|
+
readonly rich: TranslateRich;
|
|
106
|
+
}
|
|
107
|
+
export interface TranslateRich {
|
|
108
|
+
<S extends string>(source: Literal<S>, parts: RichParts<S>, ...rest: TranslateArguments<S>): ReadableSignal<Child>;
|
|
109
|
+
}
|
|
110
|
+
export interface I18n {
|
|
111
|
+
readonly locale: WritableSignal<string>;
|
|
112
|
+
readonly direction: ReadableSignal<"ltr" | "rtl">;
|
|
113
|
+
readonly t: Translate;
|
|
114
|
+
/**
|
|
115
|
+
* Translates text the scanner cannot see, such as site configuration copy. The text and its
|
|
116
|
+
* translation are literal: no placeholder, plural or apostrophe is interpreted. Its owner
|
|
117
|
+
* hands the same set to `auditCatalog` as extra literal messages.
|
|
118
|
+
*/
|
|
119
|
+
lookup(text: string, options?: TranslateOptions): Message;
|
|
120
|
+
number(value: MaybeSignal<number>, options?: Intl.NumberFormatOptions): ReadableSignal<string>;
|
|
121
|
+
date(value: MaybeSignal<Date | number>, options?: Intl.DateTimeFormatOptions): ReadableSignal<string>;
|
|
122
|
+
addCatalog(locale: string, catalog: Catalog): void;
|
|
123
|
+
/**
|
|
124
|
+
* Loads the catalog first when `load` is given and the locale has none, then writes
|
|
125
|
+
* `locale`. With concurrent calls only the last one writes.
|
|
126
|
+
*/
|
|
127
|
+
setLocale(locale: string): Promise<void>;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Declares messages at module level, away from their use. It performs no lookup: each entry
|
|
131
|
+
* becomes a frozen descriptor that `t` translates where it is used.
|
|
132
|
+
*/
|
|
133
|
+
export declare function defineMessages<const T extends {
|
|
134
|
+
readonly [name: string]: MessageSpec;
|
|
135
|
+
}>(table: {
|
|
136
|
+
readonly [Key in keyof T]: CheckedSpec<T[Key]>;
|
|
137
|
+
}): MessageTable<T>;
|
|
138
|
+
export declare function createI18n(options: CreateI18nOptions): I18n;
|
|
139
|
+
/** Provides one instance to the components below it. */
|
|
140
|
+
export declare const I18nProvider: import("./types.js").Component<{
|
|
141
|
+
value: I18n | undefined;
|
|
142
|
+
children?: Child;
|
|
143
|
+
}>;
|
|
144
|
+
/** The nearest provided instance, or `undefined`. Read it in a component body. */
|
|
145
|
+
export declare function useI18n(): I18n | undefined;
|
|
146
|
+
/**
|
|
147
|
+
* Translates with the nearest `I18nProvider`, or renders the source text when there is none.
|
|
148
|
+
* Callable only while a component function body runs; elsewhere it throws.
|
|
149
|
+
*/
|
|
150
|
+
export declare const t: Translate;
|
|
151
|
+
export {};
|