@manyducks.co/dolla 2.0.0-alpha.60 → 2.0.0-alpha.62
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/dist/core/context.d.ts +23 -6
- package/dist/core/context.test.d.ts +1 -0
- package/dist/core/index.d.ts +6 -5
- package/dist/core/logger.test.d.ts +0 -0
- package/dist/core/markup.d.ts +12 -39
- package/dist/core/markup.test.d.ts +0 -0
- package/dist/core/mount.test.d.ts +0 -0
- package/dist/core/nodes/_markup.d.ts +36 -0
- package/dist/core/nodes/dom.d.ts +5 -6
- package/dist/core/nodes/dynamic.d.ts +6 -13
- package/dist/core/nodes/{html.d.ts → element.d.ts} +9 -8
- package/dist/core/nodes/portal.d.ts +6 -7
- package/dist/core/nodes/repeat.d.ts +8 -6
- package/dist/core/nodes/view.d.ts +6 -5
- package/dist/core/ref.test.d.ts +1 -0
- package/dist/core/signals.d.ts +17 -2
- package/dist/core/views/default-crash-view.d.ts +5 -2
- package/dist/core/views/for.d.ts +23 -0
- package/dist/core/views/show.d.ts +26 -0
- package/dist/hooks/index.d.ts +64 -0
- package/dist/hooks/index.test.d.ts +1 -0
- package/dist/hooks.js +69 -0
- package/dist/hooks.js.map +1 -0
- package/dist/i18n.js +49 -49
- package/dist/i18n.js.map +1 -1
- package/dist/index.js +61 -58
- package/dist/index.js.map +1 -1
- package/dist/jsx-dev-runtime.js +1 -1
- package/dist/jsx-runtime.js +1 -1
- package/dist/logger-Bl496yfY.js +91 -0
- package/dist/logger-Bl496yfY.js.map +1 -0
- package/dist/markup-UzKSsawX.js +1037 -0
- package/dist/markup-UzKSsawX.js.map +1 -0
- package/dist/ref-BD79iqlg.js +15 -0
- package/dist/ref-BD79iqlg.js.map +1 -0
- package/dist/router/router.d.ts +33 -18
- package/dist/{router-BpuJQ6OA.js → router-BXBX8lnO.js} +217 -190
- package/dist/router-BXBX8lnO.js.map +1 -0
- package/dist/router.js +1 -1
- package/dist/signals-gCwiIe5X.js +450 -0
- package/dist/signals-gCwiIe5X.js.map +1 -0
- package/dist/types.d.ts +1 -1
- package/docs/hooks.md +211 -0
- package/docs/i18n.md +1 -1
- package/notes/scratch.md +23 -1
- package/package.json +5 -1
- package/vite.config.js +1 -0
- package/dist/core/symbols.d.ts +0 -2
- package/dist/logger-MPwl-Xqu.js +0 -524
- package/dist/logger-MPwl-Xqu.js.map +0 -1
- package/dist/markup-BGlfQYQk.js +0 -996
- package/dist/markup-BGlfQYQk.js.map +0 -1
- package/dist/router-BpuJQ6OA.js.map +0 -1
package/dist/i18n.js
CHANGED
|
@@ -4,8 +4,9 @@ var D = (o) => {
|
|
|
4
4
|
};
|
|
5
5
|
var J = (o, t, e) => t in o ? B(o, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[t] = e;
|
|
6
6
|
var O = (o, t, e) => J(o, typeof t != "symbol" ? t + "" : t, e), _ = (o, t, e) => t.has(o) || D("Cannot " + e);
|
|
7
|
-
var
|
|
8
|
-
import {
|
|
7
|
+
var r = (o, t, e) => (_(o, t, "read from private field"), e ? e.call(o) : t.get(o)), m = (o, t, e) => t.has(o) ? D("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(o) : t.set(o, e), N = (o, t, e, n) => (_(o, t, "write to private field"), n ? n.call(o, e) : t.set(o, e), e), g = (o, t, e) => (_(o, t, "access private method"), e);
|
|
8
|
+
import { c as K } from "./logger-Bl496yfY.js";
|
|
9
|
+
import { $, d as Q, g as x } from "./signals-gCwiIe5X.js";
|
|
9
10
|
import { b as C, i as j, c as U, t as P } from "./typeChecking-CbltMOUt.js";
|
|
10
11
|
var I, F, p, S, G;
|
|
11
12
|
class W {
|
|
@@ -18,7 +19,7 @@ class W {
|
|
|
18
19
|
}
|
|
19
20
|
async load() {
|
|
20
21
|
let t;
|
|
21
|
-
if (!
|
|
22
|
+
if (!r(this, I)) {
|
|
22
23
|
if (j(this.config.strings))
|
|
23
24
|
t = this.config.strings;
|
|
24
25
|
else if (U(this.config.fetch)) {
|
|
@@ -41,17 +42,17 @@ class W {
|
|
|
41
42
|
if (t) {
|
|
42
43
|
const e = g(this, p, S).call(this, t);
|
|
43
44
|
for (const n of e)
|
|
44
|
-
|
|
45
|
+
r(this, F).set(n[0], n[1]);
|
|
45
46
|
} else
|
|
46
47
|
throw new Error("Language could not be loaded.");
|
|
47
48
|
}
|
|
48
49
|
getTemplate(t) {
|
|
49
|
-
return
|
|
50
|
+
return r(this, F).get(t) ?? {
|
|
50
51
|
segments: [{ type: 0, text: `[MISSING: ${t}]` }]
|
|
51
52
|
};
|
|
52
53
|
}
|
|
53
54
|
hasTemplate(t) {
|
|
54
|
-
return
|
|
55
|
+
return r(this, F).has(t);
|
|
55
56
|
}
|
|
56
57
|
}
|
|
57
58
|
I = new WeakMap(), F = new WeakMap(), p = new WeakSet(), S = function(t, e = []) {
|
|
@@ -78,7 +79,7 @@ I = new WeakMap(), F = new WeakMap(), p = new WeakSet(), S = function(t, e = [])
|
|
|
78
79
|
const n = {
|
|
79
80
|
segments: []
|
|
80
81
|
};
|
|
81
|
-
let a = "", s = 0,
|
|
82
|
+
let a = "", s = 0, i = 0, c, w, l;
|
|
82
83
|
const k = () => {
|
|
83
84
|
c = {
|
|
84
85
|
type: 1,
|
|
@@ -92,32 +93,32 @@ I = new WeakMap(), F = new WeakMap(), p = new WeakSet(), S = function(t, e = [])
|
|
|
92
93
|
};
|
|
93
94
|
};
|
|
94
95
|
for (; s < t.length; ) {
|
|
95
|
-
if (
|
|
96
|
+
if (i !== 0 && t[s] === " ") {
|
|
96
97
|
s++;
|
|
97
98
|
continue;
|
|
98
99
|
}
|
|
99
|
-
switch (
|
|
100
|
+
switch (i) {
|
|
100
101
|
case 0:
|
|
101
|
-
t[s] === "{" && t[s + 1] === "{" ? (
|
|
102
|
+
t[s] === "{" && t[s + 1] === "{" ? (i = 1, s += 2, a.length > 0 && (n.segments.push({ type: 0, text: a }), a = ""), k()) : (a += t[s], s++);
|
|
102
103
|
break;
|
|
103
104
|
case 1:
|
|
104
|
-
t[s] === "|" ? (
|
|
105
|
+
t[s] === "|" ? (i = 2, s += 1, c.name = a, a = "", y()) : t[s] === "}" && t[s + 1] === "}" ? (i = 0, s += 2, c.name = a, a = "", n.segments.push(c)) : (a += t[s], s++);
|
|
105
106
|
break;
|
|
106
107
|
case 2:
|
|
107
|
-
t[s] === "(" ? (
|
|
108
|
+
t[s] === "(" ? (i = 3, s += 1, w.name = a, a = "") : t[s] === "}" && t[s + 1] === "}" ? (i = 0, s += 2, c.formats.push(w), n.segments.push(c)) : (a += t[s], s++);
|
|
108
109
|
break;
|
|
109
110
|
case 3:
|
|
110
|
-
t[s] === ")" || (t[s] === ":" ? (
|
|
111
|
+
t[s] === ")" || (t[s] === ":" ? (i = 4, s += 1, l = a, a = "") : t[s] === "}" && t[s + 1] === "}" || (a += t[s], s++));
|
|
111
112
|
break;
|
|
112
113
|
case 4:
|
|
113
|
-
t[s] === ")" ? (
|
|
114
|
+
t[s] === ")" ? (i = 5, s += 1, w.options[l] = a, a = "", c.formats.push(w)) : t[s] === "," ? (i = 3, s += 1, w.options[l] = a, a = "") : t[s] === "}" && t[s + 1] === "}" || (a += t[s], s++);
|
|
114
115
|
break;
|
|
115
116
|
case 5:
|
|
116
|
-
t[s] === "|" ? (
|
|
117
|
+
t[s] === "|" ? (i = 2, s += 1, y()) : t[s] === "}" && t[s + 1] === "}" && (i = 0, s += 2, n.segments.push(c));
|
|
117
118
|
break;
|
|
118
119
|
}
|
|
119
120
|
}
|
|
120
|
-
return
|
|
121
|
+
return i === 0 && a.length > 0 && n.segments.push({ type: 0, text: a }), n;
|
|
121
122
|
};
|
|
122
123
|
var b, h, E, T, v, d, u, H, L, V, M, A;
|
|
123
124
|
class X {
|
|
@@ -129,23 +130,23 @@ class X {
|
|
|
129
130
|
m(this, T, /* @__PURE__ */ new Map());
|
|
130
131
|
m(this, v, "auto");
|
|
131
132
|
m(this, d, $("en"));
|
|
132
|
-
O(this, "$locale", $(
|
|
133
|
+
O(this, "$locale", $(r(this, d)));
|
|
133
134
|
N(this, b, K("dolla.i18n")), this.addFormat("number", (t, e, n) => g(this, u, L).call(this, Number(e), n)), this.addFormat("datetime", (t, e, n) => g(this, u, V).call(this, e, n)), this.addFormat("list", (t, e, n) => g(this, u, M).call(this, e, n));
|
|
134
135
|
}
|
|
135
136
|
get locales() {
|
|
136
|
-
return [...
|
|
137
|
+
return [...r(this, h).keys()];
|
|
137
138
|
}
|
|
138
139
|
async setup(t) {
|
|
139
140
|
if (t.translations.forEach((e) => {
|
|
140
|
-
|
|
141
|
+
r(this, h).set(e.locale, new W(e));
|
|
141
142
|
}), t.locale && t.locale !== "auto") {
|
|
142
143
|
if (!t.translations.some((n) => n.locale === t.locale))
|
|
143
144
|
throw new Error(`Initial locale '${t.locale}' is not registered in the locales array.`);
|
|
144
145
|
N(this, v, t.locale);
|
|
145
146
|
}
|
|
146
|
-
|
|
147
|
-
`${
|
|
148
|
-
),
|
|
147
|
+
r(this, b).info(
|
|
148
|
+
`${r(this, h).size} language${r(this, h).size === 1 ? "" : "s"} supported: '${[...r(this, h).keys()].join("', '")}'`
|
|
149
|
+
), r(this, h).size > 0 && await this.setLocale(r(this, v));
|
|
149
150
|
}
|
|
150
151
|
async setLocale(t) {
|
|
151
152
|
var a;
|
|
@@ -153,24 +154,24 @@ class X {
|
|
|
153
154
|
if (t === "auto") {
|
|
154
155
|
let s = [];
|
|
155
156
|
if (typeof navigator < "u") {
|
|
156
|
-
const
|
|
157
|
-
((a =
|
|
157
|
+
const i = navigator;
|
|
158
|
+
((a = i.languages) == null ? void 0 : a.length) > 0 ? s.push(...i.languages) : i.language ? s.push(i.language) : i.browserLanguage ? s.push(i.browserLanguage) : i.userLanguage && s.push(i.userLanguage);
|
|
158
159
|
}
|
|
159
|
-
for (const
|
|
160
|
-
|
|
160
|
+
for (const i of s)
|
|
161
|
+
r(this, h).has(i) && (e = i);
|
|
161
162
|
} else
|
|
162
|
-
|
|
163
|
+
r(this, h).has(t) && (e = t);
|
|
163
164
|
if (e == null) {
|
|
164
|
-
const s =
|
|
165
|
+
const s = r(this, h).keys().next().value;
|
|
165
166
|
s && (e = s);
|
|
166
167
|
}
|
|
167
|
-
if (!e || !
|
|
168
|
+
if (!e || !r(this, h).has(e))
|
|
168
169
|
throw new Error(`Locale '${t}' has no translation.`);
|
|
169
|
-
const n =
|
|
170
|
+
const n = r(this, h).get(e);
|
|
170
171
|
try {
|
|
171
|
-
await n.load(), N(this, E, []),
|
|
172
|
+
await n.load(), N(this, E, []), r(this, d).call(this, e), r(this, b).info("set language to " + e);
|
|
172
173
|
} catch (s) {
|
|
173
|
-
s instanceof Error &&
|
|
174
|
+
s instanceof Error && r(this, b).crash(s);
|
|
174
175
|
}
|
|
175
176
|
}
|
|
176
177
|
/**
|
|
@@ -191,7 +192,7 @@ class X {
|
|
|
191
192
|
const n = {};
|
|
192
193
|
for (const a in e)
|
|
193
194
|
n[a] = x(e[a]);
|
|
194
|
-
return g(this, u, H).call(this,
|
|
195
|
+
return g(this, u, H).call(this, r(this, d).call(this), t, n);
|
|
195
196
|
});
|
|
196
197
|
}
|
|
197
198
|
/**
|
|
@@ -209,7 +210,7 @@ class X {
|
|
|
209
210
|
* t("greeting", {name: "world"}); // State<"Hello, WORLD!">
|
|
210
211
|
*/
|
|
211
212
|
addFormat(t, e) {
|
|
212
|
-
|
|
213
|
+
r(this, T).set(t, e);
|
|
213
214
|
}
|
|
214
215
|
/**
|
|
215
216
|
* Creates an `Intl.Collator` configured for the current locale.
|
|
@@ -218,7 +219,7 @@ class X {
|
|
|
218
219
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator#options
|
|
219
220
|
*/
|
|
220
221
|
collator(t) {
|
|
221
|
-
return new Intl.Collator(
|
|
222
|
+
return new Intl.Collator(r(this, d).call(this), t);
|
|
222
223
|
}
|
|
223
224
|
/**
|
|
224
225
|
* Formats a number for the current locale. Uses `Intl.NumberFormat` under the hood.
|
|
@@ -257,7 +258,7 @@ b = new WeakMap(), h = new WeakMap(), E = new WeakMap(), T = new WeakMap(), v =
|
|
|
257
258
|
var w;
|
|
258
259
|
const a = g(this, u, A).call(this, e, n);
|
|
259
260
|
if (a) return a;
|
|
260
|
-
const s =
|
|
261
|
+
const s = r(this, h).get(t);
|
|
261
262
|
if (n.context != null && (e += "_" + n.context), n.count != null)
|
|
262
263
|
if (n.ordinal) {
|
|
263
264
|
const l = `${e}_ordinal_(=${n.count})`;
|
|
@@ -266,9 +267,9 @@ b = new WeakMap(), h = new WeakMap(), E = new WeakMap(), T = new WeakMap(), v =
|
|
|
266
267
|
const l = `${e}_(=${n.count})`;
|
|
267
268
|
s.hasTemplate(l) ? e = l : e += "_" + new Intl.PluralRules(t).select(n.count);
|
|
268
269
|
}
|
|
269
|
-
const
|
|
270
|
+
const i = s.getTemplate(e);
|
|
270
271
|
let c = "";
|
|
271
|
-
for (const l of
|
|
272
|
+
for (const l of i.segments)
|
|
272
273
|
if (l.type === 0)
|
|
273
274
|
c += l.text;
|
|
274
275
|
else if (l.type === 1) {
|
|
@@ -276,12 +277,12 @@ b = new WeakMap(), h = new WeakMap(), E = new WeakMap(), T = new WeakMap(), v =
|
|
|
276
277
|
const y = ((w = n.formatOverrides) == null ? void 0 : w[l.name]) ?? [...l.formats];
|
|
277
278
|
l.name === "count" && y.length === 0 && y.push({ name: "number", options: {} });
|
|
278
279
|
for (const f of y) {
|
|
279
|
-
const R =
|
|
280
|
+
const R = r(this, T).get(f.name);
|
|
280
281
|
if (R == null) {
|
|
281
282
|
const z = new Error(
|
|
282
|
-
`Failed to load format '${f.name}' when processing '${e}', template: ${
|
|
283
|
+
`Failed to load format '${f.name}' when processing '${e}', template: ${i}`
|
|
283
284
|
);
|
|
284
|
-
throw
|
|
285
|
+
throw r(this, b).crash(z), z;
|
|
285
286
|
}
|
|
286
287
|
k = R(t, k, f.options);
|
|
287
288
|
}
|
|
@@ -289,15 +290,14 @@ b = new WeakMap(), h = new WeakMap(), E = new WeakMap(), T = new WeakMap(), v =
|
|
|
289
290
|
}
|
|
290
291
|
return c;
|
|
291
292
|
}, L = function(t, e) {
|
|
292
|
-
return new Intl.NumberFormat(
|
|
293
|
+
return new Intl.NumberFormat(r(this, d).call(this), e).format(t);
|
|
293
294
|
}, V = function(t, e) {
|
|
294
|
-
return new Intl.DateTimeFormat(
|
|
295
|
+
return new Intl.DateTimeFormat(r(this, d).call(this), e).format(C(t) ? new Date(t) : t);
|
|
295
296
|
}, M = function(t, e) {
|
|
296
|
-
return new Intl.ListFormat(
|
|
297
|
-
}, // relativeTime():
|
|
298
|
-
// }
|
|
297
|
+
return new Intl.ListFormat(r(this, d).call(this), e).format(t);
|
|
298
|
+
}, // relativeTime(date?: MaybeSignal<string | number | Date | undefined>): Signal<string> {}
|
|
299
299
|
A = function(t, e) {
|
|
300
|
-
for (const n of
|
|
300
|
+
for (const n of r(this, E))
|
|
301
301
|
if (n[0] === t && Q(n[1], e))
|
|
302
302
|
return n[2];
|
|
303
303
|
};
|
|
@@ -310,9 +310,9 @@ function Y(o, t) {
|
|
|
310
310
|
}
|
|
311
311
|
return n;
|
|
312
312
|
}
|
|
313
|
-
const q = new X(),
|
|
313
|
+
const q = new X(), nt = q.t.bind(q);
|
|
314
314
|
export {
|
|
315
315
|
q as i18n,
|
|
316
|
-
|
|
316
|
+
nt as t
|
|
317
317
|
};
|
|
318
318
|
//# sourceMappingURL=i18n.js.map
|
package/dist/i18n.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"i18n.js","sources":["../src/i18n/index.ts"],"sourcesContent":["import { createLogger, type Logger } from \"../core/logger.js\";\nimport { $, get, type MaybeSignal, type Signal } from \"../core/signals.js\";\nimport { isFunction, isObject, isString, typeOf } from \"../typeChecking.js\";\nimport { deepEqual } from \"../utils.js\";\n\n// ----- Types ----- //\n\n/**\n * A JSON object of translated strings. Values can be string templates or nested objects.\n */\ninterface LocalizedStrings extends Record<string, string | LocalizedStrings> {}\n\nenum SegmentType {\n Static,\n Variable,\n}\ntype StringTemplate = { segments: (StaticSegment | VariableSegment)[] };\n/**\n * A string segment with literal text to be appended without processing.\n */\ntype StaticSegment = {\n type: SegmentType.Static;\n text: string;\n};\n/**\n * A variable passed to the t() function. Needs to be formatted before it is appended.\n */\ntype VariableSegment = {\n type: SegmentType.Variable;\n name: string;\n formats: Format[];\n};\n/**\n * A formatter to be applied to a variable.\n */\ntype Format = {\n name: string;\n options: Record<string, any>;\n};\n\nexport interface TranslationConfig {\n /**\n * Name of the locale this translation is for (BCP 47 locale names recommended).\n */\n locale: string;\n\n /**\n * An object with translated strings for this language.\n */\n strings?: LocalizedStrings;\n\n /**\n * A callback function that returns a Promise that resolves to the translation object for this language.\n */\n fetch?: () => Promise<LocalizedStrings>;\n\n /**\n * Path to a JSON file with translated strings for this language.\n */\n path?: string;\n}\n\nexport type I18nSetupOptions = {\n /**\n * Default locale to load on startup\n */\n locale?: string | null;\n\n translations: TranslationConfig[];\n};\n\nexport type TOptions = {\n /**\n *\n */\n count?: MaybeSignal<number>;\n\n /**\n *\n */\n context?: MaybeSignal<string>;\n\n /**\n * Override formats specified in the template with the ones in the array for each named variable.\n *\n * @example\n * t(\"example_key\", {\n * count: 5,\n * formatOverrides: {\n * count: [ { name: \"datetime\", options: { style: \"currency\", currency: \"JPY\" } } ]\n * }\n * });\n */\n formatOverrides?: MaybeSignal<Record<string, Record<string, Format[]>>>;\n\n [value: string]: MaybeSignal<any>;\n};\n\nexport type Formatter = (locale: string, value: unknown, options: Record<string, any>) => string;\n\n// ----- Code ----- //\n\nclass Translation {\n config: TranslationConfig;\n\n #isLoaded = false;\n\n #templates = new Map<string, StringTemplate>();\n\n constructor(config: TranslationConfig) {\n this.config = config;\n }\n\n async load(): Promise<void> {\n let strings: LocalizedStrings | undefined;\n\n if (!this.#isLoaded) {\n if (isObject(this.config.strings)) {\n strings = this.config.strings;\n } else if (isFunction(this.config.fetch)) {\n strings = await this.config.fetch();\n if (!isObject(strings)) {\n throw new Error(`Fetch function did not return an object of language strings: ${strings}`);\n }\n } else if (isString(this.config.path)) {\n const res = await fetch(this.config.path);\n if (res.ok) {\n const body = await res.json();\n if (isObject(body)) {\n strings = body as LocalizedStrings;\n } else {\n throw new Error(\n `Language path '${this.config.path}' did not return an object of language strings: ${body}`,\n );\n }\n } else {\n throw new Error(`HTTP request failed.`);\n }\n }\n }\n\n if (strings) {\n const entries = this.#compile(strings);\n for (const entry of entries) {\n this.#templates.set(entry[0], entry[1]);\n }\n } else {\n throw new Error(`Language could not be loaded.`);\n }\n }\n\n getTemplate(selector: string): StringTemplate {\n return (\n this.#templates.get(selector) ?? {\n segments: [{ type: SegmentType.Static, text: `[MISSING: ${selector}]` }],\n }\n );\n }\n\n hasTemplate(selector: string): boolean {\n return this.#templates.has(selector);\n }\n\n #compile(strings: { [key: string]: any }, path: string[] = []): [string, StringTemplate][] {\n const entries: [string, StringTemplate][] = [];\n\n for (const key in strings) {\n switch (typeOf(strings[key])) {\n case \"string\":\n entries.push([[...path, key].join(\".\"), this.#parseTemplate(strings[key])]);\n break;\n case \"object\":\n entries.push(...this.#compile(strings[key], [...path, key]));\n break;\n default:\n throw new Error(\n `Expected to find a string or object at ${[...path, key].join(\".\")}. Got: ${typeOf(strings[key])}`,\n );\n }\n }\n\n return entries;\n }\n\n #parseTemplate(template: string): StringTemplate {\n // \"{{itemName}} costs {{amount | number(style: currency, currency: USD)}}.\"\n\n enum Loc {\n /**\n * Outside value braces.\n */\n Static,\n /**\n * Inside value braces; currently parsing the name of the value. e.g. `{{ [name] | number(style: currency, currency: USD) }}`\n */\n ValueName,\n /**\n * Inside value braces; currently parsing the name of a format function. e.g. `{{ name | [number](style: currency, currency: USD) }}`\n */\n FormatName,\n /**\n * Inside value braces; currently parsing the name of a format option. e.g. `{{ name | number([style]: currency, currency: USD) }}`\n */\n FormatOptionName,\n /**\n * Inside value braces; currently parsing the value of a format option. e.g. `{{ name | number(style: [currency], currency: USD ) }}`\n */\n FormatOptionValue,\n /**\n * Inside value braces; just reached the closing bracket of a format option. e.g. `{{ name | number(style: [currency], currency: USD) [] }}`\n */\n FormatOptionEnd,\n }\n\n const parsed: StringTemplate = {\n segments: [],\n };\n\n let buffer = \"\";\n let i = 0;\n let loc: Loc = Loc.Static;\n let segment!: VariableSegment;\n let format!: VariableSegment[\"formats\"][0];\n\n let formatOptionName!: string;\n\n const startSegment = () => {\n segment = {\n type: SegmentType.Variable,\n name: \"\",\n formats: [],\n };\n };\n\n const startFormat = () => {\n format = {\n name: \"\",\n options: {},\n };\n };\n\n while (i < template.length) {\n // Skip spaces (unless we're in static)\n if (loc !== Loc.Static && template[i] === \" \") {\n i++;\n continue;\n }\n\n switch (loc) {\n case Loc.Static:\n if (template[i] === \"{\" && template[i + 1] === \"{\") {\n loc = Loc.ValueName;\n i += 2;\n // close static segment\n if (buffer.length > 0) {\n parsed.segments.push({ type: SegmentType.Static, text: buffer });\n buffer = \"\";\n }\n startSegment();\n } else {\n buffer += template[i];\n i++;\n }\n break;\n case Loc.ValueName:\n if (template[i] === \"|\") {\n loc = Loc.FormatName;\n i += 1;\n // add name to value segment\n segment.name = buffer;\n buffer = \"\";\n startFormat();\n } else if (template[i] === \"}\" && template[i + 1] === \"}\") {\n loc = Loc.Static;\n i += 2;\n // close value segment\n segment.name = buffer;\n buffer = \"\";\n parsed.segments.push(segment);\n } else {\n buffer += template[i];\n i++;\n }\n break;\n case Loc.FormatName:\n if (template[i] === \"(\") {\n loc = Loc.FormatOptionName;\n i += 1;\n // add name to format object\n format.name = buffer;\n buffer = \"\";\n } else if (template[i] === \"}\" && template[i + 1] === \"}\") {\n loc = Loc.Static;\n i += 2;\n // close format and value segment\n segment.formats.push(format);\n parsed.segments.push(segment);\n } else {\n buffer += template[i];\n i++;\n }\n break;\n case Loc.FormatOptionName:\n if (template[i] === \")\") {\n // TODO: error - no value provided for option\n } else if (template[i] === \":\") {\n loc = Loc.FormatOptionValue;\n i += 1;\n // add name to format option object\n formatOptionName = buffer;\n buffer = \"\";\n } else if (template[i] === \"}\" && template[i + 1] === \"}\") {\n // TODO: error - format options parenthesis not closed\n } else {\n buffer += template[i];\n i++;\n }\n break;\n case Loc.FormatOptionValue:\n if (template[i] === \")\") {\n loc = Loc.FormatOptionEnd;\n i += 1;\n // add value to format option object\n // add format option to format object; we're done with this format\n format.options[formatOptionName] = buffer;\n buffer = \"\";\n segment.formats.push(format);\n } else if (template[i] === \",\") {\n loc = Loc.FormatOptionName;\n i += 1;\n // add value to format option object\n // add format option to format object; we're adding another option\n format.options[formatOptionName] = buffer;\n buffer = \"\";\n } else if (template[i] === \"}\" && template[i + 1] === \"}\") {\n // TODO: error - format options parenthesis not closed\n } else {\n buffer += template[i];\n i++;\n }\n break;\n case Loc.FormatOptionEnd:\n if (template[i] === \"|\") {\n loc = Loc.FormatName;\n i += 1;\n startFormat();\n } else if (template[i] === \"}\" && template[i + 1] === \"}\") {\n loc = Loc.Static;\n i += 2;\n // add value segment\n parsed.segments.push(segment);\n } else {\n // TODO: error - no other valid characters\n }\n break;\n }\n }\n\n if (loc === Loc.Static && buffer.length > 0) {\n parsed.segments.push({ type: SegmentType.Static, text: buffer });\n }\n\n return parsed;\n }\n}\n\n/**\n * Dolla's I(nternationalizatio)n module. Manages language translations and locale-based formatting.\n */\nclass I18n {\n #logger: Logger;\n #translations = new Map<string, Translation>();\n #cache: [key: string, values: Record<string, any> | undefined, output: string][] = [];\n #formats = new Map<string, Formatter>();\n\n #initialLocale = \"auto\";\n\n #locale = $<string>(\"en\");\n\n readonly $locale = $(this.#locale);\n\n constructor() {\n this.#logger = createLogger(\"dolla.i18n\");\n\n this.addFormat(\"number\", (_, value, options) => {\n return this.#formatNumber(Number(value), options);\n });\n this.addFormat(\"datetime\", (_, value, options) => {\n return this.#formatDateTime(value as any, options);\n });\n this.addFormat(\"list\", (_, value, options) => {\n return this.#formatList(value as any, options);\n });\n }\n\n get locales() {\n return [...this.#translations.keys()];\n }\n\n async setup(options: I18nSetupOptions) {\n // Convert languages into Language instances.\n options.translations.forEach((entry) => {\n this.#translations.set(entry.locale, new Translation(entry));\n });\n\n // Check that initialLanguage is actually registered.\n if (options.locale && options.locale !== \"auto\") {\n const isRegistered = options.translations.some((entry) => entry.locale === options.locale);\n if (!isRegistered) {\n throw new Error(`Initial locale '${options.locale}' is not registered in the locales array.`);\n }\n this.#initialLocale = options.locale;\n }\n\n this.#logger.info(\n `${this.#translations.size} language${this.#translations.size === 1 ? \"\" : \"s\"} supported: '${[...this.#translations.keys()].join(\"', '\")}'`,\n );\n\n if (this.#translations.size > 0) {\n await this.setLocale(this.#initialLocale);\n }\n }\n\n async setLocale(name: string) {\n let realName!: string;\n\n if (name === \"auto\") {\n let names = [];\n\n if (typeof navigator !== \"undefined\") {\n const nav = navigator as any;\n\n if (nav.languages?.length > 0) {\n names.push(...nav.languages);\n } else if (nav.language) {\n names.push(nav.language);\n } else if (nav.browserLanguage) {\n names.push(nav.browserLanguage);\n } else if (nav.userLanguage) {\n names.push(nav.userLanguage);\n }\n }\n\n for (const name of names) {\n if (this.#translations.has(name)) {\n // Found a matching language.\n realName = name;\n }\n }\n } else {\n // Tag is the actual tag to set.\n if (this.#translations.has(name)) {\n realName = name;\n }\n }\n\n if (realName == null) {\n const firstLanguage = this.#translations.keys().next().value;\n if (firstLanguage) {\n realName = firstLanguage;\n }\n }\n\n if (!realName || !this.#translations.has(realName)) {\n throw new Error(`Locale '${name}' has no translation.`);\n }\n\n const translation = this.#translations.get(realName)!;\n\n try {\n await translation.load();\n\n this.#cache = [];\n this.#locale(realName);\n\n this.#logger.info(\"set language to \" + realName);\n } catch (error) {\n if (error instanceof Error) {\n this.#logger.crash(error);\n }\n }\n }\n\n /**\n * Returns a State containing the value at `key`.\n\n * @param selector - Key to the translated value.\n * @param options - A map of `{{placeholder}}` names and the values to replace them with.\n *\n * @example\n * const $value = t(\"your.key.here\", { count: 5 });\n */\n t(selector: string, options?: TOptions): Signal<string> {\n if (this === undefined) {\n throw new Error(\n `The 't' function cannot be destructured. If you need a standalone version you can import it like so: 'import { t } from \"@manyducks.co/dolla\"'`,\n );\n }\n\n return $(() => {\n const values: Record<string, any> = {};\n\n // Track all option values.\n for (const key in options) {\n values[key] = get(options[key]);\n }\n\n return this.#getValue(this.#locale(), selector, values);\n });\n }\n\n #getValue(locale: string, selector: string, options: Record<string, any>): string {\n const cached = this.#getCached(selector, options);\n if (cached) return cached;\n\n const translation = this.#translations.get(locale)!;\n\n // Handle count (pluralization) and context. Keys become \"key_context_pluralization\".\n\n if (options.context != null) {\n selector += \"_\" + options.context;\n }\n if (options.count != null) {\n if (options.ordinal) {\n // Try to match the exact number key if there is one (e.g. \"myExampleKey_ordinal_(=2)\" when count is 2).\n const exact = `${selector}_ordinal_(=${options.count})`;\n if (translation.hasTemplate(exact)) {\n selector = exact;\n } else {\n selector += \"_ordinal_\" + new Intl.PluralRules(locale, { type: \"ordinal\" }).select(options.count);\n }\n } else {\n // Try to match the exact number key if there is one (e.g. \"myExampleKey_(=2)\" when count is 2).\n const exact = `${selector}_(=${options.count})`;\n if (translation.hasTemplate(exact)) {\n selector = exact;\n } else {\n selector += \"_\" + new Intl.PluralRules(locale).select(options.count);\n }\n }\n }\n\n const template = translation.getTemplate(selector);\n let output = \"\";\n\n for (const segment of template.segments) {\n if (segment.type === SegmentType.Static) {\n output += segment.text;\n } else if (segment.type === SegmentType.Variable) {\n let value = resolve(options, segment.name);\n\n const formats = options.formatOverrides?.[segment.name] ?? [...segment.formats];\n\n if (segment.name === \"count\" && formats.length === 0) {\n formats.push({ name: \"number\", options: {} });\n }\n\n for (const format of formats) {\n const fn = this.#formats.get(format.name);\n if (fn == null) {\n const error = new Error(\n `Failed to load format '${format.name}' when processing '${selector}', template: ${template}`,\n );\n this.#logger.crash(error);\n throw error;\n }\n value = fn(locale, value, format.options);\n }\n\n output += value;\n }\n }\n\n return output;\n }\n\n /**\n * Add a custom format callback.\n *\n * @example\n * Dolla.i18n.addFormat(\"uppercase\", (locale, value, options) => {\n * return value.toUpperCase();\n * });\n *\n * {\n * \"greeting\": \"Hello, {{name|uppercase}}!\"\n * }\n *\n * t(\"greeting\", {name: \"world\"}); // State<\"Hello, WORLD!\">\n */\n addFormat(name: string, callback: (locale: string, value: unknown, options: Record<string, any>) => string) {\n this.#formats.set(name, callback);\n }\n\n /**\n * Creates an `Intl.Collator` configured for the current locale.\n * NOTE: The locale is tracked if called within a signal tracking context.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator#options\n */\n collator(options?: Intl.CollatorOptions) {\n return new Intl.Collator(this.#locale(), options);\n }\n\n /**\n * Formats a number for the current locale. Uses `Intl.NumberFormat` under the hood.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options\n */\n number(count: MaybeSignal<number | bigint>, options?: Intl.NumberFormatOptions): Signal<string> {\n return $(() => this.#formatNumber(get(count), options));\n }\n\n #formatNumber(count: number | bigint, options?: Intl.NumberFormatOptions): string {\n // NOTE: Locale is tracked if called within a tracking context.\n return new Intl.NumberFormat(this.#locale(), options).format(count);\n }\n\n /**\n * Formats a date for the current locale. Uses `Intl.DateTimeFormat` under the hood.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#options\n *\n * @example\n * const date = new Date();\n * const $formatted = Dolla.i18n.dateTime(date, { dateFormat: \"short\" });\n */\n dateTime(\n date?: MaybeSignal<string | number | Date | undefined>,\n options?: Intl.DateTimeFormatOptions,\n ): Signal<string> {\n return $(() => this.#formatDateTime(get(date), options));\n }\n\n #formatDateTime(date?: string | number | Date, options?: Intl.DateTimeFormatOptions): string {\n // NOTE: Locale is tracked if called within a tracking context.\n return new Intl.DateTimeFormat(this.#locale(), options).format(isString(date) ? new Date(date) : date);\n }\n\n /**\n * Formats a list for the current locale. Uses `Intl.ListFormat` under the hood.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#options\n *\n * @example\n * const list = new Date();\n * const $formatted = Dolla.i18n.list(list, { });\n */\n list(list: MaybeSignal<Iterable<string>>, options?: Intl.ListFormatOptions): Signal<string> {\n return $(() => this.#formatList(get(list), options));\n }\n\n #formatList(list: Iterable<string>, options?: Intl.ListFormatOptions): string {\n // NOTE: Locale is tracked if called within a tracking context.\n return new Intl.ListFormat(this.#locale(), options).format(list);\n }\n\n // relativeTime(): State<string> {\n\n // }\n\n #getCached(key: string, values?: Record<string, any>): string | undefined {\n for (const entry of this.#cache) {\n if (entry[0] === key && deepEqual(entry[1], values)) {\n return entry[2];\n }\n }\n }\n}\n\nfunction resolve(object: any, key: string) {\n const parsed = String(key)\n .split(/[\\.\\[\\]]/)\n .filter((part) => part.trim() !== \"\");\n let value = object;\n\n while (parsed.length > 0) {\n const part = parsed.shift()!;\n\n if (value != null) {\n value = value[part];\n } else {\n value = undefined;\n }\n }\n\n return value;\n}\n\nexport const i18n = new I18n();\nexport const t = i18n.t.bind(i18n);\n"],"names":["Translation","config","__privateAdd","_Translation_instances","__publicField","_isLoaded","_templates","strings","__privateGet","isObject","isFunction","isString","res","body","entries","__privateMethod","compile_fn","entry","selector","path","key","typeOf","parseTemplate_fn","template","Loc","Loc2","parsed","buffer","i","loc","segment","format","formatOptionName","startSegment","startFormat","I18n","_I18n_instances","_logger","_translations","_cache","_formats","_initialLocale","_locale","__privateSet","createLogger","_","value","options","formatNumber_fn","formatDateTime_fn","formatList_fn","name","realName","names","nav","_a","firstLanguage","translation","error","values","get","getValue_fn","callback","count","date","list","locale","cached","getCached_fn","exact","output","resolve","formats","fn","deepEqual","object","part","i18n","t"],"mappings":";;;;;;;;;;AAsGA,MAAMA,EAAY;AAAA,EAOhB,YAAYC,GAA2B;AAPzC,IAAAC,EAAA,MAAAC;AACE,IAAAC,EAAA;AAEA,IAAAF,EAAA,MAAAG,GAAY;AAEZ,IAAAH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AAGK,SAAAA;AACT;AAEa,MAAA+B,IAAO,IAAI1C,EAAK,GAChB2C,KAAID,EAAK,EAAE,KAAKA,CAAI;"}
|
|
1
|
+
{"version":3,"file":"i18n.js","sources":["../src/i18n/index.ts"],"sourcesContent":["import { createLogger, type Logger } from \"../core/logger.js\";\nimport { $, get, type MaybeSignal, type Signal } from \"../core/signals.js\";\nimport { isFunction, isObject, isString, typeOf } from \"../typeChecking.js\";\nimport { deepEqual } from \"../utils.js\";\n\n// ----- Types ----- //\n\n/**\n * A JSON object of translated strings. Values can be string templates or nested objects.\n */\ninterface LocalizedStrings extends Record<string, string | LocalizedStrings> {}\n\nenum SegmentType {\n Static,\n Variable,\n}\ntype StringTemplate = { segments: (StaticSegment | VariableSegment)[] };\n/**\n * A string segment with literal text to be appended without processing.\n */\ntype StaticSegment = {\n type: SegmentType.Static;\n text: string;\n};\n/**\n * A variable passed to the t() function. Needs to be formatted before it is appended.\n */\ntype VariableSegment = {\n type: SegmentType.Variable;\n name: string;\n formats: Format[];\n};\n/**\n * A formatter to be applied to a variable.\n */\ntype Format = {\n name: string;\n options: Record<string, any>;\n};\n\nexport interface TranslationConfig {\n /**\n * Name of the locale this translation is for (BCP 47 locale names recommended).\n */\n locale: string;\n\n /**\n * An object with translated strings for this language.\n */\n strings?: LocalizedStrings;\n\n /**\n * A callback function that returns a Promise that resolves to the translation object for this language.\n */\n fetch?: () => Promise<LocalizedStrings>;\n\n /**\n * Path to a JSON file with translated strings for this language.\n */\n path?: string;\n}\n\nexport type I18nSetupOptions = {\n /**\n * Default locale to load on startup\n */\n locale?: string | null;\n\n translations: TranslationConfig[];\n};\n\nexport type TOptions = {\n /**\n *\n */\n count?: MaybeSignal<number>;\n\n /**\n *\n */\n context?: MaybeSignal<string>;\n\n /**\n * Override formats specified in the template with the ones in the array for each named variable.\n *\n * @example\n * t(\"example_key\", {\n * count: 5,\n * formatOverrides: {\n * count: [ { name: \"datetime\", options: { style: \"currency\", currency: \"JPY\" } } ]\n * }\n * });\n */\n formatOverrides?: MaybeSignal<Record<string, Record<string, Format[]>>>;\n\n [value: string]: MaybeSignal<any>;\n};\n\nexport type Formatter = (locale: string, value: unknown, options: Record<string, any>) => string;\n\n// ----- Code ----- //\n\nclass Translation {\n config: TranslationConfig;\n\n #isLoaded = false;\n\n #templates = new Map<string, StringTemplate>();\n\n constructor(config: TranslationConfig) {\n this.config = config;\n }\n\n async load(): Promise<void> {\n let strings: LocalizedStrings | undefined;\n\n if (!this.#isLoaded) {\n if (isObject(this.config.strings)) {\n strings = this.config.strings;\n } else if (isFunction(this.config.fetch)) {\n strings = await this.config.fetch();\n if (!isObject(strings)) {\n throw new Error(`Fetch function did not return an object of language strings: ${strings}`);\n }\n } else if (isString(this.config.path)) {\n const res = await fetch(this.config.path);\n if (res.ok) {\n const body = await res.json();\n if (isObject(body)) {\n strings = body as LocalizedStrings;\n } else {\n throw new Error(\n `Language path '${this.config.path}' did not return an object of language strings: ${body}`,\n );\n }\n } else {\n throw new Error(`HTTP request failed.`);\n }\n }\n }\n\n if (strings) {\n const entries = this.#compile(strings);\n for (const entry of entries) {\n this.#templates.set(entry[0], entry[1]);\n }\n } else {\n throw new Error(`Language could not be loaded.`);\n }\n }\n\n getTemplate(selector: string): StringTemplate {\n return (\n this.#templates.get(selector) ?? {\n segments: [{ type: SegmentType.Static, text: `[MISSING: ${selector}]` }],\n }\n );\n }\n\n hasTemplate(selector: string): boolean {\n return this.#templates.has(selector);\n }\n\n #compile(strings: { [key: string]: any }, path: string[] = []): [string, StringTemplate][] {\n const entries: [string, StringTemplate][] = [];\n\n for (const key in strings) {\n switch (typeOf(strings[key])) {\n case \"string\":\n entries.push([[...path, key].join(\".\"), this.#parseTemplate(strings[key])]);\n break;\n case \"object\":\n entries.push(...this.#compile(strings[key], [...path, key]));\n break;\n default:\n throw new Error(\n `Expected to find a string or object at ${[...path, key].join(\".\")}. Got: ${typeOf(strings[key])}`,\n );\n }\n }\n\n return entries;\n }\n\n #parseTemplate(template: string): StringTemplate {\n // \"{{itemName}} costs {{amount | number(style: currency, currency: USD)}}.\"\n\n enum Loc {\n /**\n * Outside value braces.\n */\n Static,\n /**\n * Inside value braces; currently parsing the name of the value. e.g. `{{ [name] | number(style: currency, currency: USD) }}`\n */\n ValueName,\n /**\n * Inside value braces; currently parsing the name of a format function. e.g. `{{ name | [number](style: currency, currency: USD) }}`\n */\n FormatName,\n /**\n * Inside value braces; currently parsing the name of a format option. e.g. `{{ name | number([style]: currency, currency: USD) }}`\n */\n FormatOptionName,\n /**\n * Inside value braces; currently parsing the value of a format option. e.g. `{{ name | number(style: [currency], currency: USD ) }}`\n */\n FormatOptionValue,\n /**\n * Inside value braces; just reached the closing bracket of a format option. e.g. `{{ name | number(style: [currency], currency: USD) [] }}`\n */\n FormatOptionEnd,\n }\n\n const parsed: StringTemplate = {\n segments: [],\n };\n\n let buffer = \"\";\n let i = 0;\n let loc: Loc = Loc.Static;\n let segment!: VariableSegment;\n let format!: VariableSegment[\"formats\"][0];\n\n let formatOptionName!: string;\n\n const startSegment = () => {\n segment = {\n type: SegmentType.Variable,\n name: \"\",\n formats: [],\n };\n };\n\n const startFormat = () => {\n format = {\n name: \"\",\n options: {},\n };\n };\n\n while (i < template.length) {\n // Skip spaces (unless we're in static)\n if (loc !== Loc.Static && template[i] === \" \") {\n i++;\n continue;\n }\n\n switch (loc) {\n case Loc.Static:\n if (template[i] === \"{\" && template[i + 1] === \"{\") {\n loc = Loc.ValueName;\n i += 2;\n // close static segment\n if (buffer.length > 0) {\n parsed.segments.push({ type: SegmentType.Static, text: buffer });\n buffer = \"\";\n }\n startSegment();\n } else {\n buffer += template[i];\n i++;\n }\n break;\n case Loc.ValueName:\n if (template[i] === \"|\") {\n loc = Loc.FormatName;\n i += 1;\n // add name to value segment\n segment.name = buffer;\n buffer = \"\";\n startFormat();\n } else if (template[i] === \"}\" && template[i + 1] === \"}\") {\n loc = Loc.Static;\n i += 2;\n // close value segment\n segment.name = buffer;\n buffer = \"\";\n parsed.segments.push(segment);\n } else {\n buffer += template[i];\n i++;\n }\n break;\n case Loc.FormatName:\n if (template[i] === \"(\") {\n loc = Loc.FormatOptionName;\n i += 1;\n // add name to format object\n format.name = buffer;\n buffer = \"\";\n } else if (template[i] === \"}\" && template[i + 1] === \"}\") {\n loc = Loc.Static;\n i += 2;\n // close format and value segment\n segment.formats.push(format);\n parsed.segments.push(segment);\n } else {\n buffer += template[i];\n i++;\n }\n break;\n case Loc.FormatOptionName:\n if (template[i] === \")\") {\n // TODO: error - no value provided for option\n } else if (template[i] === \":\") {\n loc = Loc.FormatOptionValue;\n i += 1;\n // add name to format option object\n formatOptionName = buffer;\n buffer = \"\";\n } else if (template[i] === \"}\" && template[i + 1] === \"}\") {\n // TODO: error - format options parenthesis not closed\n } else {\n buffer += template[i];\n i++;\n }\n break;\n case Loc.FormatOptionValue:\n if (template[i] === \")\") {\n loc = Loc.FormatOptionEnd;\n i += 1;\n // add value to format option object\n // add format option to format object; we're done with this format\n format.options[formatOptionName] = buffer;\n buffer = \"\";\n segment.formats.push(format);\n } else if (template[i] === \",\") {\n loc = Loc.FormatOptionName;\n i += 1;\n // add value to format option object\n // add format option to format object; we're adding another option\n format.options[formatOptionName] = buffer;\n buffer = \"\";\n } else if (template[i] === \"}\" && template[i + 1] === \"}\") {\n // TODO: error - format options parenthesis not closed\n } else {\n buffer += template[i];\n i++;\n }\n break;\n case Loc.FormatOptionEnd:\n if (template[i] === \"|\") {\n loc = Loc.FormatName;\n i += 1;\n startFormat();\n } else if (template[i] === \"}\" && template[i + 1] === \"}\") {\n loc = Loc.Static;\n i += 2;\n // add value segment\n parsed.segments.push(segment);\n } else {\n // TODO: error - no other valid characters\n }\n break;\n }\n }\n\n if (loc === Loc.Static && buffer.length > 0) {\n parsed.segments.push({ type: SegmentType.Static, text: buffer });\n }\n\n return parsed;\n }\n}\n\n/**\n * Dolla's I(nternationalizatio)n module. Manages language translations and locale-based formatting.\n */\nclass I18n {\n #logger: Logger;\n #translations = new Map<string, Translation>();\n #cache: [key: string, values: Record<string, any> | undefined, output: string][] = [];\n #formats = new Map<string, Formatter>();\n\n #initialLocale = \"auto\";\n\n #locale = $<string>(\"en\");\n\n readonly $locale = $(this.#locale);\n\n constructor() {\n this.#logger = createLogger(\"dolla.i18n\");\n\n this.addFormat(\"number\", (_, value, options) => {\n return this.#formatNumber(Number(value), options);\n });\n this.addFormat(\"datetime\", (_, value, options) => {\n return this.#formatDateTime(value as any, options);\n });\n this.addFormat(\"list\", (_, value, options) => {\n return this.#formatList(value as any, options);\n });\n }\n\n get locales() {\n return [...this.#translations.keys()];\n }\n\n async setup(options: I18nSetupOptions) {\n // Convert languages into Language instances.\n options.translations.forEach((entry) => {\n this.#translations.set(entry.locale, new Translation(entry));\n });\n\n // Check that initialLanguage is actually registered.\n if (options.locale && options.locale !== \"auto\") {\n const isRegistered = options.translations.some((entry) => entry.locale === options.locale);\n if (!isRegistered) {\n throw new Error(`Initial locale '${options.locale}' is not registered in the locales array.`);\n }\n this.#initialLocale = options.locale;\n }\n\n this.#logger.info(\n `${this.#translations.size} language${this.#translations.size === 1 ? \"\" : \"s\"} supported: '${[...this.#translations.keys()].join(\"', '\")}'`,\n );\n\n if (this.#translations.size > 0) {\n await this.setLocale(this.#initialLocale);\n }\n }\n\n async setLocale(name: string) {\n let realName!: string;\n\n if (name === \"auto\") {\n let names = [];\n\n if (typeof navigator !== \"undefined\") {\n const nav = navigator as any;\n\n if (nav.languages?.length > 0) {\n names.push(...nav.languages);\n } else if (nav.language) {\n names.push(nav.language);\n } else if (nav.browserLanguage) {\n names.push(nav.browserLanguage);\n } else if (nav.userLanguage) {\n names.push(nav.userLanguage);\n }\n }\n\n for (const name of names) {\n if (this.#translations.has(name)) {\n // Found a matching language.\n realName = name;\n }\n }\n } else {\n // Tag is the actual tag to set.\n if (this.#translations.has(name)) {\n realName = name;\n }\n }\n\n if (realName == null) {\n const firstLanguage = this.#translations.keys().next().value;\n if (firstLanguage) {\n realName = firstLanguage;\n }\n }\n\n if (!realName || !this.#translations.has(realName)) {\n throw new Error(`Locale '${name}' has no translation.`);\n }\n\n const translation = this.#translations.get(realName)!;\n\n try {\n await translation.load();\n\n this.#cache = [];\n this.#locale(realName);\n\n this.#logger.info(\"set language to \" + realName);\n } catch (error) {\n if (error instanceof Error) {\n this.#logger.crash(error);\n }\n }\n }\n\n /**\n * Returns a State containing the value at `key`.\n\n * @param selector - Key to the translated value.\n * @param options - A map of `{{placeholder}}` names and the values to replace them with.\n *\n * @example\n * const $value = t(\"your.key.here\", { count: 5 });\n */\n t(selector: string, options?: TOptions): Signal<string> {\n if (this === undefined) {\n throw new Error(\n `The 't' function cannot be destructured. If you need a standalone version you can import it like so: 'import { t } from \"@manyducks.co/dolla\"'`,\n );\n }\n\n return $(() => {\n const values: Record<string, any> = {};\n\n // Track all option values.\n for (const key in options) {\n values[key] = get(options[key]);\n }\n\n return this.#getValue(this.#locale(), selector, values);\n });\n }\n\n #getValue(locale: string, selector: string, options: Record<string, any>): string {\n const cached = this.#getCached(selector, options);\n if (cached) return cached;\n\n const translation = this.#translations.get(locale)!;\n\n // Handle count (pluralization) and context. Keys become \"key_context_pluralization\".\n\n if (options.context != null) {\n selector += \"_\" + options.context;\n }\n if (options.count != null) {\n if (options.ordinal) {\n // Try to match the exact number key if there is one (e.g. \"myExampleKey_ordinal_(=2)\" when count is 2).\n const exact = `${selector}_ordinal_(=${options.count})`;\n if (translation.hasTemplate(exact)) {\n selector = exact;\n } else {\n selector += \"_ordinal_\" + new Intl.PluralRules(locale, { type: \"ordinal\" }).select(options.count);\n }\n } else {\n // Try to match the exact number key if there is one (e.g. \"myExampleKey_(=2)\" when count is 2).\n const exact = `${selector}_(=${options.count})`;\n if (translation.hasTemplate(exact)) {\n selector = exact;\n } else {\n selector += \"_\" + new Intl.PluralRules(locale).select(options.count);\n }\n }\n }\n\n const template = translation.getTemplate(selector);\n let output = \"\";\n\n for (const segment of template.segments) {\n if (segment.type === SegmentType.Static) {\n output += segment.text;\n } else if (segment.type === SegmentType.Variable) {\n let value = resolve(options, segment.name);\n\n const formats = options.formatOverrides?.[segment.name] ?? [...segment.formats];\n\n if (segment.name === \"count\" && formats.length === 0) {\n formats.push({ name: \"number\", options: {} });\n }\n\n for (const format of formats) {\n const fn = this.#formats.get(format.name);\n if (fn == null) {\n const error = new Error(\n `Failed to load format '${format.name}' when processing '${selector}', template: ${template}`,\n );\n this.#logger.crash(error);\n throw error;\n }\n value = fn(locale, value, format.options);\n }\n\n output += value;\n }\n }\n\n return output;\n }\n\n /**\n * Add a custom format callback.\n *\n * @example\n * Dolla.i18n.addFormat(\"uppercase\", (locale, value, options) => {\n * return value.toUpperCase();\n * });\n *\n * {\n * \"greeting\": \"Hello, {{name|uppercase}}!\"\n * }\n *\n * t(\"greeting\", {name: \"world\"}); // State<\"Hello, WORLD!\">\n */\n addFormat(name: string, callback: (locale: string, value: unknown, options: Record<string, any>) => string) {\n this.#formats.set(name, callback);\n }\n\n /**\n * Creates an `Intl.Collator` configured for the current locale.\n * NOTE: The locale is tracked if called within a signal tracking context.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator#options\n */\n collator(options?: Intl.CollatorOptions) {\n return new Intl.Collator(this.#locale(), options);\n }\n\n /**\n * Formats a number for the current locale. Uses `Intl.NumberFormat` under the hood.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options\n */\n number(count: MaybeSignal<number | bigint>, options?: Intl.NumberFormatOptions): Signal<string> {\n return $(() => this.#formatNumber(get(count), options));\n }\n\n #formatNumber(count: number | bigint, options?: Intl.NumberFormatOptions): string {\n // NOTE: Locale is tracked if called within a tracking context.\n return new Intl.NumberFormat(this.#locale(), options).format(count);\n }\n\n /**\n * Formats a date for the current locale. Uses `Intl.DateTimeFormat` under the hood.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#options\n *\n * @example\n * const date = new Date();\n * const $formatted = Dolla.i18n.dateTime(date, { dateFormat: \"short\" });\n */\n dateTime(\n date?: MaybeSignal<string | number | Date | undefined>,\n options?: Intl.DateTimeFormatOptions,\n ): Signal<string> {\n return $(() => this.#formatDateTime(get(date), options));\n }\n\n #formatDateTime(date?: string | number | Date, options?: Intl.DateTimeFormatOptions): string {\n // NOTE: Locale is tracked if called within a tracking context.\n return new Intl.DateTimeFormat(this.#locale(), options).format(isString(date) ? new Date(date) : date);\n }\n\n /**\n * Formats a list for the current locale. Uses `Intl.ListFormat` under the hood.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#options\n *\n * @example\n * const list = new Date();\n * const $formatted = Dolla.i18n.list(list, { });\n */\n list(list: MaybeSignal<Iterable<string>>, options?: Intl.ListFormatOptions): Signal<string> {\n return $(() => this.#formatList(get(list), options));\n }\n\n #formatList(list: Iterable<string>, options?: Intl.ListFormatOptions): string {\n // NOTE: Locale is tracked if called within a tracking context.\n return new Intl.ListFormat(this.#locale(), options).format(list);\n }\n\n // relativeTime(date?: MaybeSignal<string | number | Date | undefined>): Signal<string> {}\n\n #getCached(key: string, values?: Record<string, any>): string | undefined {\n for (const entry of this.#cache) {\n if (entry[0] === key && deepEqual(entry[1], values)) {\n return entry[2];\n }\n }\n }\n}\n\nfunction resolve(object: any, key: string) {\n const parsed = String(key)\n .split(/[\\.\\[\\]]/)\n .filter((part) => part.trim() !== \"\");\n let value = object;\n\n while (parsed.length > 0) {\n const part = parsed.shift()!;\n\n if (value != null) {\n value = value[part];\n } else {\n value = undefined;\n }\n }\n\n return value;\n}\n\nexport const i18n = new I18n();\nexport const t = i18n.t.bind(i18n);\n"],"names":["Translation","config","__privateAdd","_Translation_instances","__publicField","_isLoaded","_templates","strings","__privateGet","isObject","isFunction","isString","res","body","entries","__privateMethod","compile_fn","entry","selector","path","key","typeOf","parseTemplate_fn","template","Loc","Loc2","parsed","buffer","i","loc","segment","format","formatOptionName","startSegment","startFormat","I18n","_I18n_instances","_logger","_translations","_cache","_formats","_initialLocale","_locale","__privateSet","createLogger","_","value","options","formatNumber_fn","formatDateTime_fn","formatList_fn","name","realName","names","nav","_a","firstLanguage","translation","error","values","get","getValue_fn","callback","count","date","list","locale","cached","getCached_fn","exact","output","resolve","formats","fn","deepEqual","object","part","i18n","t"],"mappings":";;;;;;;;;;;AAsGA,MAAMA,EAAY;AAAA,EAOhB,YAAYC,GAA2B;AAPzC,IAAAC,EAAA,MAAAC;AACE,IAAAC,EAAA;AAEA,IAAAF,EAAA,MAAAG,GAAY;AAEZ,IAAAH,EAAA,MAAAI,uBAAiB,IAA4B;AAG3C,SAAK,SAASL;AAAA,EAAA;AAAA,EAGhB,MAAM,OAAsB;AACtB,QAAAM;AAEA,QAAA,CAACC,EAAA,MAAKH;AACR,UAAII,EAAS,KAAK,OAAO,OAAO;AAC9B,QAAAF,IAAU,KAAK,OAAO;AAAA,eACbG,EAAW,KAAK,OAAO,KAAK;AAEjC,YADMH,IAAA,MAAM,KAAK,OAAO,MAAM,GAC9B,CAACE,EAASF,CAAO;AACnB,gBAAM,IAAI,MAAM,gEAAgEA,CAAO,EAAE;AAAA,iBAElFI,EAAS,KAAK,OAAO,IAAI,GAAG;AACrC,cAAMC,IAAM,MAAM,MAAM,KAAK,OAAO,IAAI;AACxC,YAAIA,EAAI,IAAI;AACJ,gBAAAC,IAAO,MAAMD,EAAI,KAAK;AACxB,cAAAH,EAASI,CAAI;AACL,YAAAN,IAAAM;AAAA;AAEV,kBAAM,IAAI;AAAA,cACR,kBAAkB,KAAK,OAAO,IAAI,mDAAmDA,CAAI;AAAA,YAC3F;AAAA,QACF;AAEM,gBAAA,IAAI,MAAM,sBAAsB;AAAA,MACxC;AAAA;AAIJ,QAAIN,GAAS;AACL,YAAAO,IAAUC,EAAA,MAAKZ,GAAAa,GAAL,WAAcT;AAC9B,iBAAWU,KAASH;AAClB,QA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+B;AACnB,QAAAtB,EAAM,CAAC,MAAMG,KAAOsD,EAAUzD,EAAM,CAAC,GAAG0C,CAAM;AAChD,aAAO1C,EAAM,CAAC;AAElB;AAIJ,SAASsD,EAAQI,GAAavD,GAAa;AACzC,QAAMM,IAAS,OAAON,CAAG,EACtB,MAAM,UAAU,EAChB,OAAO,CAACwD,MAASA,EAAK,KAAA,MAAW,EAAE;AACtC,MAAI9B,IAAQ6B;AAEL,SAAAjD,EAAO,SAAS,KAAG;AAClB,UAAAkD,IAAOlD,EAAO,MAAM;AAE1B,IAAIoB,KAAS,OACXA,IAAQA,EAAM8B,CAAI,IAEV9B,IAAA;AAAA,EACV;AAGK,SAAAA;AACT;AAEa,MAAA+B,IAAO,IAAI1C,EAAK,GAChB2C,KAAID,EAAK,EAAE,KAAKA,CAAI;"}
|
package/dist/index.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { $ as
|
|
3
|
-
import { m as
|
|
4
|
-
import { c as
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
} else if (arguments.length === 1)
|
|
14
|
-
e = arguments[0];
|
|
15
|
-
else
|
|
16
|
-
throw new Error("Ref called with too many arguments. Expected 0 or 1 arguments.");
|
|
17
|
-
return e;
|
|
18
|
-
};
|
|
1
|
+
import { g as m } from "./signals-gCwiIe5X.js";
|
|
2
|
+
import { $ as q, b as S, d as _, e as $, s as B, a as W, u as z } from "./signals-gCwiIe5X.js";
|
|
3
|
+
import { R as g, D as w, m as n, w as y, C as l, L as c, V as f } from "./markup-UzKSsawX.js";
|
|
4
|
+
import { M as G, c as K, p as P, r as j, a as H, u as J } from "./markup-UzKSsawX.js";
|
|
5
|
+
import { r as X } from "./ref-BD79iqlg.js";
|
|
6
|
+
import { R as N, M as x, U as b } from "./router-BXBX8lnO.js";
|
|
7
|
+
import { a as L } from "./typeChecking-CbltMOUt.js";
|
|
8
|
+
import { o as M } from "./logger-Bl496yfY.js";
|
|
9
|
+
import { c as Z, g as p, s as ee, a as ae, b as te } from "./logger-Bl496yfY.js";
|
|
10
|
+
const U = (e) => e;
|
|
11
|
+
function E(e, t) {
|
|
12
|
+
return new g(t, e.each, e.key ?? U, e.children);
|
|
19
13
|
}
|
|
20
|
-
function
|
|
21
|
-
return t
|
|
14
|
+
function F(e, t) {
|
|
15
|
+
return new w(t, () => {
|
|
16
|
+
let a = !0;
|
|
17
|
+
return e.when != null && e.unless != null ? a = m(e.when) && !m(e.unless) : e.when != null ? a = m(e.when) : e.unless != null && (a = !m(e.unless)), a ? e.children : e.fallback;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
function k(e) {
|
|
21
|
+
return n("div", {
|
|
22
22
|
style: {
|
|
23
23
|
backgroundColor: "#880000",
|
|
24
24
|
color: "#fff",
|
|
@@ -28,17 +28,17 @@ function E(e) {
|
|
|
28
28
|
fontSize: "20px"
|
|
29
29
|
},
|
|
30
30
|
children: [
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
n("h1", { style: { marginBottom: "0.5rem" }, children: "The app has crashed" }),
|
|
32
|
+
n("p", {
|
|
33
33
|
style: { marginBottom: "0.25rem" },
|
|
34
34
|
children: [
|
|
35
|
-
|
|
35
|
+
n("span", {
|
|
36
36
|
style: { fontFamily: "monospace" },
|
|
37
37
|
children: e.loggerName
|
|
38
38
|
}),
|
|
39
39
|
y(
|
|
40
40
|
e.tag,
|
|
41
|
-
|
|
41
|
+
n("span", {
|
|
42
42
|
style: { fontFamily: "monospace", opacity: 0.5 },
|
|
43
43
|
children: ` [${e.tagName ? `${e.tagName}: ` : ""}${e.tag}]`
|
|
44
44
|
})
|
|
@@ -46,7 +46,7 @@ function E(e) {
|
|
|
46
46
|
" says:"
|
|
47
47
|
]
|
|
48
48
|
}),
|
|
49
|
-
|
|
49
|
+
n("blockquote", {
|
|
50
50
|
style: {
|
|
51
51
|
backgroundColor: "#991111",
|
|
52
52
|
padding: "0.25em",
|
|
@@ -55,7 +55,7 @@ function E(e) {
|
|
|
55
55
|
marginBottom: "1rem"
|
|
56
56
|
},
|
|
57
57
|
children: [
|
|
58
|
-
|
|
58
|
+
n("span", {
|
|
59
59
|
style: {
|
|
60
60
|
display: "inline-block",
|
|
61
61
|
backgroundColor: "red",
|
|
@@ -70,47 +70,50 @@ function E(e) {
|
|
|
70
70
|
e.error.message
|
|
71
71
|
]
|
|
72
72
|
}),
|
|
73
|
-
|
|
73
|
+
n("p", { children: "Please see the browser console for details." })
|
|
74
74
|
]
|
|
75
75
|
});
|
|
76
76
|
}
|
|
77
77
|
let s = !1;
|
|
78
|
-
async function
|
|
79
|
-
if (
|
|
78
|
+
async function V(e, t, a) {
|
|
79
|
+
if (L(Element, t, "Expected an element or a selector string. Got type: %t, value: %v"), s)
|
|
80
80
|
throw new Error("A Dolla app is already mounted.");
|
|
81
|
-
let
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
s &&
|
|
85
|
-
}),
|
|
86
|
-
async function
|
|
87
|
-
s && (
|
|
81
|
+
let o, i, d = (a == null ? void 0 : a.crashView) ?? k;
|
|
82
|
+
const r = (a == null ? void 0 : a.context) ?? new l("App");
|
|
83
|
+
M((h) => {
|
|
84
|
+
s && u(), new f(r, d, h).mount(t);
|
|
85
|
+
}), l.emit(r, c.WILL_MOUNT), e instanceof N ? (i = e, o = await i[x](t, r)) : o = new f(r, e, {}), o.mount(t), s = !0, l.emit(r, c.DID_MOUNT);
|
|
86
|
+
async function u() {
|
|
87
|
+
s && (l.emit(r, c.WILL_UNMOUNT), o.unmount(!1), i && await i[b](), s = !1, l.emit(r, c.DID_UNMOUNT));
|
|
88
88
|
}
|
|
89
|
-
return
|
|
89
|
+
return u;
|
|
90
90
|
}
|
|
91
91
|
export {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
92
|
+
q as $,
|
|
93
|
+
E as For,
|
|
94
|
+
G as MarkupNode,
|
|
95
|
+
F as Show,
|
|
96
|
+
S as batch,
|
|
97
|
+
K as createContext,
|
|
98
|
+
Z as createLogger,
|
|
99
|
+
_ as deepEqual,
|
|
100
|
+
$ as effect,
|
|
101
|
+
m as get,
|
|
102
|
+
p as getEnv,
|
|
103
|
+
n as m,
|
|
104
|
+
V as mount,
|
|
105
|
+
M as onLoggerCrash,
|
|
106
|
+
P as portal,
|
|
107
|
+
X as ref,
|
|
105
108
|
j as render,
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
109
|
+
H as repeat,
|
|
110
|
+
ee as setEnv,
|
|
111
|
+
ae as setLogFilter,
|
|
112
|
+
te as setLogLevels,
|
|
113
|
+
B as shallowEqual,
|
|
114
|
+
W as strictEqual,
|
|
115
|
+
J as unless,
|
|
116
|
+
z as untracked,
|
|
114
117
|
y as when
|
|
115
118
|
};
|
|
116
119
|
//# sourceMappingURL=index.js.map
|