@rivinia/i18n 0.3.3 → 0.5.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 +103 -0
- package/README.md +440 -153
- package/cli/rivinia-i18n-keys.mjs +400 -0
- 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 +781 -169
- package/fesm2022/rivinia-i18n.mjs.map +1 -1
- package/package.json +26 -2
- package/types/rivinia-i18n.d.ts +327 -53
- package/fesm2022/rivinia-i18n-http-loader.impl-DVlKmSDL.mjs +0 -15
- package/fesm2022/rivinia-i18n-http-loader.impl-DVlKmSDL.mjs.map +0 -1
- package/fesm2022/rivinia-i18n-json-loader.impl-VXwAmyWL.mjs +0 -16
- package/fesm2022/rivinia-i18n-json-loader.impl-VXwAmyWL.mjs.map +0 -1
- package/fesm2022/rivinia-i18n-remote-loader.impl-XKqdLHoJ.mjs +0 -15
- package/fesm2022/rivinia-i18n-remote-loader.impl-XKqdLHoJ.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, 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-VXwAmyWL.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,157 +135,251 @@ 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 (
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
};
|
|
169
|
-
if (type.word !== "plural" && type.word !== "select") return {
|
|
170
|
-
text: src.slice(start, end),
|
|
171
|
-
next: end,
|
|
172
|
-
ok: true
|
|
173
|
-
};
|
|
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";
|
|
174
254
|
i++;
|
|
175
|
-
const numeric = type.word === "plural" ? toFiniteNumber(params[name.word]) : null;
|
|
176
255
|
const branches = /* @__PURE__ */ new Map();
|
|
256
|
+
const exacts = /* @__PURE__ */ new Map();
|
|
257
|
+
let offset = 0;
|
|
177
258
|
i = skipSpaces(src, i, end);
|
|
178
259
|
let closed = false;
|
|
179
260
|
while (i < end) {
|
|
180
261
|
i = skipSpaces(src, i, end);
|
|
181
|
-
if (i < end && src
|
|
262
|
+
if (i < end && src.charCodeAt(i) === 125) {
|
|
182
263
|
closed = true;
|
|
183
264
|
break;
|
|
184
265
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
const body =
|
|
198
|
-
branches.set(
|
|
266
|
+
if (branchPlural) {
|
|
267
|
+
const offsetClause = readOffset(src, i, end);
|
|
268
|
+
if (offsetClause) {
|
|
269
|
+
offset = offsetClause.value;
|
|
270
|
+
i = offsetClause.next;
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
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
|
+
}
|
|
199
284
|
i = body.next + 1;
|
|
200
285
|
}
|
|
201
|
-
if (!closed) return
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
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
|
|
205
294
|
};
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
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
|
|
305
|
+
};
|
|
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);
|
|
209
313
|
return {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
314
|
+
node: {
|
|
315
|
+
type: kind,
|
|
316
|
+
name,
|
|
317
|
+
style,
|
|
318
|
+
raw
|
|
319
|
+
},
|
|
320
|
+
next: i + 1
|
|
213
321
|
};
|
|
214
322
|
}
|
|
215
|
-
if (
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
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);
|
|
329
|
+
return {
|
|
330
|
+
node: {
|
|
331
|
+
type: kind,
|
|
332
|
+
name,
|
|
333
|
+
style,
|
|
334
|
+
raw
|
|
335
|
+
},
|
|
336
|
+
next: after + 1
|
|
225
337
|
};
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
} catch {
|
|
230
|
-
category = "other";
|
|
231
|
-
}
|
|
338
|
+
}
|
|
339
|
+
function rawNode(src, start, end) {
|
|
340
|
+
const segment = rawSegment(src, start, end);
|
|
232
341
|
return {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
342
|
+
node: {
|
|
343
|
+
type: "raw",
|
|
344
|
+
value: segment.text
|
|
345
|
+
},
|
|
346
|
+
next: segment.next
|
|
236
347
|
};
|
|
237
348
|
}
|
|
238
|
-
function
|
|
239
|
-
|
|
240
|
-
if (
|
|
241
|
-
|
|
242
|
-
|
|
349
|
+
function readOffset(src, i, end) {
|
|
350
|
+
const start = skipSpaces(src, i, end);
|
|
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
|
+
};
|
|
243
362
|
}
|
|
244
363
|
return null;
|
|
245
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
|
+
}
|
|
246
371
|
function skipSpaces(src, i, end) {
|
|
247
|
-
while (i < end &&
|
|
372
|
+
while (i < end && isSpace(src.charCodeAt(i))) i++;
|
|
248
373
|
return i;
|
|
249
374
|
}
|
|
250
375
|
function readWord(src, i, end) {
|
|
251
376
|
const start = skipSpaces(src, i, end);
|
|
252
377
|
let j = start;
|
|
253
|
-
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
|
+
}
|
|
254
383
|
return {
|
|
255
384
|
word: src.slice(start, j),
|
|
256
385
|
next: j
|
|
@@ -258,14 +387,14 @@ function readWord(src, i, end) {
|
|
|
258
387
|
}
|
|
259
388
|
function readSelector(src, i, end) {
|
|
260
389
|
i = skipSpaces(src, i, end);
|
|
261
|
-
if (i < end && src
|
|
390
|
+
if (i < end && src.charCodeAt(i) === 61) {
|
|
262
391
|
let j = i + 1;
|
|
263
|
-
if (j < end && src
|
|
392
|
+
if (j < end && src.charCodeAt(j) === 45) j++;
|
|
264
393
|
const digits = j;
|
|
265
|
-
while (j < end &&
|
|
266
|
-
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))) {
|
|
267
396
|
j++;
|
|
268
|
-
while (j < end &&
|
|
397
|
+
while (j < end && isDigit(src.charCodeAt(j))) j++;
|
|
269
398
|
}
|
|
270
399
|
if (j === digits) return {
|
|
271
400
|
selector: null,
|
|
@@ -286,7 +415,279 @@ function readSelector(src, i, end) {
|
|
|
286
415
|
next: word.next
|
|
287
416
|
};
|
|
288
417
|
}
|
|
418
|
+
function rawSegment(src, start, end) {
|
|
419
|
+
let depth = 0;
|
|
420
|
+
let i = start;
|
|
421
|
+
while (i < end) {
|
|
422
|
+
const ch = src.charCodeAt(i);
|
|
423
|
+
if (ch === 123) depth++;
|
|
424
|
+
else if (ch === 125) {
|
|
425
|
+
depth--;
|
|
426
|
+
if (depth === 0) return {
|
|
427
|
+
text: src.slice(start, i + 1),
|
|
428
|
+
next: i + 1
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
i++;
|
|
432
|
+
}
|
|
433
|
+
return {
|
|
434
|
+
text: src.slice(start, end),
|
|
435
|
+
next: end
|
|
436
|
+
};
|
|
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
|
+
}
|
|
535
|
+
const CACHE_LIMIT = 40;
|
|
536
|
+
const numberFormats = /* @__PURE__ */ new Map();
|
|
537
|
+
const dateFormats = /* @__PURE__ */ new Map();
|
|
538
|
+
const pluralRulesByLocale = /* @__PURE__ */ new Map();
|
|
539
|
+
const pluralCategories = /* @__PURE__ */ new Map();
|
|
540
|
+
const formattedNumbers = /* @__PURE__ */ new Map();
|
|
541
|
+
const FORMATTED_NUMBERS_LIMIT = 1e3;
|
|
542
|
+
function memo(map, key, create) {
|
|
543
|
+
const hit = map.get(key);
|
|
544
|
+
if (hit !== void 0) return hit;
|
|
545
|
+
const created = create();
|
|
546
|
+
if (map.size >= CACHE_LIMIT) map.clear();
|
|
547
|
+
map.set(key, created);
|
|
548
|
+
return created;
|
|
549
|
+
}
|
|
550
|
+
function pluralRules(locale, ordinal) {
|
|
551
|
+
const key = `${ordinal ? "o" : "c"}|${locale}`;
|
|
552
|
+
return memo(pluralRulesByLocale, key, () => new Intl.PluralRules(locale, ordinal ? { type: "ordinal" } : void 0));
|
|
553
|
+
}
|
|
554
|
+
function formatNumberValue(value, locale, style) {
|
|
555
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return null;
|
|
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;
|
|
570
|
+
try {
|
|
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;
|
|
575
|
+
} catch {
|
|
576
|
+
return null;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
function numberOptions(style) {
|
|
580
|
+
if (style === void 0) return {};
|
|
581
|
+
if (style === "percent") return { style: "percent" };
|
|
582
|
+
if (style === "integer") return { maximumFractionDigits: 0 };
|
|
583
|
+
if (style.startsWith("::currency/")) {
|
|
584
|
+
const code = style.slice(11);
|
|
585
|
+
if (/^[A-Za-z]{3}$/.test(code)) return {
|
|
586
|
+
style: "currency",
|
|
587
|
+
currency: code.toUpperCase()
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
return null;
|
|
591
|
+
}
|
|
592
|
+
const DATE_STYLES = /* @__PURE__ */ new Set([
|
|
593
|
+
"short",
|
|
594
|
+
"medium",
|
|
595
|
+
"long",
|
|
596
|
+
"full"
|
|
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})?$/;
|
|
600
|
+
function formatDateValue(value, locale, style) {
|
|
601
|
+
const date = toDate(value);
|
|
602
|
+
if (date === null) return null;
|
|
603
|
+
if (style !== void 0 && !DATE_STYLES.has(style)) return null;
|
|
604
|
+
const options = style === void 0 ? {} : { dateStyle: style };
|
|
605
|
+
try {
|
|
606
|
+
const key = `${locale}|${style ?? ""}`;
|
|
607
|
+
return memo(dateFormats, key, () => new Intl.DateTimeFormat(locale, options)).format(date);
|
|
608
|
+
} catch {
|
|
609
|
+
return null;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
function toDate(value) {
|
|
613
|
+
if (typeof value === "number") {
|
|
614
|
+
if (!Number.isFinite(value)) return null;
|
|
615
|
+
const date = new Date(value);
|
|
616
|
+
return Number.isNaN(date.getTime()) ? null : date;
|
|
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
|
+
}
|
|
630
|
+
if (Object.prototype.toString.call(value) === "[object Date]") {
|
|
631
|
+
const date = value;
|
|
632
|
+
return Number.isNaN(date.getTime()) ? null : date;
|
|
633
|
+
}
|
|
634
|
+
return null;
|
|
635
|
+
}
|
|
289
636
|
const RIVINIA_I18N_CONFIG = new InjectionToken("RIVINIA_I18N_CONFIG");
|
|
637
|
+
function createLoaderContext(request, doc) {
|
|
638
|
+
const documentBase = readBaseUri(doc);
|
|
639
|
+
if (!request) return {
|
|
640
|
+
baseUrl: documentBase,
|
|
641
|
+
isServer: false
|
|
642
|
+
};
|
|
643
|
+
const requestUrl = parseUrl(request.url);
|
|
644
|
+
const baseHref = readBaseHref(doc);
|
|
645
|
+
if (baseHref) {
|
|
646
|
+
const resolved = resolveUrl(baseHref, requestUrl?.href ?? documentBase);
|
|
647
|
+
if (resolved) return {
|
|
648
|
+
baseUrl: resolved,
|
|
649
|
+
isServer: true
|
|
650
|
+
};
|
|
651
|
+
}
|
|
652
|
+
if (requestUrl) return {
|
|
653
|
+
baseUrl: `${requestUrl.origin}/`,
|
|
654
|
+
isServer: true
|
|
655
|
+
};
|
|
656
|
+
return {
|
|
657
|
+
baseUrl: documentBase,
|
|
658
|
+
isServer: true
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
function readBaseUri(doc) {
|
|
662
|
+
try {
|
|
663
|
+
const uri = doc?.baseURI;
|
|
664
|
+
if (typeof uri === "string" && uri.length > 0) return uri;
|
|
665
|
+
} catch {}
|
|
666
|
+
return "/";
|
|
667
|
+
}
|
|
668
|
+
function readBaseHref(doc) {
|
|
669
|
+
try {
|
|
670
|
+
const href = (doc.querySelector?.("base[href]"))?.getAttribute("href")?.trim();
|
|
671
|
+
return href ? href : null;
|
|
672
|
+
} catch {
|
|
673
|
+
return null;
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
function parseUrl(value) {
|
|
677
|
+
if (!value) return null;
|
|
678
|
+
try {
|
|
679
|
+
return new URL(value);
|
|
680
|
+
} catch {
|
|
681
|
+
return null;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
function resolveUrl(value, base) {
|
|
685
|
+
try {
|
|
686
|
+
return new URL(value, base).href;
|
|
687
|
+
} catch {
|
|
688
|
+
return null;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
290
691
|
var ViewTransitionService = class ViewTransitionService {
|
|
291
692
|
_doc = inject(DOCUMENT);
|
|
292
693
|
run(callback) {
|
|
@@ -296,7 +697,7 @@ var ViewTransitionService = class ViewTransitionService {
|
|
|
296
697
|
}
|
|
297
698
|
static ɵfac = i0.ɵɵngDeclareFactory({
|
|
298
699
|
minVersion: "12.0.0",
|
|
299
|
-
version: "22.2.
|
|
700
|
+
version: "22.2.3",
|
|
300
701
|
ngImport: i0,
|
|
301
702
|
type: ViewTransitionService,
|
|
302
703
|
deps: [],
|
|
@@ -304,14 +705,14 @@ var ViewTransitionService = class ViewTransitionService {
|
|
|
304
705
|
});
|
|
305
706
|
static ɵprov = i0.ɵɵngDeclareService({
|
|
306
707
|
minVersion: "22.0.0",
|
|
307
|
-
version: "22.2.
|
|
708
|
+
version: "22.2.3",
|
|
308
709
|
ngImport: i0,
|
|
309
710
|
type: ViewTransitionService
|
|
310
711
|
});
|
|
311
712
|
};
|
|
312
713
|
i0.ɵɵngDeclareClassMetadata({
|
|
313
714
|
minVersion: "12.0.0",
|
|
314
|
-
version: "22.2.
|
|
715
|
+
version: "22.2.3",
|
|
315
716
|
ngImport: i0,
|
|
316
717
|
type: ViewTransitionService,
|
|
317
718
|
decorators: [{ type: Service }]
|
|
@@ -322,16 +723,22 @@ var RiviniaI18n = class RiviniaI18n {
|
|
|
322
723
|
_storageKey = this._config.storageKey ?? "rivinia.i18n.language";
|
|
323
724
|
_storageStrategy = this._config.storageStrategy ?? "local";
|
|
324
725
|
_autoApplyDirection = this._config.autoApplyDirection ?? true;
|
|
726
|
+
_persist = this._config.persist ?? true;
|
|
727
|
+
_fallbackId = this._config.fallbackLanguage ?? null;
|
|
325
728
|
_doc = inject(DOCUMENT);
|
|
326
729
|
_request = inject(REQUEST, { optional: true });
|
|
327
730
|
_transfer = inject(TransferState, { optional: true });
|
|
731
|
+
loaderContext = createLoaderContext(this._request ?? null, this._doc);
|
|
328
732
|
_currentLanguageId = signal(this._resolveInitialLanguage(), ...ngDevMode ? [{ debugName: "_currentLanguageId" }] : /* istanbul ignore next */ []);
|
|
329
733
|
_languages = signal(this._config.languages, ...ngDevMode ? [{ debugName: "_languages" }] : /* istanbul ignore next */ []);
|
|
330
734
|
_loadedTranslations = signal({}, ...ngDevMode ? [{ debugName: "_loadedTranslations" }] : /* istanbul ignore next */ []);
|
|
331
|
-
|
|
735
|
+
_fallbackDict = signal(null, ...ngDevMode ? [{ debugName: "_fallbackDict" }] : /* istanbul ignore next */ []);
|
|
736
|
+
_getLoader = (context) => this._config.loader ? this._config.loader.resolve(context) : jsonLoader().resolve(context);
|
|
332
737
|
_resolvedLoader = null;
|
|
333
738
|
_translationsCache = /* @__PURE__ */ new Map();
|
|
739
|
+
_generations = /* @__PURE__ */ new Map();
|
|
334
740
|
_inflight = /* @__PURE__ */ new Map();
|
|
741
|
+
_loadingCount = 0;
|
|
335
742
|
_applyCount = 0;
|
|
336
743
|
_initialized = false;
|
|
337
744
|
_tasks = inject(PendingTasks, { optional: true });
|
|
@@ -351,19 +758,37 @@ var RiviniaI18n = class RiviniaI18n {
|
|
|
351
758
|
isLoading = computed(() => this._loadingState() === "loading", ...ngDevMode ? [{ debugName: "isLoading" }] : /* istanbul ignore next */ []);
|
|
352
759
|
_ready = signal(false, ...ngDevMode ? [{ debugName: "_ready" }] : /* istanbul ignore next */ []);
|
|
353
760
|
ready = this._ready.asReadonly();
|
|
761
|
+
_warnOnMissingKey = this._config.warnOnMissingKey ?? (isDevMode() && this._config.onMissingKey === void 0);
|
|
762
|
+
_missingKeys = signal([], ...ngDevMode ? [{ debugName: "_missingKeys" }] : /* istanbul ignore next */ []);
|
|
763
|
+
_reportedMissingKeys = /* @__PURE__ */ new Set();
|
|
764
|
+
_pendingMissingKeys = [];
|
|
765
|
+
_pendingMissingEvents = [];
|
|
766
|
+
_missingFlushQueued = false;
|
|
767
|
+
_warnOnMissingParam = this._config.warnOnMissingParam ?? (isDevMode() && this._config.onMissingParam === void 0);
|
|
768
|
+
_reportedMissingParams = /* @__PURE__ */ new Set();
|
|
769
|
+
_pendingMissingParams = [];
|
|
770
|
+
_missingParamsFlushQueued = false;
|
|
771
|
+
missingKeys = this._missingKeys.asReadonly();
|
|
772
|
+
clearMissingKeys() {
|
|
773
|
+
this._reportedMissingKeys.clear();
|
|
774
|
+
this._pendingMissingKeys.length = 0;
|
|
775
|
+
this._pendingMissingEvents.length = 0;
|
|
776
|
+
this._missingKeys.set([]);
|
|
777
|
+
}
|
|
778
|
+
clearMissingParams() {
|
|
779
|
+
this._reportedMissingParams.clear();
|
|
780
|
+
this._pendingMissingParams.length = 0;
|
|
781
|
+
}
|
|
354
782
|
constructor() {
|
|
355
783
|
if (this._config.languages.length === 0) throw new Error("[rivinia/i18n] At least one language must be provided in the languages array.");
|
|
356
784
|
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.`);
|
|
357
|
-
|
|
358
|
-
const id = this._currentLanguageId();
|
|
359
|
-
this._writeStored(id);
|
|
360
|
-
});
|
|
785
|
+
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.`);
|
|
361
786
|
if (this._autoApplyDirection) effect(() => {
|
|
362
787
|
const lang = this.currentLanguageObject();
|
|
363
788
|
const root = this._doc.documentElement;
|
|
364
789
|
if (!root) return;
|
|
365
790
|
root.dir = lang.dir;
|
|
366
|
-
root.lang = lang.id;
|
|
791
|
+
root.lang = lang.locale ?? lang.id;
|
|
367
792
|
});
|
|
368
793
|
}
|
|
369
794
|
getLanguage(id) {
|
|
@@ -375,6 +800,9 @@ var RiviniaI18n = class RiviniaI18n {
|
|
|
375
800
|
const done = this._tasks?.add();
|
|
376
801
|
try {
|
|
377
802
|
const current = this._currentLanguageId();
|
|
803
|
+
const fallback = this._fallbackId;
|
|
804
|
+
const hasFallback = fallback !== null && fallback !== current;
|
|
805
|
+
const preload = this._config.preload ?? true;
|
|
378
806
|
const transferred = this.readTransfer(current);
|
|
379
807
|
if (transferred) {
|
|
380
808
|
this._translationsCache.set(current, transferred);
|
|
@@ -382,8 +810,22 @@ var RiviniaI18n = class RiviniaI18n {
|
|
|
382
810
|
} else {
|
|
383
811
|
const inline = this._config.translations?.[current];
|
|
384
812
|
if (inline) this._loadedTranslations.set(inline);
|
|
385
|
-
if (this._config.preload ?? true) await this._loadTranslations(current);
|
|
386
813
|
}
|
|
814
|
+
let fallbackTransferred = null;
|
|
815
|
+
if (hasFallback) {
|
|
816
|
+
fallbackTransferred = this.readTransfer(fallback);
|
|
817
|
+
if (fallbackTransferred) {
|
|
818
|
+
this._translationsCache.set(fallback, fallbackTransferred);
|
|
819
|
+
this._fallbackDict.set(fallbackTransferred);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
const loads = [];
|
|
823
|
+
if (!transferred && preload) loads.push(this._loadTranslations(current));
|
|
824
|
+
if (hasFallback && !fallbackTransferred && preload) loads.push(this._loadTranslations(fallback, {
|
|
825
|
+
silent: true,
|
|
826
|
+
background: true
|
|
827
|
+
}));
|
|
828
|
+
await Promise.all(loads);
|
|
387
829
|
} finally {
|
|
388
830
|
this._ready.set(true);
|
|
389
831
|
done?.();
|
|
@@ -392,21 +834,61 @@ var RiviniaI18n = class RiviniaI18n {
|
|
|
392
834
|
translate(keyOrTranslations, defaultValueOrParams, maybeParams) {
|
|
393
835
|
if (typeof keyOrTranslations === "string") {
|
|
394
836
|
const { defaultValue, params } = normalizeKeyArgs(defaultValueOrParams, maybeParams);
|
|
395
|
-
|
|
396
|
-
const resolved = this._resolveKey(loaded, keyOrTranslations);
|
|
397
|
-
if (resolved !== void 0) return this.render(resolved, params);
|
|
398
|
-
const fallback = defaultValue ?? keyOrTranslations;
|
|
399
|
-
return params ? this.render(fallback, params) : fallback;
|
|
837
|
+
return this._resolveKeyText(keyOrTranslations, defaultValue, params);
|
|
400
838
|
}
|
|
401
|
-
|
|
839
|
+
const currentId = this._currentLanguageId();
|
|
840
|
+
const fallbackId = this._fallbackId;
|
|
841
|
+
return keyOrTranslations[currentId] ?? (fallbackId !== null ? keyOrTranslations[fallbackId] : void 0) ?? Object.values(keyOrTranslations)[0] ?? "";
|
|
842
|
+
}
|
|
843
|
+
t(key, defaultValueOrParams, maybeParams) {
|
|
844
|
+
const defaultValue = typeof defaultValueOrParams === "string" ? defaultValueOrParams : void 0;
|
|
845
|
+
const paramsSource = typeof defaultValueOrParams === "string" ? maybeParams : defaultValueOrParams;
|
|
846
|
+
return computed(() => {
|
|
847
|
+
const params = typeof paramsSource === "function" ? paramsSource() : paramsSource;
|
|
848
|
+
return this._resolveKeyText(key, defaultValue, params);
|
|
849
|
+
});
|
|
850
|
+
}
|
|
851
|
+
_resolveKeyText(key, defaultValue, params) {
|
|
852
|
+
const resolved = this._lookupKey(key);
|
|
853
|
+
if (resolved !== void 0) return this.render(resolved.text, params, resolved.locale, key);
|
|
854
|
+
if (defaultValue === void 0) untracked(() => this._reportMissingKey(key));
|
|
855
|
+
const fallback = defaultValue ?? key;
|
|
856
|
+
return params ? this.render(fallback, params, this.locale(), key) : fallback;
|
|
402
857
|
}
|
|
403
858
|
loadedTranslations = this._loadedTranslations.asReadonly();
|
|
404
859
|
async loadTranslations(languageId, opts) {
|
|
405
860
|
return (await this._loadTranslations(languageId, opts)).data;
|
|
406
861
|
}
|
|
862
|
+
reload(languageId) {
|
|
863
|
+
if (languageId !== void 0) {
|
|
864
|
+
if (!this._languages().some((l) => l.id === languageId)) return Promise.resolve(false);
|
|
865
|
+
this._invalidate(languageId);
|
|
866
|
+
return this._reloadOne(languageId);
|
|
867
|
+
}
|
|
868
|
+
const currentId = this._currentLanguageId();
|
|
869
|
+
const fallbackId = this._fallbackId;
|
|
870
|
+
for (const cachedId of [...this._translationsCache.keys()]) if (cachedId !== currentId && cachedId !== fallbackId) this._translationsCache.delete(cachedId);
|
|
871
|
+
for (const language of this._languages()) this._invalidate(language.id);
|
|
872
|
+
const currentLoad = this._reloadOne(currentId);
|
|
873
|
+
if (fallbackId !== null && fallbackId !== currentId) this._reloadOne(fallbackId);
|
|
874
|
+
return currentLoad;
|
|
875
|
+
}
|
|
876
|
+
_reloadOne(languageId) {
|
|
877
|
+
return this._loadTranslations(languageId, {
|
|
878
|
+
force: true,
|
|
879
|
+
background: languageId !== this._currentLanguageId()
|
|
880
|
+
}).then((result) => result.ok);
|
|
881
|
+
}
|
|
882
|
+
_generationOf(languageId) {
|
|
883
|
+
return this._generations.get(languageId) ?? 0;
|
|
884
|
+
}
|
|
885
|
+
_invalidate(languageId) {
|
|
886
|
+
this._generations.set(languageId, this._generationOf(languageId) + 1);
|
|
887
|
+
}
|
|
407
888
|
toggle() {
|
|
408
889
|
const langs = this._languages();
|
|
409
890
|
if (langs.length < 2) return;
|
|
891
|
+
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.");
|
|
410
892
|
const newId = this._currentLanguageId() === langs[0].id ? langs[1].id : langs[0].id;
|
|
411
893
|
this.applyLanguage(newId);
|
|
412
894
|
}
|
|
@@ -478,61 +960,191 @@ var RiviniaI18n = class RiviniaI18n {
|
|
|
478
960
|
});
|
|
479
961
|
this._viewTransition.run(apply);
|
|
480
962
|
} else apply();
|
|
481
|
-
if (!opts?.skipLoad)
|
|
963
|
+
if (!opts?.skipLoad) {
|
|
964
|
+
this._loadTranslations(id);
|
|
965
|
+
this._ensureFallback();
|
|
966
|
+
}
|
|
967
|
+
this._persistLanguage(id);
|
|
482
968
|
}
|
|
483
|
-
|
|
484
|
-
if (!
|
|
485
|
-
if (
|
|
969
|
+
_persistLanguage(id) {
|
|
970
|
+
if (!this._persist) return;
|
|
971
|
+
if (this._readStored() === id) return;
|
|
972
|
+
this._writeStored(id);
|
|
973
|
+
}
|
|
974
|
+
render(template, params, locale, key) {
|
|
975
|
+
const quoting = template.includes("'");
|
|
976
|
+
if (params === void 0 && !quoting) return template;
|
|
977
|
+
if (!quoting && !template.includes("{")) return template;
|
|
486
978
|
try {
|
|
487
|
-
return formatIcu(template, params, this.
|
|
979
|
+
return formatIcu(template, params ?? {}, locale, { onMissingParam: (name) => untracked(() => this._reportMissingParam(key, name)) });
|
|
488
980
|
} catch {
|
|
489
981
|
return template;
|
|
490
982
|
}
|
|
491
983
|
}
|
|
984
|
+
_lookupKey(key) {
|
|
985
|
+
const currentValue = this._resolveKey(this._loadedTranslations(), key);
|
|
986
|
+
if (currentValue !== void 0) return {
|
|
987
|
+
text: currentValue,
|
|
988
|
+
locale: this.locale()
|
|
989
|
+
};
|
|
990
|
+
const fallbackId = this._fallbackId;
|
|
991
|
+
if (fallbackId === null || fallbackId === this._currentLanguageId()) return void 0;
|
|
992
|
+
const fallbackDict = this._fallbackDict() ?? this._config.translations?.[fallbackId] ?? null;
|
|
993
|
+
if (!fallbackDict) return void 0;
|
|
994
|
+
const fallbackValue = this._resolveKey(fallbackDict, key);
|
|
995
|
+
if (fallbackValue === void 0) return void 0;
|
|
996
|
+
return {
|
|
997
|
+
text: fallbackValue,
|
|
998
|
+
locale: this._localeOf(fallbackId)
|
|
999
|
+
};
|
|
1000
|
+
}
|
|
1001
|
+
_localeOf(languageId) {
|
|
1002
|
+
return this._languages().find((l) => l.id === languageId)?.locale ?? languageId;
|
|
1003
|
+
}
|
|
1004
|
+
_ensureFallback() {
|
|
1005
|
+
const fallbackId = this._fallbackId;
|
|
1006
|
+
if (fallbackId === null || fallbackId === this._currentLanguageId()) return;
|
|
1007
|
+
if (this._hasDictionary(fallbackId)) return;
|
|
1008
|
+
this._loadTranslations(fallbackId, {
|
|
1009
|
+
silent: true,
|
|
1010
|
+
background: true
|
|
1011
|
+
});
|
|
1012
|
+
}
|
|
1013
|
+
_hasDictionary(languageId) {
|
|
1014
|
+
return this._translationsCache.has(languageId) || this._config.translations?.[languageId] !== void 0;
|
|
1015
|
+
}
|
|
1016
|
+
_reportMissingKey(key) {
|
|
1017
|
+
if (!this._hasDictionary(this._currentLanguageId())) return;
|
|
1018
|
+
if (this._reportedMissingKeys.has(key)) return;
|
|
1019
|
+
this._reportedMissingKeys.add(key);
|
|
1020
|
+
const language = this._currentLanguageId();
|
|
1021
|
+
const fallbackLanguage = this._fallbackId;
|
|
1022
|
+
const event = fallbackLanguage === null ? {
|
|
1023
|
+
key,
|
|
1024
|
+
language
|
|
1025
|
+
} : {
|
|
1026
|
+
key,
|
|
1027
|
+
language,
|
|
1028
|
+
fallbackLanguage
|
|
1029
|
+
};
|
|
1030
|
+
this._pendingMissingKeys.push(key);
|
|
1031
|
+
this._pendingMissingEvents.push(event);
|
|
1032
|
+
this._queueMissingFlush();
|
|
1033
|
+
if (this._warnOnMissingKey) console.warn(`[rivinia/i18n] Missing key "${key}" for language "${language}"` + (fallbackLanguage === null ? "." : ` (fallback "${fallbackLanguage}" has no value either).`));
|
|
1034
|
+
}
|
|
1035
|
+
_queueMissingFlush() {
|
|
1036
|
+
if (this._missingFlushQueued) return;
|
|
1037
|
+
this._missingFlushQueued = true;
|
|
1038
|
+
queueMicrotask(() => {
|
|
1039
|
+
this._missingFlushQueued = false;
|
|
1040
|
+
const pending = this._pendingMissingKeys.splice(0);
|
|
1041
|
+
if (pending.length === 0) return;
|
|
1042
|
+
this._missingKeys.update((current) => [...current, ...pending]);
|
|
1043
|
+
const events = this._pendingMissingEvents.splice(0);
|
|
1044
|
+
for (const event of events) this._config.onMissingKey?.(event);
|
|
1045
|
+
});
|
|
1046
|
+
}
|
|
1047
|
+
_reportMissingParam(key, param) {
|
|
1048
|
+
const dedupe = `${key}\u0000${param}`;
|
|
1049
|
+
if (this._reportedMissingParams.has(dedupe)) return;
|
|
1050
|
+
this._reportedMissingParams.add(dedupe);
|
|
1051
|
+
const language = this._currentLanguageId();
|
|
1052
|
+
this._pendingMissingParams.push({
|
|
1053
|
+
key,
|
|
1054
|
+
param,
|
|
1055
|
+
language
|
|
1056
|
+
});
|
|
1057
|
+
this._queueMissingParamsFlush();
|
|
1058
|
+
if (this._warnOnMissingParam) console.warn(`[rivinia/i18n] Missing parameter "${param}" for key "${key}" and language "${language}"; the placeholder was kept.`);
|
|
1059
|
+
}
|
|
1060
|
+
_queueMissingParamsFlush() {
|
|
1061
|
+
if (this._missingParamsFlushQueued) return;
|
|
1062
|
+
this._missingParamsFlushQueued = true;
|
|
1063
|
+
queueMicrotask(() => {
|
|
1064
|
+
this._missingParamsFlushQueued = false;
|
|
1065
|
+
const events = this._pendingMissingParams.splice(0);
|
|
1066
|
+
for (const event of events) this._config.onMissingParam?.(event);
|
|
1067
|
+
});
|
|
1068
|
+
}
|
|
492
1069
|
async _loadTranslations(languageId, opts) {
|
|
493
|
-
const
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
if (
|
|
1070
|
+
const background = opts?.background === true;
|
|
1071
|
+
if (!opts?.force) {
|
|
1072
|
+
const cached = this._translationsCache.get(languageId);
|
|
1073
|
+
if (cached) {
|
|
1074
|
+
this._applyDictionary(languageId, cached);
|
|
1075
|
+
return {
|
|
1076
|
+
data: cached,
|
|
1077
|
+
ok: true
|
|
1078
|
+
};
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
let task = opts?.force ? void 0 : this._inflight.get(languageId);
|
|
1082
|
+
if (!task) {
|
|
1083
|
+
task = this._fetchDictionary(languageId, this._generationOf(languageId));
|
|
1084
|
+
this._inflight.set(languageId, task);
|
|
1085
|
+
}
|
|
1086
|
+
if (!background) {
|
|
1087
|
+
this._loadingCount++;
|
|
1088
|
+
this._loadingState.set("loading");
|
|
1089
|
+
}
|
|
1090
|
+
try {
|
|
1091
|
+
const result = await task;
|
|
1092
|
+
if (result.ok) this._applyDictionary(languageId, result.data);
|
|
1093
|
+
else if (!background && result.error !== void 0 && languageId === this._currentLanguageId()) {
|
|
1094
|
+
this._loadingError.set(result.error);
|
|
1095
|
+
this._loadingState.set("error");
|
|
1096
|
+
}
|
|
497
1097
|
return {
|
|
498
|
-
data:
|
|
499
|
-
ok:
|
|
1098
|
+
data: result.data,
|
|
1099
|
+
ok: result.ok
|
|
500
1100
|
};
|
|
1101
|
+
} finally {
|
|
1102
|
+
if (!background) {
|
|
1103
|
+
this._loadingCount = Math.max(0, this._loadingCount - 1);
|
|
1104
|
+
if (this._loadingCount === 0 && this._loadingState() === "loading") this._loadingState.set("idle");
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
_applyDictionary(languageId, data) {
|
|
1109
|
+
if (languageId === this._currentLanguageId()) {
|
|
1110
|
+
this._loadedTranslations.set(data);
|
|
1111
|
+
this._loadingError.set(null);
|
|
1112
|
+
if (this._loadingCount === 0 && this._loadingState() === "error") this._loadingState.set("idle");
|
|
501
1113
|
}
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
1114
|
+
if (languageId === this._fallbackId) this._fallbackDict.set(data);
|
|
1115
|
+
}
|
|
1116
|
+
_fetchDictionary(languageId, generation) {
|
|
1117
|
+
let task;
|
|
1118
|
+
task = (async () => {
|
|
1119
|
+
const stale = () => this._generationOf(languageId) !== generation;
|
|
506
1120
|
try {
|
|
507
|
-
if (!this._resolvedLoader) this._resolvedLoader = await this._getLoader();
|
|
508
|
-
const translations = await this._resolvedLoader.load(languageId);
|
|
1121
|
+
if (!this._resolvedLoader) this._resolvedLoader = await this._getLoader(this.loaderContext);
|
|
1122
|
+
const translations = await this._resolvedLoader.load(languageId, this.loaderContext);
|
|
1123
|
+
if (stale()) return {
|
|
1124
|
+
data: {},
|
|
1125
|
+
ok: false
|
|
1126
|
+
};
|
|
509
1127
|
this._translationsCache.set(languageId, translations);
|
|
510
1128
|
this.writeTransfer(languageId, translations);
|
|
511
|
-
if (!opts?.silent && current()) {
|
|
512
|
-
this._loadedTranslations.set(translations);
|
|
513
|
-
this._loadingError.set(null);
|
|
514
|
-
this._loadingState.set("idle");
|
|
515
|
-
}
|
|
516
1129
|
return {
|
|
517
1130
|
data: translations,
|
|
518
1131
|
ok: true
|
|
519
1132
|
};
|
|
520
1133
|
} catch (error) {
|
|
521
|
-
if (
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
}
|
|
1134
|
+
if (stale()) return {
|
|
1135
|
+
data: {},
|
|
1136
|
+
ok: false
|
|
1137
|
+
};
|
|
525
1138
|
this._config.onLoadError?.(languageId, error);
|
|
526
1139
|
return {
|
|
527
1140
|
data: {},
|
|
528
|
-
ok: false
|
|
1141
|
+
ok: false,
|
|
1142
|
+
error
|
|
529
1143
|
};
|
|
530
1144
|
} finally {
|
|
531
|
-
this._inflight.delete(languageId);
|
|
532
|
-
if (this._inflight.size === 0 && this._loadingState() === "loading") this._loadingState.set("idle");
|
|
1145
|
+
if (this._inflight.get(languageId) === task) this._inflight.delete(languageId);
|
|
533
1146
|
}
|
|
534
1147
|
})();
|
|
535
|
-
this._inflight.set(languageId, task);
|
|
536
1148
|
return task;
|
|
537
1149
|
}
|
|
538
1150
|
_readStored() {
|
|
@@ -575,7 +1187,7 @@ var RiviniaI18n = class RiviniaI18n {
|
|
|
575
1187
|
this._transfer?.set(this.transferKey(languageId), dict);
|
|
576
1188
|
}
|
|
577
1189
|
_resolveKey(obj, path) {
|
|
578
|
-
const value = path.split(".").reduce((acc, segment) => acc && typeof acc === "object" ? acc[segment] : void 0, obj);
|
|
1190
|
+
const value = Object.prototype.hasOwnProperty.call(obj, path) ? obj[path] : path.split(".").reduce((acc, segment) => acc && typeof acc === "object" ? acc[segment] : void 0, obj);
|
|
579
1191
|
if (typeof value === "string") return value;
|
|
580
1192
|
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
581
1193
|
}
|
|
@@ -590,7 +1202,7 @@ var RiviniaI18n = class RiviniaI18n {
|
|
|
590
1202
|
}
|
|
591
1203
|
static ɵfac = i0.ɵɵngDeclareFactory({
|
|
592
1204
|
minVersion: "12.0.0",
|
|
593
|
-
version: "22.2.
|
|
1205
|
+
version: "22.2.3",
|
|
594
1206
|
ngImport: i0,
|
|
595
1207
|
type: RiviniaI18n,
|
|
596
1208
|
deps: [],
|
|
@@ -598,14 +1210,14 @@ var RiviniaI18n = class RiviniaI18n {
|
|
|
598
1210
|
});
|
|
599
1211
|
static ɵprov = i0.ɵɵngDeclareService({
|
|
600
1212
|
minVersion: "22.0.0",
|
|
601
|
-
version: "22.2.
|
|
1213
|
+
version: "22.2.3",
|
|
602
1214
|
ngImport: i0,
|
|
603
1215
|
type: RiviniaI18n
|
|
604
1216
|
});
|
|
605
1217
|
};
|
|
606
1218
|
i0.ɵɵngDeclareClassMetadata({
|
|
607
1219
|
minVersion: "12.0.0",
|
|
608
|
-
version: "22.2.
|
|
1220
|
+
version: "22.2.3",
|
|
609
1221
|
ngImport: i0,
|
|
610
1222
|
type: RiviniaI18n,
|
|
611
1223
|
decorators: [{ type: Service }],
|
|
@@ -643,7 +1255,7 @@ var RTranslatePipe = class RTranslatePipe {
|
|
|
643
1255
|
}
|
|
644
1256
|
static ɵfac = i0.ɵɵngDeclareFactory({
|
|
645
1257
|
minVersion: "12.0.0",
|
|
646
|
-
version: "22.2.
|
|
1258
|
+
version: "22.2.3",
|
|
647
1259
|
ngImport: i0,
|
|
648
1260
|
type: RTranslatePipe,
|
|
649
1261
|
deps: [],
|
|
@@ -651,7 +1263,7 @@ var RTranslatePipe = class RTranslatePipe {
|
|
|
651
1263
|
});
|
|
652
1264
|
static ɵpipe = i0.ɵɵngDeclarePipe({
|
|
653
1265
|
minVersion: "14.0.0",
|
|
654
|
-
version: "22.2.
|
|
1266
|
+
version: "22.2.3",
|
|
655
1267
|
ngImport: i0,
|
|
656
1268
|
type: RTranslatePipe,
|
|
657
1269
|
isStandalone: true,
|
|
@@ -661,7 +1273,7 @@ var RTranslatePipe = class RTranslatePipe {
|
|
|
661
1273
|
};
|
|
662
1274
|
i0.ɵɵngDeclareClassMetadata({
|
|
663
1275
|
minVersion: "12.0.0",
|
|
664
|
-
version: "22.2.
|
|
1276
|
+
version: "22.2.3",
|
|
665
1277
|
ngImport: i0,
|
|
666
1278
|
type: RTranslatePipe,
|
|
667
1279
|
decorators: [{
|
|
@@ -672,6 +1284,6 @@ i0.ɵɵngDeclareClassMetadata({
|
|
|
672
1284
|
}]
|
|
673
1285
|
}]
|
|
674
1286
|
});
|
|
675
|
-
export { RTranslatePipe, RiviniaI18n, ViewTransitionService, browserLanguages, customLoader, defineRiviniaI18n, detectBrowserLanguage, formatIcu, httpLoader, injectRiviniaI18n, jsonLoader, parseCookie, provideRiviniaI18n, remoteLoader, serializeCookie };
|
|
1287
|
+
export { RTranslatePipe, RiviniaI18n, RiviniaLoadError, ViewTransitionService, browserLanguages, customLoader, defineRiviniaI18n, detectBrowserLanguage, formatIcu, httpLoader, injectRiviniaI18n, jsonLoader, parseCookie, provideRiviniaI18n, remoteLoader, serializeCookie };
|
|
676
1288
|
|
|
677
1289
|
//# sourceMappingURL=rivinia-i18n.mjs.map
|