@rivinia/i18n 0.4.0 → 0.6.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 +131 -0
- package/README.md +96 -13
- package/cli/rivinia-i18n-keys.mjs +267 -32
- package/fesm2022/rivinia-i18n-http-loader.impl-sLjTazQZ.mjs +20 -0
- package/fesm2022/rivinia-i18n-http-loader.impl-sLjTazQZ.mjs.map +1 -0
- package/fesm2022/rivinia-i18n-remote-loader.impl-DE90KQn_.mjs +19 -0
- package/fesm2022/rivinia-i18n-remote-loader.impl-DE90KQn_.mjs.map +1 -0
- package/fesm2022/rivinia-i18n.mjs +822 -432
- package/fesm2022/rivinia-i18n.mjs.map +1 -1
- package/package.json +22 -1
- package/types/rivinia-i18n.d.ts +128 -84
- package/fesm2022/rivinia-i18n-http-loader.impl--BRii7w1.mjs +0 -17
- package/fesm2022/rivinia-i18n-http-loader.impl--BRii7w1.mjs.map +0 -1
- package/fesm2022/rivinia-i18n-json-loader.impl-Bq-W-UW9.mjs +0 -18
- package/fesm2022/rivinia-i18n-json-loader.impl-Bq-W-UW9.mjs.map +0 -1
- package/fesm2022/rivinia-i18n-loader-url-B9SlR1XB.mjs +0 -11
- package/fesm2022/rivinia-i18n-loader-url-B9SlR1XB.mjs.map +0 -1
- package/fesm2022/rivinia-i18n-remote-loader.impl-CqKS6JWN.mjs +0 -16
- package/fesm2022/rivinia-i18n-remote-loader.impl-CqKS6JWN.mjs.map +0 -1
|
@@ -1,21 +1,56 @@
|
|
|
1
1
|
import * as i0 from "@angular/core";
|
|
2
|
-
import { DOCUMENT, InjectionToken, PendingTasks, Pipe, REQUEST, Service, TransferState, computed, effect, inject, isDevMode, makeEnvironmentProviders, makeStateKey, provideAppInitializer, signal } from "@angular/core";
|
|
2
|
+
import { DOCUMENT, InjectionToken, PendingTasks, Pipe, REQUEST, Service, TransferState, computed, effect, inject, isDevMode, makeEnvironmentProviders, makeStateKey, provideAppInitializer, signal, untracked } from "@angular/core";
|
|
3
|
+
var RiviniaLoadError = class extends Error {
|
|
4
|
+
url;
|
|
5
|
+
status;
|
|
6
|
+
language;
|
|
7
|
+
constructor(url, status, language, options) {
|
|
8
|
+
super(`[rivinia/i18n] Failed to load '${url}' (HTTP ${status})`, options);
|
|
9
|
+
this.name = "RiviniaLoadError";
|
|
10
|
+
this.url = url;
|
|
11
|
+
this.status = status;
|
|
12
|
+
this.language = language;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
function resolveLoaderUrl(url, context) {
|
|
16
|
+
if (!context?.isServer) return url;
|
|
17
|
+
try {
|
|
18
|
+
return new URL(url, context.baseUrl).href;
|
|
19
|
+
} catch {
|
|
20
|
+
return url;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const DEFAULT_BASE_PATH = "i18n";
|
|
24
|
+
var JsonLoader = class {
|
|
25
|
+
_options;
|
|
26
|
+
constructor(_options) {
|
|
27
|
+
this._options = _options;
|
|
28
|
+
}
|
|
29
|
+
async load(language, context) {
|
|
30
|
+
const url = resolveLoaderUrl(`${this._options?.path ?? DEFAULT_BASE_PATH}/${language}.json`, context);
|
|
31
|
+
const response = await fetch(url);
|
|
32
|
+
if (!response.ok) throw new RiviniaLoadError(url, response.status, language);
|
|
33
|
+
return response.json();
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
function loadError(url, status, language) {
|
|
37
|
+
return new RiviniaLoadError(url, status, language);
|
|
38
|
+
}
|
|
3
39
|
function jsonLoader(options) {
|
|
4
40
|
return { async resolve() {
|
|
5
|
-
const { JsonLoader } = await import("./rivinia-i18n-json-loader.impl-Bq-W-UW9.mjs");
|
|
6
41
|
return new JsonLoader(options);
|
|
7
42
|
} };
|
|
8
43
|
}
|
|
9
44
|
function httpLoader(options) {
|
|
10
45
|
return { async resolve() {
|
|
11
|
-
const { HttpLoader } = await import("./rivinia-i18n-http-loader.impl
|
|
12
|
-
return new HttpLoader(options);
|
|
46
|
+
const { HttpLoader } = await import("./rivinia-i18n-http-loader.impl-sLjTazQZ.mjs");
|
|
47
|
+
return new HttpLoader(options, resolveLoaderUrl, loadError);
|
|
13
48
|
} };
|
|
14
49
|
}
|
|
15
50
|
function remoteLoader(options) {
|
|
16
51
|
return { async resolve() {
|
|
17
|
-
const { RemoteLoader } = await import("./rivinia-i18n-remote-loader.impl-
|
|
18
|
-
return new RemoteLoader(options);
|
|
52
|
+
const { RemoteLoader } = await import("./rivinia-i18n-remote-loader.impl-DE90KQn_.mjs");
|
|
53
|
+
return new RemoteLoader(options, resolveLoaderUrl, loadError);
|
|
19
54
|
} };
|
|
20
55
|
}
|
|
21
56
|
function customLoader(load) {
|
|
@@ -100,88 +135,135 @@ function shouldSecureCookie(location) {
|
|
|
100
135
|
return false;
|
|
101
136
|
}
|
|
102
137
|
}
|
|
103
|
-
function formatIcu(template, params, locale) {
|
|
104
|
-
return
|
|
138
|
+
function formatIcu(template, params, locale, options) {
|
|
139
|
+
return renderMessage(compile(template), params, locale, options);
|
|
105
140
|
}
|
|
106
|
-
|
|
107
|
-
|
|
141
|
+
const COMPILE_LIMIT = 500;
|
|
142
|
+
const compiled = /* @__PURE__ */ new Map();
|
|
143
|
+
function compile(template) {
|
|
144
|
+
const hit = compiled.get(template);
|
|
145
|
+
if (hit !== void 0) return hit;
|
|
146
|
+
const message = { nodes: parseNodes(template, 0, template.length, false).nodes };
|
|
147
|
+
if (compiled.size >= COMPILE_LIMIT) {
|
|
148
|
+
const oldest = compiled.keys().next();
|
|
149
|
+
if (!oldest.done) compiled.delete(oldest.value);
|
|
150
|
+
}
|
|
151
|
+
compiled.set(template, message);
|
|
152
|
+
return message;
|
|
153
|
+
}
|
|
154
|
+
function parseNodes(src, start, end, inPlural) {
|
|
155
|
+
const nodes = [];
|
|
156
|
+
let buffer = "";
|
|
157
|
+
const flush = () => {
|
|
158
|
+
if (buffer !== "") {
|
|
159
|
+
nodes.push({
|
|
160
|
+
type: "text",
|
|
161
|
+
value: buffer
|
|
162
|
+
});
|
|
163
|
+
buffer = "";
|
|
164
|
+
}
|
|
165
|
+
};
|
|
108
166
|
let i = start;
|
|
109
167
|
while (i < end) {
|
|
110
|
-
const ch = src
|
|
111
|
-
if (ch ===
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
168
|
+
const ch = src.charCodeAt(i);
|
|
169
|
+
if (ch === 123) {
|
|
170
|
+
flush();
|
|
171
|
+
const arg = parseArgument(src, i, end, inPlural);
|
|
172
|
+
if (arg.rest) {
|
|
173
|
+
nodes.push({
|
|
174
|
+
type: "text",
|
|
175
|
+
value: src.slice(i, end)
|
|
176
|
+
});
|
|
177
|
+
return {
|
|
178
|
+
nodes,
|
|
179
|
+
next: end,
|
|
180
|
+
closed: false
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
nodes.push(arg.node);
|
|
119
184
|
i = arg.next;
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
if (ch === 125) {
|
|
188
|
+
flush();
|
|
189
|
+
return {
|
|
190
|
+
nodes,
|
|
191
|
+
next: i,
|
|
192
|
+
closed: true
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
if (ch === 35 && inPlural) {
|
|
196
|
+
flush();
|
|
197
|
+
nodes.push({ type: "hash" });
|
|
127
198
|
i++;
|
|
128
|
-
|
|
129
|
-
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
if (ch === 39) {
|
|
202
|
+
const after = i + 1 < end ? src.charCodeAt(i + 1) : -1;
|
|
203
|
+
if (after === 39) {
|
|
204
|
+
buffer += "'";
|
|
205
|
+
i += 2;
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (after === 123 || after === 125 || inPlural && after === 35) {
|
|
209
|
+
let j = i + 1;
|
|
210
|
+
while (j < end && src.charCodeAt(j) !== 39) j++;
|
|
211
|
+
buffer += src.slice(i + 1, j);
|
|
212
|
+
i = j < end ? j + 1 : end;
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
buffer += "'";
|
|
130
216
|
i++;
|
|
217
|
+
continue;
|
|
131
218
|
}
|
|
219
|
+
buffer += src[i];
|
|
220
|
+
i++;
|
|
132
221
|
}
|
|
222
|
+
flush();
|
|
133
223
|
return {
|
|
134
|
-
|
|
224
|
+
nodes,
|
|
135
225
|
next: i,
|
|
136
|
-
|
|
226
|
+
closed: false
|
|
137
227
|
};
|
|
138
228
|
}
|
|
139
|
-
function parseArgument(src, start, end,
|
|
229
|
+
function parseArgument(src, start, end, inPlural) {
|
|
230
|
+
const rest = () => ({
|
|
231
|
+
rest: true,
|
|
232
|
+
next: end
|
|
233
|
+
});
|
|
140
234
|
let i = skipSpaces(src, start + 1, end);
|
|
141
235
|
const name = readWord(src, i, end);
|
|
142
236
|
i = skipSpaces(src, name.next, end);
|
|
143
|
-
if (i >= end) return
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
text: value === void 0 || value === null || name.word === "" ? src.slice(start, i + 1) : String(value),
|
|
152
|
-
next: i + 1,
|
|
153
|
-
ok: true
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
if (src[i] !== "," || name.word === "") return {
|
|
157
|
-
text: src.slice(start, end),
|
|
158
|
-
next: end,
|
|
159
|
-
ok: true
|
|
237
|
+
if (i >= end) return rest();
|
|
238
|
+
if (src.charCodeAt(i) === 125) return {
|
|
239
|
+
node: {
|
|
240
|
+
type: "param",
|
|
241
|
+
name: name.word,
|
|
242
|
+
raw: src.slice(start, i + 1)
|
|
243
|
+
},
|
|
244
|
+
next: i + 1
|
|
160
245
|
};
|
|
246
|
+
if (src.charCodeAt(i) !== 44 || name.word === "") return rest();
|
|
161
247
|
i = skipSpaces(src, i + 1, end);
|
|
162
248
|
const type = readWord(src, i, end);
|
|
163
249
|
i = skipSpaces(src, type.next, end);
|
|
164
|
-
if (type.word === "number" || type.word === "date") return
|
|
165
|
-
if (i >= end || src
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
ok: true
|
|
169
|
-
};
|
|
170
|
-
if (type.word !== "plural" && type.word !== "selectordinal" && type.word !== "select") return rawSegment(src, start, end);
|
|
250
|
+
if (type.word === "number" || type.word === "date") return parseTyped(src, start, i, end, type.word, name.word);
|
|
251
|
+
if (i >= end || src.charCodeAt(i) !== 44) return rest();
|
|
252
|
+
if (type.word !== "plural" && type.word !== "selectordinal" && type.word !== "select") return rawNode(src, start, end);
|
|
253
|
+
const branchPlural = type.word !== "select";
|
|
171
254
|
i++;
|
|
172
|
-
const numeric = type.word === "select" ? null : toFiniteNumber(params[name.word]);
|
|
173
|
-
const ordinal = type.word === "selectordinal";
|
|
174
255
|
const branches = /* @__PURE__ */ new Map();
|
|
256
|
+
const exacts = /* @__PURE__ */ new Map();
|
|
175
257
|
let offset = 0;
|
|
176
258
|
i = skipSpaces(src, i, end);
|
|
177
259
|
let closed = false;
|
|
178
260
|
while (i < end) {
|
|
179
261
|
i = skipSpaces(src, i, end);
|
|
180
|
-
if (i < end && src
|
|
262
|
+
if (i < end && src.charCodeAt(i) === 125) {
|
|
181
263
|
closed = true;
|
|
182
264
|
break;
|
|
183
265
|
}
|
|
184
|
-
if (
|
|
266
|
+
if (branchPlural) {
|
|
185
267
|
const offsetClause = readOffset(src, i, end);
|
|
186
268
|
if (offsetClause) {
|
|
187
269
|
offset = offsetClause.value;
|
|
@@ -189,103 +271,115 @@ function parseArgument(src, start, end, params, locale, outerPlural) {
|
|
|
189
271
|
continue;
|
|
190
272
|
}
|
|
191
273
|
}
|
|
192
|
-
const
|
|
193
|
-
if (
|
|
194
|
-
i = skipSpaces(src,
|
|
195
|
-
if (i >= end || src
|
|
196
|
-
const
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
274
|
+
const selector = readSelector(src, i, end);
|
|
275
|
+
if (selector.selector === null) return rawNode(src, start, end);
|
|
276
|
+
i = skipSpaces(src, selector.next, end);
|
|
277
|
+
if (i >= end || src.charCodeAt(i) !== 123) return rawNode(src, start, end);
|
|
278
|
+
const body = parseNodes(src, i + 1, end, branchPlural || inPlural);
|
|
279
|
+
branches.set(selector.selector, body.nodes);
|
|
280
|
+
if (selector.selector.charCodeAt(0) === 61) {
|
|
281
|
+
const exact = Number(selector.selector.slice(1));
|
|
282
|
+
if (Number.isFinite(exact)) exacts.set(exact, body.nodes);
|
|
283
|
+
}
|
|
202
284
|
i = body.next + 1;
|
|
203
285
|
}
|
|
204
|
-
if (!closed) return
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
286
|
+
if (!closed) return rest();
|
|
287
|
+
if (type.word === "select") return {
|
|
288
|
+
node: {
|
|
289
|
+
type: "select",
|
|
290
|
+
name: name.word,
|
|
291
|
+
branches
|
|
292
|
+
},
|
|
293
|
+
next: i + 1
|
|
294
|
+
};
|
|
295
|
+
return {
|
|
296
|
+
node: {
|
|
297
|
+
type: "plural",
|
|
298
|
+
name: name.word,
|
|
299
|
+
ordinal: type.word === "selectordinal",
|
|
300
|
+
offset,
|
|
301
|
+
branches,
|
|
302
|
+
exacts
|
|
303
|
+
},
|
|
304
|
+
next: i + 1
|
|
208
305
|
};
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
306
|
+
}
|
|
307
|
+
function parseTyped(src, start, i, end, kind, name) {
|
|
308
|
+
let style;
|
|
309
|
+
let raw;
|
|
310
|
+
if (i < end && src.charCodeAt(i) === 125) {
|
|
311
|
+
style = void 0;
|
|
312
|
+
raw = src.slice(start, i + 1);
|
|
212
313
|
return {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
314
|
+
node: {
|
|
315
|
+
type: kind,
|
|
316
|
+
name,
|
|
317
|
+
style,
|
|
318
|
+
raw
|
|
319
|
+
},
|
|
320
|
+
next: i + 1
|
|
216
321
|
};
|
|
217
322
|
}
|
|
218
|
-
if (
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
if (exact !== void 0) return {
|
|
225
|
-
text: exact,
|
|
226
|
-
next: i + 1,
|
|
227
|
-
ok: true
|
|
228
|
-
};
|
|
229
|
-
let category;
|
|
230
|
-
try {
|
|
231
|
-
category = pluralRules(locale, ordinal).select(numeric - offset);
|
|
232
|
-
} catch {
|
|
233
|
-
category = "other";
|
|
234
|
-
}
|
|
323
|
+
if (i >= end || src.charCodeAt(i) !== 44) return rawNode(src, start, end);
|
|
324
|
+
const word = readWord(src, i + 1, end);
|
|
325
|
+
const after = skipSpaces(src, word.next, end);
|
|
326
|
+
if (after >= end || src.charCodeAt(after) !== 125) return rawNode(src, start, end);
|
|
327
|
+
style = word.word;
|
|
328
|
+
raw = src.slice(start, after + 1);
|
|
235
329
|
return {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
330
|
+
node: {
|
|
331
|
+
type: kind,
|
|
332
|
+
name,
|
|
333
|
+
style,
|
|
334
|
+
raw
|
|
335
|
+
},
|
|
336
|
+
next: after + 1
|
|
239
337
|
};
|
|
240
338
|
}
|
|
241
|
-
function
|
|
242
|
-
|
|
243
|
-
if (i < end && src[i] === "}") style = void 0;
|
|
244
|
-
else if (i < end && src[i] === ",") {
|
|
245
|
-
const word = readWord(src, i + 1, end);
|
|
246
|
-
const after = skipSpaces(src, word.next, end);
|
|
247
|
-
if (after >= end || src[after] !== "}") return null;
|
|
248
|
-
style = word.word;
|
|
249
|
-
i = after;
|
|
250
|
-
} else return null;
|
|
251
|
-
const text = kind === "number" ? formatNumberValue(value, locale, style) : formatDateValue(value, locale, style);
|
|
252
|
-
if (text === null) return null;
|
|
339
|
+
function rawNode(src, start, end) {
|
|
340
|
+
const segment = rawSegment(src, start, end);
|
|
253
341
|
return {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
342
|
+
node: {
|
|
343
|
+
type: "raw",
|
|
344
|
+
value: segment.text
|
|
345
|
+
},
|
|
346
|
+
next: segment.next
|
|
257
347
|
};
|
|
258
348
|
}
|
|
259
349
|
function readOffset(src, i, end) {
|
|
260
350
|
const start = skipSpaces(src, i, end);
|
|
261
|
-
if (
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
}
|
|
273
|
-
function toFiniteNumber(value) {
|
|
274
|
-
if (typeof value === "number") return Number.isFinite(value) ? value : null;
|
|
275
|
-
if (typeof value === "string" && value.trim() !== "") {
|
|
276
|
-
const n = Number(value);
|
|
277
|
-
return Number.isFinite(n) ? n : null;
|
|
351
|
+
if (src.startsWith("offset:", start)) {
|
|
352
|
+
let j = start + 7;
|
|
353
|
+
const digits = j;
|
|
354
|
+
while (j < end && isDigit(src.charCodeAt(j))) j++;
|
|
355
|
+
if (j === digits) return null;
|
|
356
|
+
const value = Number(src.slice(digits, j));
|
|
357
|
+
if (!Number.isFinite(value)) return null;
|
|
358
|
+
return {
|
|
359
|
+
value,
|
|
360
|
+
next: j
|
|
361
|
+
};
|
|
278
362
|
}
|
|
279
363
|
return null;
|
|
280
364
|
}
|
|
365
|
+
function isSpace(ch) {
|
|
366
|
+
return ch === 32 || ch >= 9 && ch <= 13 || ch === 160;
|
|
367
|
+
}
|
|
368
|
+
function isDigit(ch) {
|
|
369
|
+
return ch >= 48 && ch <= 57;
|
|
370
|
+
}
|
|
281
371
|
function skipSpaces(src, i, end) {
|
|
282
|
-
while (i < end &&
|
|
372
|
+
while (i < end && isSpace(src.charCodeAt(i))) i++;
|
|
283
373
|
return i;
|
|
284
374
|
}
|
|
285
375
|
function readWord(src, i, end) {
|
|
286
376
|
const start = skipSpaces(src, i, end);
|
|
287
377
|
let j = start;
|
|
288
|
-
while (j < end
|
|
378
|
+
while (j < end) {
|
|
379
|
+
const ch = src.charCodeAt(j);
|
|
380
|
+
if (isSpace(ch) || ch === 44 || ch === 123 || ch === 125) break;
|
|
381
|
+
j++;
|
|
382
|
+
}
|
|
289
383
|
return {
|
|
290
384
|
word: src.slice(start, j),
|
|
291
385
|
next: j
|
|
@@ -293,14 +387,14 @@ function readWord(src, i, end) {
|
|
|
293
387
|
}
|
|
294
388
|
function readSelector(src, i, end) {
|
|
295
389
|
i = skipSpaces(src, i, end);
|
|
296
|
-
if (i < end && src
|
|
390
|
+
if (i < end && src.charCodeAt(i) === 61) {
|
|
297
391
|
let j = i + 1;
|
|
298
|
-
if (j < end && src
|
|
392
|
+
if (j < end && src.charCodeAt(j) === 45) j++;
|
|
299
393
|
const digits = j;
|
|
300
|
-
while (j < end &&
|
|
301
|
-
if (j < end && src
|
|
394
|
+
while (j < end && isDigit(src.charCodeAt(j))) j++;
|
|
395
|
+
if (j < end && src.charCodeAt(j) === 46 && j + 1 < end && isDigit(src.charCodeAt(j + 1))) {
|
|
302
396
|
j++;
|
|
303
|
-
while (j < end &&
|
|
397
|
+
while (j < end && isDigit(src.charCodeAt(j))) j++;
|
|
304
398
|
}
|
|
305
399
|
if (j === digits) return {
|
|
306
400
|
selector: null,
|
|
@@ -325,28 +419,126 @@ function rawSegment(src, start, end) {
|
|
|
325
419
|
let depth = 0;
|
|
326
420
|
let i = start;
|
|
327
421
|
while (i < end) {
|
|
328
|
-
const ch = src
|
|
329
|
-
if (ch ===
|
|
330
|
-
else if (ch ===
|
|
422
|
+
const ch = src.charCodeAt(i);
|
|
423
|
+
if (ch === 123) depth++;
|
|
424
|
+
else if (ch === 125) {
|
|
331
425
|
depth--;
|
|
332
426
|
if (depth === 0) return {
|
|
333
427
|
text: src.slice(start, i + 1),
|
|
334
|
-
next: i + 1
|
|
335
|
-
ok: true
|
|
428
|
+
next: i + 1
|
|
336
429
|
};
|
|
337
430
|
}
|
|
338
431
|
i++;
|
|
339
432
|
}
|
|
340
433
|
return {
|
|
341
434
|
text: src.slice(start, end),
|
|
342
|
-
next: end
|
|
343
|
-
ok: true
|
|
435
|
+
next: end
|
|
344
436
|
};
|
|
345
437
|
}
|
|
438
|
+
function renderMessage(message, params, locale, options) {
|
|
439
|
+
return renderNodes(message.nodes, {
|
|
440
|
+
params,
|
|
441
|
+
locale,
|
|
442
|
+
plural: null,
|
|
443
|
+
options
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
function renderNodes(nodes, ctx) {
|
|
447
|
+
let out = "";
|
|
448
|
+
for (const node of nodes) switch (node.type) {
|
|
449
|
+
case "text":
|
|
450
|
+
out += node.value;
|
|
451
|
+
break;
|
|
452
|
+
case "raw":
|
|
453
|
+
out += node.value;
|
|
454
|
+
break;
|
|
455
|
+
case "hash":
|
|
456
|
+
out += ctx.plural === null ? "#" : formatNumberValue(ctx.plural.value - ctx.plural.offset, ctx.locale, void 0) ?? "#";
|
|
457
|
+
break;
|
|
458
|
+
case "param": {
|
|
459
|
+
const value = ctx.params[node.name];
|
|
460
|
+
if (value === void 0 || value === null || node.name === "") {
|
|
461
|
+
if (node.name !== "") ctx.options?.onMissingParam?.(node.name);
|
|
462
|
+
out += node.raw;
|
|
463
|
+
} else out += String(value);
|
|
464
|
+
break;
|
|
465
|
+
}
|
|
466
|
+
case "number": {
|
|
467
|
+
const value = ctx.params[node.name];
|
|
468
|
+
reportMissing(ctx, node.name, value);
|
|
469
|
+
out += formatNumberValue(value, ctx.locale, node.style) ?? node.raw;
|
|
470
|
+
break;
|
|
471
|
+
}
|
|
472
|
+
case "date": {
|
|
473
|
+
const value = ctx.params[node.name];
|
|
474
|
+
reportMissing(ctx, node.name, value);
|
|
475
|
+
out += formatDateValue(value, ctx.locale, node.style) ?? node.raw;
|
|
476
|
+
break;
|
|
477
|
+
}
|
|
478
|
+
case "plural": {
|
|
479
|
+
const value = ctx.params[node.name];
|
|
480
|
+
const numeric = toFiniteNumber(value);
|
|
481
|
+
reportMissing(ctx, node.name, value);
|
|
482
|
+
let branch;
|
|
483
|
+
if (numeric !== null) {
|
|
484
|
+
branch = node.exacts.get(numeric);
|
|
485
|
+
if (branch === void 0) branch = node.branches.get(selectCategory(numeric - node.offset, node.ordinal, ctx.locale));
|
|
486
|
+
if (branch === void 0) branch = node.branches.get("other");
|
|
487
|
+
} else branch = node.branches.get("other");
|
|
488
|
+
if (branch !== void 0) {
|
|
489
|
+
const saved = ctx.plural;
|
|
490
|
+
ctx.plural = numeric !== null ? {
|
|
491
|
+
value: numeric,
|
|
492
|
+
offset: node.offset
|
|
493
|
+
} : saved;
|
|
494
|
+
out += renderNodes(branch, ctx);
|
|
495
|
+
ctx.plural = saved;
|
|
496
|
+
}
|
|
497
|
+
break;
|
|
498
|
+
}
|
|
499
|
+
case "select": {
|
|
500
|
+
const value = ctx.params[node.name];
|
|
501
|
+
reportMissing(ctx, node.name, value);
|
|
502
|
+
const key = value === void 0 || value === null ? "" : String(value);
|
|
503
|
+
const branch = node.branches.get(key) ?? node.branches.get("other");
|
|
504
|
+
if (branch) out += renderNodes(branch, ctx);
|
|
505
|
+
break;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return out;
|
|
509
|
+
}
|
|
510
|
+
function reportMissing(ctx, name, value) {
|
|
511
|
+
if (value === void 0 || value === null) ctx.options?.onMissingParam?.(name);
|
|
512
|
+
}
|
|
513
|
+
function selectCategory(value, ordinal, locale) {
|
|
514
|
+
const key = `${ordinal ? 1 : 0}|${locale}|${value}`;
|
|
515
|
+
const hit = pluralCategories.get(key);
|
|
516
|
+
if (hit !== void 0) return hit;
|
|
517
|
+
let category;
|
|
518
|
+
try {
|
|
519
|
+
category = pluralRules(locale, ordinal).select(value);
|
|
520
|
+
} catch {
|
|
521
|
+
category = "other";
|
|
522
|
+
}
|
|
523
|
+
if (pluralCategories.size >= CACHE_LIMIT) pluralCategories.clear();
|
|
524
|
+
pluralCategories.set(key, category);
|
|
525
|
+
return category;
|
|
526
|
+
}
|
|
527
|
+
function toFiniteNumber(value) {
|
|
528
|
+
if (typeof value === "number") return Number.isFinite(value) ? value : null;
|
|
529
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
530
|
+
const n = Number(value);
|
|
531
|
+
return Number.isFinite(n) ? n : null;
|
|
532
|
+
}
|
|
533
|
+
return null;
|
|
534
|
+
}
|
|
346
535
|
const CACHE_LIMIT = 40;
|
|
347
536
|
const numberFormats = /* @__PURE__ */ new Map();
|
|
348
537
|
const dateFormats = /* @__PURE__ */ new Map();
|
|
349
538
|
const pluralRulesByLocale = /* @__PURE__ */ new Map();
|
|
539
|
+
const pluralCategories = /* @__PURE__ */ new Map();
|
|
540
|
+
const formattedNumbers = /* @__PURE__ */ new Map();
|
|
541
|
+
const FORMATTED_NUMBERS_LIMIT = 1e3;
|
|
350
542
|
function memo(map, key, create) {
|
|
351
543
|
const hit = map.get(key);
|
|
352
544
|
if (hit !== void 0) return hit;
|
|
@@ -361,11 +553,25 @@ function pluralRules(locale, ordinal) {
|
|
|
361
553
|
}
|
|
362
554
|
function formatNumberValue(value, locale, style) {
|
|
363
555
|
if (typeof value !== "number" || !Number.isFinite(value)) return null;
|
|
364
|
-
|
|
365
|
-
|
|
556
|
+
let key;
|
|
557
|
+
let options;
|
|
558
|
+
if (style === void 0) {
|
|
559
|
+
key = `${locale}|`;
|
|
560
|
+
options = {};
|
|
561
|
+
} else {
|
|
562
|
+
const resolved = numberOptions(style);
|
|
563
|
+
if (resolved === null) return null;
|
|
564
|
+
key = `${locale}|${style}`;
|
|
565
|
+
options = resolved;
|
|
566
|
+
}
|
|
567
|
+
const cacheKey = `${key}${value}`;
|
|
568
|
+
const cached = formattedNumbers.get(cacheKey);
|
|
569
|
+
if (cached !== void 0) return cached;
|
|
366
570
|
try {
|
|
367
|
-
const
|
|
368
|
-
|
|
571
|
+
const text = memo(numberFormats, key, () => new Intl.NumberFormat(locale, options)).format(value);
|
|
572
|
+
if (formattedNumbers.size >= FORMATTED_NUMBERS_LIMIT) formattedNumbers.clear();
|
|
573
|
+
formattedNumbers.set(cacheKey, text);
|
|
574
|
+
return text;
|
|
369
575
|
} catch {
|
|
370
576
|
return null;
|
|
371
577
|
}
|
|
@@ -389,6 +595,8 @@ const DATE_STYLES = /* @__PURE__ */ new Set([
|
|
|
389
595
|
"long",
|
|
390
596
|
"full"
|
|
391
597
|
]);
|
|
598
|
+
const ISO_DATE = /^\d{4}-\d{2}-\d{2}$/;
|
|
599
|
+
const ISO_DATE_TIME = /^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d{1,9})?)?(?:Z|[+-]\d{2}:?\d{2})?$/;
|
|
392
600
|
function formatDateValue(value, locale, style) {
|
|
393
601
|
const date = toDate(value);
|
|
394
602
|
if (date === null) return null;
|
|
@@ -407,6 +615,18 @@ function toDate(value) {
|
|
|
407
615
|
const date = new Date(value);
|
|
408
616
|
return Number.isNaN(date.getTime()) ? null : date;
|
|
409
617
|
}
|
|
618
|
+
if (typeof value === "string") {
|
|
619
|
+
if (ISO_DATE.test(value)) {
|
|
620
|
+
const [year, month, day] = value.split("-").map(Number);
|
|
621
|
+
const date = new Date(year, month - 1, day);
|
|
622
|
+
return date.getFullYear() === year && date.getMonth() === month - 1 && date.getDate() === day ? date : null;
|
|
623
|
+
}
|
|
624
|
+
if (ISO_DATE_TIME.test(value)) {
|
|
625
|
+
const date = new Date(value);
|
|
626
|
+
return Number.isNaN(date.getTime()) ? null : date;
|
|
627
|
+
}
|
|
628
|
+
return null;
|
|
629
|
+
}
|
|
410
630
|
if (Object.prototype.toString.call(value) === "[object Date]") {
|
|
411
631
|
const date = value;
|
|
412
632
|
return Number.isNaN(date.getTime()) ? null : date;
|
|
@@ -468,6 +688,370 @@ function resolveUrl(value, base) {
|
|
|
468
688
|
return null;
|
|
469
689
|
}
|
|
470
690
|
}
|
|
691
|
+
var DocumentDirectionApplier = class {
|
|
692
|
+
_deps;
|
|
693
|
+
_applyCount = 0;
|
|
694
|
+
constructor(_deps) {
|
|
695
|
+
this._deps = _deps;
|
|
696
|
+
}
|
|
697
|
+
install(language) {
|
|
698
|
+
effect(() => this.applyDirection(language()));
|
|
699
|
+
}
|
|
700
|
+
applyDirection(language) {
|
|
701
|
+
const root = this._deps.document.documentElement;
|
|
702
|
+
if (!root) return;
|
|
703
|
+
root.dir = language.dir;
|
|
704
|
+
root.lang = language.locale ?? language.id;
|
|
705
|
+
}
|
|
706
|
+
apply(language, callback) {
|
|
707
|
+
const config = this._deps.config;
|
|
708
|
+
const service = this._deps.viewTransition;
|
|
709
|
+
this._applyCount++;
|
|
710
|
+
if (!(config?.enabled && (!config.skipInitialTransition || this._applyCount > 1) && service != null)) {
|
|
711
|
+
callback();
|
|
712
|
+
return;
|
|
713
|
+
}
|
|
714
|
+
const info = {
|
|
715
|
+
language: language.id,
|
|
716
|
+
direction: language.dir
|
|
717
|
+
};
|
|
718
|
+
config.onViewTransitionCreated?.(info);
|
|
719
|
+
service.run(callback);
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
var DictionaryStore = class {
|
|
723
|
+
_deps;
|
|
724
|
+
_visible = signal({}, ...ngDevMode ? [{ debugName: "_visible" }] : /* istanbul ignore next */ []);
|
|
725
|
+
_fallbackDict = signal(null, ...ngDevMode ? [{ debugName: "_fallbackDict" }] : /* istanbul ignore next */ []);
|
|
726
|
+
_loadingState = signal("idle", ...ngDevMode ? [{ debugName: "_loadingState" }] : /* istanbul ignore next */ []);
|
|
727
|
+
_loadingError = signal(null, ...ngDevMode ? [{ debugName: "_loadingError" }] : /* istanbul ignore next */ []);
|
|
728
|
+
_cache = /* @__PURE__ */ new Map();
|
|
729
|
+
_generations = /* @__PURE__ */ new Map();
|
|
730
|
+
_inflight = /* @__PURE__ */ new Map();
|
|
731
|
+
_loadingCount = 0;
|
|
732
|
+
_resolvedLoader = null;
|
|
733
|
+
visible = this._visible.asReadonly();
|
|
734
|
+
fallback = this._fallbackDict.asReadonly();
|
|
735
|
+
loadingState = this._loadingState.asReadonly();
|
|
736
|
+
loadingError = this._loadingError.asReadonly();
|
|
737
|
+
constructor(_deps) {
|
|
738
|
+
this._deps = _deps;
|
|
739
|
+
}
|
|
740
|
+
has(languageId) {
|
|
741
|
+
return this._cache.has(languageId) || this._deps.hasInline(languageId);
|
|
742
|
+
}
|
|
743
|
+
seedVisible(data) {
|
|
744
|
+
this._visible.set(data);
|
|
745
|
+
}
|
|
746
|
+
adoptTransferred(languageId, data) {
|
|
747
|
+
this._cache.set(languageId, data);
|
|
748
|
+
this._apply(languageId, data);
|
|
749
|
+
}
|
|
750
|
+
consumeTransfer(languageId) {
|
|
751
|
+
const transfer = this._deps.transfer;
|
|
752
|
+
if (!transfer) return null;
|
|
753
|
+
const key = this._transferKey(languageId);
|
|
754
|
+
if (!transfer.hasKey(key)) return null;
|
|
755
|
+
const dict = transfer.get(key, null);
|
|
756
|
+
transfer.remove(key);
|
|
757
|
+
return dict;
|
|
758
|
+
}
|
|
759
|
+
async load(languageId, opts) {
|
|
760
|
+
const background = opts?.background === true;
|
|
761
|
+
if (!opts?.force) {
|
|
762
|
+
const cached = this._cache.get(languageId);
|
|
763
|
+
if (cached) {
|
|
764
|
+
this._apply(languageId, cached);
|
|
765
|
+
return {
|
|
766
|
+
data: cached,
|
|
767
|
+
ok: true
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
let task = opts?.force ? void 0 : this._inflight.get(languageId);
|
|
772
|
+
if (!task) {
|
|
773
|
+
task = this._fetch(languageId, this._generationOf(languageId));
|
|
774
|
+
this._inflight.set(languageId, task);
|
|
775
|
+
}
|
|
776
|
+
if (!background) {
|
|
777
|
+
this._loadingCount++;
|
|
778
|
+
this._loadingState.set("loading");
|
|
779
|
+
}
|
|
780
|
+
try {
|
|
781
|
+
const result = await task;
|
|
782
|
+
if (result.ok) this._apply(languageId, result.data);
|
|
783
|
+
else if (!background && result.error !== void 0 && languageId === this._deps.currentLanguage()) {
|
|
784
|
+
this._loadingError.set(result.error);
|
|
785
|
+
this._loadingState.set("error");
|
|
786
|
+
}
|
|
787
|
+
return {
|
|
788
|
+
data: result.data,
|
|
789
|
+
ok: result.ok
|
|
790
|
+
};
|
|
791
|
+
} finally {
|
|
792
|
+
if (!background) {
|
|
793
|
+
this._loadingCount = Math.max(0, this._loadingCount - 1);
|
|
794
|
+
if (this._loadingCount === 0 && this._loadingState() === "loading") this._loadingState.set("idle");
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
reload(languageId) {
|
|
799
|
+
if (languageId !== void 0) {
|
|
800
|
+
if (!this._deps.languageIds().includes(languageId)) return Promise.resolve(false);
|
|
801
|
+
this._invalidate(languageId);
|
|
802
|
+
return this._reloadOne(languageId);
|
|
803
|
+
}
|
|
804
|
+
const currentId = this._deps.currentLanguage();
|
|
805
|
+
const fallbackId = this._deps.fallbackLanguage;
|
|
806
|
+
for (const cachedId of [...this._cache.keys()]) if (cachedId !== currentId && cachedId !== fallbackId) this._cache.delete(cachedId);
|
|
807
|
+
for (const language of this._deps.languageIds()) this._invalidate(language);
|
|
808
|
+
const currentLoad = this._reloadOne(currentId);
|
|
809
|
+
if (fallbackId !== null && fallbackId !== currentId) this._reloadOne(fallbackId);
|
|
810
|
+
return currentLoad;
|
|
811
|
+
}
|
|
812
|
+
ensureFallback() {
|
|
813
|
+
const fallbackId = this._deps.fallbackLanguage;
|
|
814
|
+
if (fallbackId === null || fallbackId === this._deps.currentLanguage()) return;
|
|
815
|
+
if (this.has(fallbackId)) return;
|
|
816
|
+
this.load(fallbackId, { background: true });
|
|
817
|
+
}
|
|
818
|
+
_reloadOne(languageId) {
|
|
819
|
+
return this.load(languageId, {
|
|
820
|
+
force: true,
|
|
821
|
+
background: languageId !== this._deps.currentLanguage()
|
|
822
|
+
}).then((result) => result.ok);
|
|
823
|
+
}
|
|
824
|
+
_apply(languageId, data) {
|
|
825
|
+
if (languageId === this._deps.currentLanguage()) {
|
|
826
|
+
this._visible.set(data);
|
|
827
|
+
this._loadingError.set(null);
|
|
828
|
+
if (this._loadingCount === 0 && this._loadingState() === "error") this._loadingState.set("idle");
|
|
829
|
+
}
|
|
830
|
+
if (languageId === this._deps.fallbackLanguage) this._fallbackDict.set(data);
|
|
831
|
+
}
|
|
832
|
+
_fetch(languageId, generation) {
|
|
833
|
+
let task;
|
|
834
|
+
task = (async () => {
|
|
835
|
+
const stale = () => this._generationOf(languageId) !== generation;
|
|
836
|
+
try {
|
|
837
|
+
if (!this._resolvedLoader) this._resolvedLoader = await this._deps.loader(this._deps.loaderContext);
|
|
838
|
+
const translations = await this._resolvedLoader.load(languageId, this._deps.loaderContext);
|
|
839
|
+
if (stale()) return {
|
|
840
|
+
data: {},
|
|
841
|
+
ok: false
|
|
842
|
+
};
|
|
843
|
+
this._cache.set(languageId, translations);
|
|
844
|
+
this._writeTransfer(languageId, translations);
|
|
845
|
+
return {
|
|
846
|
+
data: translations,
|
|
847
|
+
ok: true
|
|
848
|
+
};
|
|
849
|
+
} catch (error) {
|
|
850
|
+
if (stale()) return {
|
|
851
|
+
data: {},
|
|
852
|
+
ok: false
|
|
853
|
+
};
|
|
854
|
+
this._deps.onLoadError?.(languageId, error);
|
|
855
|
+
return {
|
|
856
|
+
data: {},
|
|
857
|
+
ok: false,
|
|
858
|
+
error
|
|
859
|
+
};
|
|
860
|
+
} finally {
|
|
861
|
+
if (this._inflight.get(languageId) === task) this._inflight.delete(languageId);
|
|
862
|
+
}
|
|
863
|
+
})();
|
|
864
|
+
return task;
|
|
865
|
+
}
|
|
866
|
+
_generationOf(languageId) {
|
|
867
|
+
return this._generations.get(languageId) ?? 0;
|
|
868
|
+
}
|
|
869
|
+
_invalidate(languageId) {
|
|
870
|
+
this._generations.set(languageId, this._generationOf(languageId) + 1);
|
|
871
|
+
}
|
|
872
|
+
_transferKey(languageId) {
|
|
873
|
+
return makeStateKey(`rivinia.i18n.dict.${languageId}`);
|
|
874
|
+
}
|
|
875
|
+
_writeTransfer(languageId, dict) {
|
|
876
|
+
if (!this._deps.request) return;
|
|
877
|
+
this._deps.transfer?.set(this._transferKey(languageId), dict);
|
|
878
|
+
}
|
|
879
|
+
};
|
|
880
|
+
var LanguageStorage = class {
|
|
881
|
+
_deps;
|
|
882
|
+
constructor(_deps) {
|
|
883
|
+
this._deps = _deps;
|
|
884
|
+
}
|
|
885
|
+
read() {
|
|
886
|
+
if (this._deps.strategy === "cookie") {
|
|
887
|
+
const header = this._deps.request?.headers.get("cookie") ?? null;
|
|
888
|
+
if (header !== null) return parseCookie(header, this._deps.key);
|
|
889
|
+
try {
|
|
890
|
+
return parseCookie(this._deps.document.cookie, this._deps.key);
|
|
891
|
+
} catch {
|
|
892
|
+
return null;
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
return safeGetItem(this._deps.key, this._deps.strategy);
|
|
896
|
+
}
|
|
897
|
+
write(id) {
|
|
898
|
+
if (this._deps.strategy === "cookie") {
|
|
899
|
+
if (this._deps.request) return;
|
|
900
|
+
try {
|
|
901
|
+
const location = this._deps.document?.location;
|
|
902
|
+
this._deps.document.cookie = serializeCookie(this._deps.key, id, { secure: shouldSecureCookie(location) });
|
|
903
|
+
} catch {}
|
|
904
|
+
return;
|
|
905
|
+
}
|
|
906
|
+
safeSetItem(this._deps.key, id, this._deps.strategy);
|
|
907
|
+
}
|
|
908
|
+
persistAfterSwitch(id) {
|
|
909
|
+
if (!this._deps.persist) return;
|
|
910
|
+
if (this.read() === id) return;
|
|
911
|
+
this.write(id);
|
|
912
|
+
}
|
|
913
|
+
resolveInitialLanguage() {
|
|
914
|
+
const stored = this.read();
|
|
915
|
+
const languages = this._deps.languages();
|
|
916
|
+
if (stored && languages.some((l) => l.id === stored)) return stored;
|
|
917
|
+
if (this._deps.detectBrowser) {
|
|
918
|
+
const detected = detectBrowserLanguage(languages.map((l) => l.id), this._deps.browserLanguages());
|
|
919
|
+
if (detected) return detected;
|
|
920
|
+
}
|
|
921
|
+
return this._deps.defaultLanguage;
|
|
922
|
+
}
|
|
923
|
+
};
|
|
924
|
+
var MissingKeyReporter = class {
|
|
925
|
+
_deps;
|
|
926
|
+
_missingKeys = signal([], ...ngDevMode ? [{ debugName: "_missingKeys" }] : /* istanbul ignore next */ []);
|
|
927
|
+
_reportedKeys = /* @__PURE__ */ new Set();
|
|
928
|
+
_pendingKeys = [];
|
|
929
|
+
_pendingKeyEvents = [];
|
|
930
|
+
_keyFlushQueued = false;
|
|
931
|
+
_reportedParams = /* @__PURE__ */ new Set();
|
|
932
|
+
_pendingParamEvents = [];
|
|
933
|
+
_paramFlushQueued = false;
|
|
934
|
+
missingKeys = this._missingKeys.asReadonly();
|
|
935
|
+
constructor(_deps) {
|
|
936
|
+
this._deps = _deps;
|
|
937
|
+
}
|
|
938
|
+
reportKey(key) {
|
|
939
|
+
if (!this._deps.hasDictionary(this._deps.language())) return;
|
|
940
|
+
if (this._reportedKeys.has(key)) return;
|
|
941
|
+
this._reportedKeys.add(key);
|
|
942
|
+
const language = this._deps.language();
|
|
943
|
+
const fallbackLanguage = this._deps.fallbackLanguage;
|
|
944
|
+
this._pendingKeys.push(key);
|
|
945
|
+
this._pendingKeyEvents.push(fallbackLanguage === null ? {
|
|
946
|
+
key,
|
|
947
|
+
language
|
|
948
|
+
} : {
|
|
949
|
+
key,
|
|
950
|
+
language,
|
|
951
|
+
fallbackLanguage
|
|
952
|
+
});
|
|
953
|
+
this._queueKeyFlush();
|
|
954
|
+
if (this._deps.warnOnMissingKey) console.warn(`[rivinia/i18n] Missing key "${key}" for language "${language}"` + (fallbackLanguage === null ? "." : ` (fallback "${fallbackLanguage}" has no value either).`));
|
|
955
|
+
}
|
|
956
|
+
reportParam(key, param) {
|
|
957
|
+
const dedupe = `${key}\u0000${param}`;
|
|
958
|
+
if (this._reportedParams.has(dedupe)) return;
|
|
959
|
+
this._reportedParams.add(dedupe);
|
|
960
|
+
const language = this._deps.language();
|
|
961
|
+
this._pendingParamEvents.push({
|
|
962
|
+
key,
|
|
963
|
+
param,
|
|
964
|
+
language
|
|
965
|
+
});
|
|
966
|
+
this._queueParamFlush();
|
|
967
|
+
if (this._deps.warnOnMissingParam) console.warn(`[rivinia/i18n] Missing parameter "${param}" for key "${key}" and language "${language}"; the placeholder was kept.`);
|
|
968
|
+
}
|
|
969
|
+
clearKeys() {
|
|
970
|
+
this._reportedKeys.clear();
|
|
971
|
+
this._pendingKeys.length = 0;
|
|
972
|
+
this._pendingKeyEvents.length = 0;
|
|
973
|
+
this._missingKeys.set([]);
|
|
974
|
+
}
|
|
975
|
+
clearParams() {
|
|
976
|
+
this._reportedParams.clear();
|
|
977
|
+
this._pendingParamEvents.length = 0;
|
|
978
|
+
}
|
|
979
|
+
_queueKeyFlush() {
|
|
980
|
+
if (this._keyFlushQueued) return;
|
|
981
|
+
this._keyFlushQueued = true;
|
|
982
|
+
queueMicrotask(() => {
|
|
983
|
+
this._keyFlushQueued = false;
|
|
984
|
+
const pending = this._pendingKeys.splice(0);
|
|
985
|
+
if (pending.length === 0) return;
|
|
986
|
+
this._missingKeys.update((current) => [...current, ...pending]);
|
|
987
|
+
const events = this._pendingKeyEvents.splice(0);
|
|
988
|
+
for (const event of events) this._deps.onMissingKey?.(event);
|
|
989
|
+
});
|
|
990
|
+
}
|
|
991
|
+
_queueParamFlush() {
|
|
992
|
+
if (this._paramFlushQueued) return;
|
|
993
|
+
this._paramFlushQueued = true;
|
|
994
|
+
queueMicrotask(() => {
|
|
995
|
+
this._paramFlushQueued = false;
|
|
996
|
+
const events = this._pendingParamEvents.splice(0);
|
|
997
|
+
for (const event of events) this._deps.onMissingParam?.(event);
|
|
998
|
+
});
|
|
999
|
+
}
|
|
1000
|
+
};
|
|
1001
|
+
const FORMATTER_CACHE_LIMIT = 40;
|
|
1002
|
+
var MessageFormatter = class {
|
|
1003
|
+
_deps;
|
|
1004
|
+
_capacity;
|
|
1005
|
+
_formatters = /* @__PURE__ */ new Map();
|
|
1006
|
+
constructor(_deps, _capacity = FORMATTER_CACHE_LIMIT) {
|
|
1007
|
+
this._deps = _deps;
|
|
1008
|
+
this._capacity = _capacity;
|
|
1009
|
+
}
|
|
1010
|
+
get cachedFormatterCount() {
|
|
1011
|
+
return this._formatters.size;
|
|
1012
|
+
}
|
|
1013
|
+
render(template, params, locale, key) {
|
|
1014
|
+
const quoting = template.includes("'");
|
|
1015
|
+
if (params === void 0 && !quoting) return template;
|
|
1016
|
+
if (!quoting && !template.includes("{")) return template;
|
|
1017
|
+
try {
|
|
1018
|
+
return formatIcu(template, params ?? {}, locale, { onMissingParam: (name) => this._deps.onMissingParam?.(key, name) });
|
|
1019
|
+
} catch {
|
|
1020
|
+
return template;
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
formatNumber(value, options) {
|
|
1024
|
+
return this._numberFormatter(options).format(value);
|
|
1025
|
+
}
|
|
1026
|
+
formatDate(value, options) {
|
|
1027
|
+
return this._dateTimeFormatter(options).format(value);
|
|
1028
|
+
}
|
|
1029
|
+
_numberFormatter(options) {
|
|
1030
|
+
const locale = this._deps.locale();
|
|
1031
|
+
const key = `n|${locale}|${JSON.stringify(options ?? null)}`;
|
|
1032
|
+
return this._memo(key, () => new Intl.NumberFormat(locale, options));
|
|
1033
|
+
}
|
|
1034
|
+
_dateTimeFormatter(options) {
|
|
1035
|
+
const locale = this._deps.locale();
|
|
1036
|
+
const key = `d|${locale}|${JSON.stringify(options ?? null)}`;
|
|
1037
|
+
return this._memo(key, () => new Intl.DateTimeFormat(locale, options));
|
|
1038
|
+
}
|
|
1039
|
+
_memo(key, create) {
|
|
1040
|
+
const cached = this._formatters.get(key);
|
|
1041
|
+
if (cached) {
|
|
1042
|
+
this._formatters.delete(key);
|
|
1043
|
+
this._formatters.set(key, cached);
|
|
1044
|
+
return cached;
|
|
1045
|
+
}
|
|
1046
|
+
const created = create();
|
|
1047
|
+
if (this._formatters.size >= this._capacity) {
|
|
1048
|
+
const oldest = this._formatters.keys().next().value;
|
|
1049
|
+
if (oldest !== void 0) this._formatters.delete(oldest);
|
|
1050
|
+
}
|
|
1051
|
+
this._formatters.set(key, created);
|
|
1052
|
+
return created;
|
|
1053
|
+
}
|
|
1054
|
+
};
|
|
471
1055
|
var ViewTransitionService = class ViewTransitionService {
|
|
472
1056
|
_doc = inject(DOCUMENT);
|
|
473
1057
|
run(callback) {
|
|
@@ -500,27 +1084,43 @@ i0.ɵɵngDeclareClassMetadata({
|
|
|
500
1084
|
var RiviniaI18n = class RiviniaI18n {
|
|
501
1085
|
_config = inject(RIVINIA_I18N_CONFIG);
|
|
502
1086
|
_viewTransition = inject(ViewTransitionService, { optional: true });
|
|
503
|
-
_storageKey = this._config.storageKey ?? "rivinia.i18n.language";
|
|
504
|
-
_storageStrategy = this._config.storageStrategy ?? "local";
|
|
505
1087
|
_autoApplyDirection = this._config.autoApplyDirection ?? true;
|
|
506
1088
|
_fallbackId = this._config.fallbackLanguage ?? null;
|
|
507
1089
|
_doc = inject(DOCUMENT);
|
|
508
1090
|
_request = inject(REQUEST, { optional: true });
|
|
509
1091
|
_transfer = inject(TransferState, { optional: true });
|
|
1092
|
+
_direction = new DocumentDirectionApplier({
|
|
1093
|
+
document: this._doc,
|
|
1094
|
+
viewTransition: this._viewTransition,
|
|
1095
|
+
config: this._config.viewTransition
|
|
1096
|
+
});
|
|
510
1097
|
loaderContext = createLoaderContext(this._request ?? null, this._doc);
|
|
511
|
-
_currentLanguageId = signal(this._resolveInitialLanguage(), ...ngDevMode ? [{ debugName: "_currentLanguageId" }] : /* istanbul ignore next */ []);
|
|
512
1098
|
_languages = signal(this._config.languages, ...ngDevMode ? [{ debugName: "_languages" }] : /* istanbul ignore next */ []);
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
1099
|
+
_languageStorage = new LanguageStorage({
|
|
1100
|
+
key: this._config.storageKey ?? "rivinia.i18n.language",
|
|
1101
|
+
strategy: this._config.storageStrategy ?? "local",
|
|
1102
|
+
languages: () => this._languages(),
|
|
1103
|
+
defaultLanguage: this._config.defaultLanguage,
|
|
1104
|
+
detectBrowser: this._config.detectBrowser ?? false,
|
|
1105
|
+
document: this._doc,
|
|
1106
|
+
request: this._request ?? null,
|
|
1107
|
+
persist: this._config.persist ?? true,
|
|
1108
|
+
browserLanguages
|
|
1109
|
+
});
|
|
1110
|
+
_currentLanguageId = signal(this._languageStorage.resolveInitialLanguage(), ...ngDevMode ? [{ debugName: "_currentLanguageId" }] : /* istanbul ignore next */ []);
|
|
1111
|
+
_store = new DictionaryStore({
|
|
1112
|
+
loader: (context) => this._config.loader ? this._config.loader.resolve(context) : jsonLoader().resolve(context),
|
|
1113
|
+
loaderContext: this.loaderContext,
|
|
1114
|
+
request: this._request ?? null,
|
|
1115
|
+
transfer: this._transfer,
|
|
1116
|
+
onLoadError: this._config.onLoadError,
|
|
1117
|
+
currentLanguage: () => this._currentLanguageId(),
|
|
1118
|
+
fallbackLanguage: this._fallbackId,
|
|
1119
|
+
languageIds: () => this._languages().map((l) => l.id),
|
|
1120
|
+
hasInline: (languageId) => this._config.translations?.[languageId] !== void 0
|
|
1121
|
+
});
|
|
521
1122
|
_initialized = false;
|
|
522
1123
|
_tasks = inject(PendingTasks, { optional: true });
|
|
523
|
-
languageChanged = computed(() => this._currentLanguageId(), ...ngDevMode ? [{ debugName: "languageChanged" }] : /* istanbul ignore next */ []);
|
|
524
1124
|
languages = this._languages.asReadonly();
|
|
525
1125
|
currentLanguage = this._currentLanguageId.asReadonly();
|
|
526
1126
|
currentLanguageObject = computed(() => {
|
|
@@ -529,39 +1129,32 @@ var RiviniaI18n = class RiviniaI18n {
|
|
|
529
1129
|
}, ...ngDevMode ? [{ debugName: "currentLanguageObject" }] : /* istanbul ignore next */ []);
|
|
530
1130
|
dir = computed(() => this.currentLanguageObject().dir, ...ngDevMode ? [{ debugName: "dir" }] : /* istanbul ignore next */ []);
|
|
531
1131
|
isRtl = computed(() => this.dir() === "rtl", ...ngDevMode ? [{ debugName: "isRtl" }] : /* istanbul ignore next */ []);
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
loadingError = this._loadingError.asReadonly();
|
|
536
|
-
isLoading = computed(() => this._loadingState() === "loading", ...ngDevMode ? [{ debugName: "isLoading" }] : /* istanbul ignore next */ []);
|
|
1132
|
+
loadingState = this._store.loadingState;
|
|
1133
|
+
loadingError = this._store.loadingError;
|
|
1134
|
+
isLoading = computed(() => this._store.loadingState() === "loading", ...ngDevMode ? [{ debugName: "isLoading" }] : /* istanbul ignore next */ []);
|
|
537
1135
|
_ready = signal(false, ...ngDevMode ? [{ debugName: "_ready" }] : /* istanbul ignore next */ []);
|
|
538
1136
|
ready = this._ready.asReadonly();
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
1137
|
+
_missingKeyReporter = new MissingKeyReporter({
|
|
1138
|
+
language: () => this._currentLanguageId(),
|
|
1139
|
+
fallbackLanguage: this._fallbackId,
|
|
1140
|
+
hasDictionary: (language) => this._hasDictionary(language),
|
|
1141
|
+
onMissingKey: this._config.onMissingKey,
|
|
1142
|
+
onMissingParam: this._config.onMissingParam,
|
|
1143
|
+
warnOnMissingKey: this._config.warnOnMissingKey ?? (isDevMode() && this._config.onMissingKey === void 0),
|
|
1144
|
+
warnOnMissingParam: this._config.warnOnMissingParam ?? (isDevMode() && this._config.onMissingParam === void 0)
|
|
1145
|
+
});
|
|
1146
|
+
missingKeys = this._missingKeyReporter.missingKeys;
|
|
545
1147
|
clearMissingKeys() {
|
|
546
|
-
this.
|
|
547
|
-
|
|
548
|
-
|
|
1148
|
+
this._missingKeyReporter.clearKeys();
|
|
1149
|
+
}
|
|
1150
|
+
clearMissingParams() {
|
|
1151
|
+
this._missingKeyReporter.clearParams();
|
|
549
1152
|
}
|
|
550
1153
|
constructor() {
|
|
551
1154
|
if (this._config.languages.length === 0) throw new Error("[rivinia/i18n] At least one language must be provided in the languages array.");
|
|
552
1155
|
if (!this._config.languages.some((l) => l.id === this._config.defaultLanguage)) throw new Error(`[rivinia/i18n] defaultLanguage "${this._config.defaultLanguage}" must be one of the configured language IDs.`);
|
|
553
1156
|
if (this._fallbackId !== null && !this._config.languages.some((l) => l.id === this._fallbackId)) throw new Error(`[rivinia/i18n] fallbackLanguage "${this._fallbackId}" must be one of the configured language IDs.`);
|
|
554
|
-
|
|
555
|
-
const id = this._currentLanguageId();
|
|
556
|
-
this._writeStored(id);
|
|
557
|
-
});
|
|
558
|
-
if (this._autoApplyDirection) effect(() => {
|
|
559
|
-
const lang = this.currentLanguageObject();
|
|
560
|
-
const root = this._doc.documentElement;
|
|
561
|
-
if (!root) return;
|
|
562
|
-
root.dir = lang.dir;
|
|
563
|
-
root.lang = lang.id;
|
|
564
|
-
});
|
|
1157
|
+
if (this._autoApplyDirection) this._direction.install(() => this.currentLanguageObject());
|
|
565
1158
|
}
|
|
566
1159
|
getLanguage(id) {
|
|
567
1160
|
return this._languages().find((l) => l.id === id);
|
|
@@ -575,28 +1168,20 @@ var RiviniaI18n = class RiviniaI18n {
|
|
|
575
1168
|
const fallback = this._fallbackId;
|
|
576
1169
|
const hasFallback = fallback !== null && fallback !== current;
|
|
577
1170
|
const preload = this._config.preload ?? true;
|
|
578
|
-
const transferred = this.
|
|
579
|
-
if (transferred)
|
|
580
|
-
|
|
581
|
-
this._loadedTranslations.set(transferred);
|
|
582
|
-
} else {
|
|
1171
|
+
const transferred = this._store.consumeTransfer(current);
|
|
1172
|
+
if (transferred) this._store.adoptTransferred(current, transferred);
|
|
1173
|
+
else {
|
|
583
1174
|
const inline = this._config.translations?.[current];
|
|
584
|
-
if (inline) this.
|
|
1175
|
+
if (inline) this._store.seedVisible(inline);
|
|
585
1176
|
}
|
|
586
1177
|
let fallbackTransferred = null;
|
|
587
1178
|
if (hasFallback) {
|
|
588
|
-
fallbackTransferred = this.
|
|
589
|
-
if (fallbackTransferred)
|
|
590
|
-
this._translationsCache.set(fallback, fallbackTransferred);
|
|
591
|
-
this._fallbackDict.set(fallbackTransferred);
|
|
592
|
-
}
|
|
1179
|
+
fallbackTransferred = this._store.consumeTransfer(fallback);
|
|
1180
|
+
if (fallbackTransferred) this._store.adoptTransferred(fallback, fallbackTransferred);
|
|
593
1181
|
}
|
|
594
1182
|
const loads = [];
|
|
595
|
-
if (!transferred && preload) loads.push(this.
|
|
596
|
-
if (hasFallback && !fallbackTransferred && preload) loads.push(this.
|
|
597
|
-
silent: true,
|
|
598
|
-
background: true
|
|
599
|
-
}));
|
|
1183
|
+
if (!transferred && preload) loads.push(this._store.load(current));
|
|
1184
|
+
if (hasFallback && !fallbackTransferred && preload) loads.push(this._store.load(fallback, { background: true }));
|
|
600
1185
|
await Promise.all(loads);
|
|
601
1186
|
} finally {
|
|
602
1187
|
this._ready.set(true);
|
|
@@ -622,44 +1207,22 @@ var RiviniaI18n = class RiviniaI18n {
|
|
|
622
1207
|
}
|
|
623
1208
|
_resolveKeyText(key, defaultValue, params) {
|
|
624
1209
|
const resolved = this._lookupKey(key);
|
|
625
|
-
if (resolved !== void 0) return this.render(resolved.text, params, resolved.locale);
|
|
626
|
-
if (defaultValue === void 0) this._reportMissingKey(key);
|
|
1210
|
+
if (resolved !== void 0) return this.render(resolved.text, params, resolved.locale, key);
|
|
1211
|
+
if (defaultValue === void 0) untracked(() => this._reportMissingKey(key));
|
|
627
1212
|
const fallback = defaultValue ?? key;
|
|
628
|
-
return params ? this.render(fallback, params, this.locale()) : fallback;
|
|
1213
|
+
return params ? this.render(fallback, params, this.locale(), key) : fallback;
|
|
629
1214
|
}
|
|
630
|
-
loadedTranslations = this.
|
|
631
|
-
async loadTranslations(languageId
|
|
632
|
-
return (await this.
|
|
1215
|
+
loadedTranslations = this._store.visible;
|
|
1216
|
+
async loadTranslations(languageId) {
|
|
1217
|
+
return (await this._store.load(languageId)).data;
|
|
633
1218
|
}
|
|
634
1219
|
reload(languageId) {
|
|
635
|
-
|
|
636
|
-
if (!this._languages().some((l) => l.id === languageId)) return Promise.resolve(false);
|
|
637
|
-
this._invalidate(languageId);
|
|
638
|
-
return this._reloadOne(languageId);
|
|
639
|
-
}
|
|
640
|
-
const currentId = this._currentLanguageId();
|
|
641
|
-
const fallbackId = this._fallbackId;
|
|
642
|
-
for (const cachedId of [...this._translationsCache.keys()]) if (cachedId !== currentId && cachedId !== fallbackId) this._translationsCache.delete(cachedId);
|
|
643
|
-
for (const language of this._languages()) this._invalidate(language.id);
|
|
644
|
-
const currentLoad = this._reloadOne(currentId);
|
|
645
|
-
if (fallbackId !== null && fallbackId !== currentId) this._reloadOne(fallbackId);
|
|
646
|
-
return currentLoad;
|
|
647
|
-
}
|
|
648
|
-
_reloadOne(languageId) {
|
|
649
|
-
return this._loadTranslations(languageId, {
|
|
650
|
-
force: true,
|
|
651
|
-
background: languageId !== this._currentLanguageId()
|
|
652
|
-
}).then((result) => result.ok);
|
|
653
|
-
}
|
|
654
|
-
_generationOf(languageId) {
|
|
655
|
-
return this._generations.get(languageId) ?? 0;
|
|
656
|
-
}
|
|
657
|
-
_invalidate(languageId) {
|
|
658
|
-
this._generations.set(languageId, this._generationOf(languageId) + 1);
|
|
1220
|
+
return this._store.reload(languageId);
|
|
659
1221
|
}
|
|
660
1222
|
toggle() {
|
|
661
1223
|
const langs = this._languages();
|
|
662
1224
|
if (langs.length < 2) return;
|
|
1225
|
+
if (langs.length > 2 && isDevMode()) console.warn("[rivinia/i18n] toggle() only flips the first two languages; use next()/previous() when more than two are configured.");
|
|
663
1226
|
const newId = this._currentLanguageId() === langs[0].id ? langs[1].id : langs[0].id;
|
|
664
1227
|
this.applyLanguage(newId);
|
|
665
1228
|
}
|
|
@@ -685,75 +1248,46 @@ var RiviniaI18n = class RiviniaI18n {
|
|
|
685
1248
|
async setLanguageAsync(id) {
|
|
686
1249
|
if (id === this._currentLanguageId()) return;
|
|
687
1250
|
if (!this._languages().some((l) => l.id === id)) return;
|
|
688
|
-
const { ok } = await this.
|
|
1251
|
+
const { ok } = await this._store.load(id);
|
|
689
1252
|
if (!ok) return;
|
|
690
1253
|
this.applyLanguage(id);
|
|
691
1254
|
}
|
|
692
1255
|
formatNumber(value, options) {
|
|
693
|
-
return this.
|
|
1256
|
+
return this._messageFormatter.formatNumber(value, options);
|
|
694
1257
|
}
|
|
695
1258
|
formatDate(value, options) {
|
|
696
|
-
return this.
|
|
1259
|
+
return this._messageFormatter.formatDate(value, options);
|
|
697
1260
|
}
|
|
698
1261
|
locale = computed(() => {
|
|
699
1262
|
return this.currentLanguageObject().locale ?? this._currentLanguageId();
|
|
700
1263
|
}, ...ngDevMode ? [{ debugName: "locale" }] : /* istanbul ignore next */ []);
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
if (!formatter) {
|
|
706
|
-
formatter = new Intl.NumberFormat(this.locale(), options);
|
|
707
|
-
this._formatters.set(key, formatter);
|
|
708
|
-
}
|
|
709
|
-
return formatter;
|
|
710
|
-
}
|
|
711
|
-
dateTimeFormatter(options) {
|
|
712
|
-
const key = `d|${this.locale()}|${JSON.stringify(options ?? null)}`;
|
|
713
|
-
let formatter = this._formatters.get(key);
|
|
714
|
-
if (!formatter) {
|
|
715
|
-
formatter = new Intl.DateTimeFormat(this.locale(), options);
|
|
716
|
-
this._formatters.set(key, formatter);
|
|
717
|
-
}
|
|
718
|
-
return formatter;
|
|
719
|
-
}
|
|
1264
|
+
_messageFormatter = new MessageFormatter({
|
|
1265
|
+
locale: () => this.locale(),
|
|
1266
|
+
onMissingParam: (key, name) => untracked(() => this._reportMissingParam(key, name))
|
|
1267
|
+
});
|
|
720
1268
|
applyLanguage(id, opts) {
|
|
721
1269
|
if (id === this._currentLanguageId()) return;
|
|
722
1270
|
const lang = this._languages().find((l) => l.id === id);
|
|
723
1271
|
if (!lang) return;
|
|
724
|
-
this.
|
|
725
|
-
const apply = () => this._currentLanguageId.set(id);
|
|
726
|
-
const vt = this._config.viewTransition;
|
|
727
|
-
if (vt?.enabled && (!vt.skipInitialTransition || this._applyCount > 1) && this._viewTransition != null) {
|
|
728
|
-
vt.onViewTransitionCreated?.({
|
|
729
|
-
language: id,
|
|
730
|
-
direction: lang.dir
|
|
731
|
-
});
|
|
732
|
-
this._viewTransition.run(apply);
|
|
733
|
-
} else apply();
|
|
1272
|
+
this._direction.apply(lang, () => this._currentLanguageId.set(id));
|
|
734
1273
|
if (!opts?.skipLoad) {
|
|
735
|
-
this.
|
|
736
|
-
this.
|
|
1274
|
+
this._store.load(id);
|
|
1275
|
+
this._store.ensureFallback();
|
|
737
1276
|
}
|
|
1277
|
+
this._languageStorage.persistAfterSwitch(id);
|
|
738
1278
|
}
|
|
739
|
-
render(template, params, locale) {
|
|
740
|
-
|
|
741
|
-
if (!template.includes("{")) return template;
|
|
742
|
-
try {
|
|
743
|
-
return formatIcu(template, params, locale);
|
|
744
|
-
} catch {
|
|
745
|
-
return template;
|
|
746
|
-
}
|
|
1279
|
+
render(template, params, locale, key) {
|
|
1280
|
+
return this._messageFormatter.render(template, params, locale, key);
|
|
747
1281
|
}
|
|
748
1282
|
_lookupKey(key) {
|
|
749
|
-
const currentValue = this._resolveKey(this.
|
|
1283
|
+
const currentValue = this._resolveKey(this._store.visible(), key);
|
|
750
1284
|
if (currentValue !== void 0) return {
|
|
751
1285
|
text: currentValue,
|
|
752
1286
|
locale: this.locale()
|
|
753
1287
|
};
|
|
754
1288
|
const fallbackId = this._fallbackId;
|
|
755
1289
|
if (fallbackId === null || fallbackId === this._currentLanguageId()) return void 0;
|
|
756
|
-
const fallbackDict = this.
|
|
1290
|
+
const fallbackDict = this._store.fallback() ?? this._config.translations?.[fallbackId] ?? null;
|
|
757
1291
|
if (!fallbackDict) return void 0;
|
|
758
1292
|
const fallbackValue = this._resolveKey(fallbackDict, key);
|
|
759
1293
|
if (fallbackValue === void 0) return void 0;
|
|
@@ -765,164 +1299,20 @@ var RiviniaI18n = class RiviniaI18n {
|
|
|
765
1299
|
_localeOf(languageId) {
|
|
766
1300
|
return this._languages().find((l) => l.id === languageId)?.locale ?? languageId;
|
|
767
1301
|
}
|
|
768
|
-
_ensureFallback() {
|
|
769
|
-
const fallbackId = this._fallbackId;
|
|
770
|
-
if (fallbackId === null || fallbackId === this._currentLanguageId()) return;
|
|
771
|
-
if (this._hasDictionary(fallbackId)) return;
|
|
772
|
-
this._loadTranslations(fallbackId, {
|
|
773
|
-
silent: true,
|
|
774
|
-
background: true
|
|
775
|
-
});
|
|
776
|
-
}
|
|
777
1302
|
_hasDictionary(languageId) {
|
|
778
|
-
return this.
|
|
1303
|
+
return this._store.has(languageId);
|
|
779
1304
|
}
|
|
780
1305
|
_reportMissingKey(key) {
|
|
781
|
-
|
|
782
|
-
if (this._reportedMissingKeys.has(key)) return;
|
|
783
|
-
this._reportedMissingKeys.add(key);
|
|
784
|
-
const language = this._currentLanguageId();
|
|
785
|
-
const fallbackLanguage = this._fallbackId;
|
|
786
|
-
const event = fallbackLanguage === null ? {
|
|
787
|
-
key,
|
|
788
|
-
language
|
|
789
|
-
} : {
|
|
790
|
-
key,
|
|
791
|
-
language,
|
|
792
|
-
fallbackLanguage
|
|
793
|
-
};
|
|
794
|
-
this._pendingMissingKeys.push(key);
|
|
795
|
-
this._queueMissingFlush();
|
|
796
|
-
this._config.onMissingKey?.(event);
|
|
797
|
-
if (this._warnOnMissingKey) console.warn(`[rivinia/i18n] Missing key "${key}" for language "${language}"` + (fallbackLanguage === null ? "." : ` (fallback "${fallbackLanguage}" has no value either).`));
|
|
798
|
-
}
|
|
799
|
-
_queueMissingFlush() {
|
|
800
|
-
if (this._missingFlushQueued) return;
|
|
801
|
-
this._missingFlushQueued = true;
|
|
802
|
-
queueMicrotask(() => {
|
|
803
|
-
this._missingFlushQueued = false;
|
|
804
|
-
const pending = this._pendingMissingKeys.splice(0);
|
|
805
|
-
if (pending.length === 0) return;
|
|
806
|
-
this._missingKeys.update((current) => [...current, ...pending]);
|
|
807
|
-
});
|
|
1306
|
+
this._missingKeyReporter.reportKey(key);
|
|
808
1307
|
}
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
const background = opts?.background === true;
|
|
812
|
-
if (!opts?.force) {
|
|
813
|
-
const cached = this._translationsCache.get(languageId);
|
|
814
|
-
if (cached) {
|
|
815
|
-
if (!opts?.silent && current()) this._loadedTranslations.set(cached);
|
|
816
|
-
if (languageId === this._fallbackId) this._fallbackDict.set(cached);
|
|
817
|
-
return {
|
|
818
|
-
data: cached,
|
|
819
|
-
ok: true
|
|
820
|
-
};
|
|
821
|
-
}
|
|
822
|
-
const inflight = this._inflight.get(languageId);
|
|
823
|
-
if (inflight) return inflight;
|
|
824
|
-
}
|
|
825
|
-
if (!background) this._loadingState.set("loading");
|
|
826
|
-
const generation = this._generationOf(languageId);
|
|
827
|
-
let task;
|
|
828
|
-
task = (async () => {
|
|
829
|
-
const stale = () => this._generationOf(languageId) !== generation;
|
|
830
|
-
try {
|
|
831
|
-
if (!this._resolvedLoader) this._resolvedLoader = await this._getLoader(this.loaderContext);
|
|
832
|
-
const translations = await this._resolvedLoader.load(languageId, this.loaderContext);
|
|
833
|
-
if (stale()) return {
|
|
834
|
-
data: {},
|
|
835
|
-
ok: false
|
|
836
|
-
};
|
|
837
|
-
this._translationsCache.set(languageId, translations);
|
|
838
|
-
this.writeTransfer(languageId, translations);
|
|
839
|
-
if (languageId === this._fallbackId && !current()) this._fallbackDict.set(translations);
|
|
840
|
-
if (!opts?.silent && current()) {
|
|
841
|
-
this._loadedTranslations.set(translations);
|
|
842
|
-
this._loadingError.set(null);
|
|
843
|
-
this._loadingState.set("idle");
|
|
844
|
-
}
|
|
845
|
-
return {
|
|
846
|
-
data: translations,
|
|
847
|
-
ok: true
|
|
848
|
-
};
|
|
849
|
-
} catch (error) {
|
|
850
|
-
if (stale()) return {
|
|
851
|
-
data: {},
|
|
852
|
-
ok: false
|
|
853
|
-
};
|
|
854
|
-
if (!background && current()) {
|
|
855
|
-
this._loadingError.set(error);
|
|
856
|
-
this._loadingState.set("error");
|
|
857
|
-
}
|
|
858
|
-
this._config.onLoadError?.(languageId, error);
|
|
859
|
-
return {
|
|
860
|
-
data: {},
|
|
861
|
-
ok: false
|
|
862
|
-
};
|
|
863
|
-
} finally {
|
|
864
|
-
if (this._inflight.get(languageId) === task) {
|
|
865
|
-
this._inflight.delete(languageId);
|
|
866
|
-
if (!background && this._inflight.size === 0 && this._loadingState() === "loading") this._loadingState.set("idle");
|
|
867
|
-
}
|
|
868
|
-
}
|
|
869
|
-
})();
|
|
870
|
-
this._inflight.set(languageId, task);
|
|
871
|
-
return task;
|
|
872
|
-
}
|
|
873
|
-
_readStored() {
|
|
874
|
-
if (this._storageStrategy === "cookie") {
|
|
875
|
-
const header = this._request?.headers.get("cookie") ?? null;
|
|
876
|
-
if (header !== null) return parseCookie(header, this._storageKey);
|
|
877
|
-
try {
|
|
878
|
-
return parseCookie(this._doc.cookie, this._storageKey);
|
|
879
|
-
} catch {
|
|
880
|
-
return null;
|
|
881
|
-
}
|
|
882
|
-
}
|
|
883
|
-
return safeGetItem(this._storageKey, this._storageStrategy);
|
|
884
|
-
}
|
|
885
|
-
_writeStored(id) {
|
|
886
|
-
if (this._storageStrategy === "cookie") {
|
|
887
|
-
if (this._request) return;
|
|
888
|
-
try {
|
|
889
|
-
const location = this._doc?.location;
|
|
890
|
-
this._doc.cookie = serializeCookie(this._storageKey, id, { secure: shouldSecureCookie(location) });
|
|
891
|
-
} catch {}
|
|
892
|
-
return;
|
|
893
|
-
}
|
|
894
|
-
safeSetItem(this._storageKey, id, this._storageStrategy);
|
|
895
|
-
}
|
|
896
|
-
transferKey(languageId) {
|
|
897
|
-
return makeStateKey(`rivinia.i18n.dict.${languageId}`);
|
|
898
|
-
}
|
|
899
|
-
readTransfer(languageId) {
|
|
900
|
-
const transfer = this._transfer;
|
|
901
|
-
if (!transfer) return null;
|
|
902
|
-
const key = this.transferKey(languageId);
|
|
903
|
-
if (!transfer.hasKey(key)) return null;
|
|
904
|
-
const dict = transfer.get(key, null);
|
|
905
|
-
transfer.remove(key);
|
|
906
|
-
return dict;
|
|
907
|
-
}
|
|
908
|
-
writeTransfer(languageId, dict) {
|
|
909
|
-
if (!this._request) return;
|
|
910
|
-
this._transfer?.set(this.transferKey(languageId), dict);
|
|
1308
|
+
_reportMissingParam(key, param) {
|
|
1309
|
+
this._missingKeyReporter.reportParam(key, param);
|
|
911
1310
|
}
|
|
912
1311
|
_resolveKey(obj, path) {
|
|
913
|
-
const value = path.split(".").reduce((acc, segment) => acc && typeof acc === "object" ? acc[segment] : void 0, obj);
|
|
1312
|
+
const value = Object.prototype.hasOwnProperty.call(obj, path) ? obj[path] : path.split(".").reduce((acc, segment) => acc && typeof acc === "object" ? acc[segment] : void 0, obj);
|
|
914
1313
|
if (typeof value === "string") return value;
|
|
915
1314
|
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
916
1315
|
}
|
|
917
|
-
_resolveInitialLanguage() {
|
|
918
|
-
const stored = this._readStored();
|
|
919
|
-
if (stored && this._config.languages.some((l) => l.id === stored)) return stored;
|
|
920
|
-
if (this._config.detectBrowser) {
|
|
921
|
-
const detected = detectBrowserLanguage(this._config.languages.map((l) => l.id), browserLanguages());
|
|
922
|
-
if (detected) return detected;
|
|
923
|
-
}
|
|
924
|
-
return this._config.defaultLanguage;
|
|
925
|
-
}
|
|
926
1316
|
static ɵfac = i0.ɵɵngDeclareFactory({
|
|
927
1317
|
minVersion: "12.0.0",
|
|
928
1318
|
version: "22.2.3",
|
|
@@ -971,7 +1361,7 @@ function defineRiviniaI18n(config) {
|
|
|
971
1361
|
inject: () => injectRiviniaI18n()
|
|
972
1362
|
};
|
|
973
1363
|
}
|
|
974
|
-
var
|
|
1364
|
+
var RvTranslatePipe = class RvTranslatePipe {
|
|
975
1365
|
_i18n = inject(RiviniaI18n);
|
|
976
1366
|
transform(keyOrTranslations, defaultValueOrParams, maybeParams) {
|
|
977
1367
|
return this._i18n.translate(keyOrTranslations, defaultValueOrParams, maybeParams);
|
|
@@ -980,7 +1370,7 @@ var RTranslatePipe = class RTranslatePipe {
|
|
|
980
1370
|
minVersion: "12.0.0",
|
|
981
1371
|
version: "22.2.3",
|
|
982
1372
|
ngImport: i0,
|
|
983
|
-
type:
|
|
1373
|
+
type: RvTranslatePipe,
|
|
984
1374
|
deps: [],
|
|
985
1375
|
target: i0.ɵɵFactoryTarget.Pipe
|
|
986
1376
|
});
|
|
@@ -988,9 +1378,9 @@ var RTranslatePipe = class RTranslatePipe {
|
|
|
988
1378
|
minVersion: "14.0.0",
|
|
989
1379
|
version: "22.2.3",
|
|
990
1380
|
ngImport: i0,
|
|
991
|
-
type:
|
|
1381
|
+
type: RvTranslatePipe,
|
|
992
1382
|
isStandalone: true,
|
|
993
|
-
name: "
|
|
1383
|
+
name: "rvTranslate",
|
|
994
1384
|
pure: false
|
|
995
1385
|
});
|
|
996
1386
|
};
|
|
@@ -998,15 +1388,15 @@ i0.ɵɵngDeclareClassMetadata({
|
|
|
998
1388
|
minVersion: "12.0.0",
|
|
999
1389
|
version: "22.2.3",
|
|
1000
1390
|
ngImport: i0,
|
|
1001
|
-
type:
|
|
1391
|
+
type: RvTranslatePipe,
|
|
1002
1392
|
decorators: [{
|
|
1003
1393
|
type: Pipe,
|
|
1004
1394
|
args: [{
|
|
1005
|
-
name: "
|
|
1395
|
+
name: "rvTranslate",
|
|
1006
1396
|
pure: false
|
|
1007
1397
|
}]
|
|
1008
1398
|
}]
|
|
1009
1399
|
});
|
|
1010
|
-
export {
|
|
1400
|
+
export { RiviniaI18n, RiviniaLoadError, RvTranslatePipe, ViewTransitionService, browserLanguages, customLoader, defineRiviniaI18n, detectBrowserLanguage, formatIcu, httpLoader, injectRiviniaI18n, jsonLoader, parseCookie, provideRiviniaI18n, remoteLoader, serializeCookie };
|
|
1011
1401
|
|
|
1012
1402
|
//# sourceMappingURL=rivinia-i18n.mjs.map
|