verbaly 0.64.0 → 0.66.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/README.md +2 -2
- package/dist/index.cjs +39 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +39 -28
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
<p align="center">
|
|
10
10
|
<a href="https://www.npmjs.com/package/verbaly"><img src="https://img.shields.io/npm/v/verbaly?logo=npm&color=cb3837" alt="npm version" /></a>
|
|
11
|
-
<img src="https://img.shields.io/badge/gzip-3.
|
|
11
|
+
<img src="https://img.shields.io/badge/gzip-3.31KB-3fb950" alt="3.31KB gzip" />
|
|
12
12
|
<img src="https://img.shields.io/badge/dependencies-0-3fb950" alt="zero dependencies" />
|
|
13
13
|
<img src="https://img.shields.io/badge/types-included-3178C6?logo=typescript&logoColor=white" alt="TypeScript" />
|
|
14
14
|
<a href="https://github.com/AronSoto/verbaly/blob/develop/LICENSE"><img src="https://img.shields.io/npm/l/verbaly?color=blue" alt="MIT" /></a>
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
|
|
17
17
|
---
|
|
18
18
|
|
|
19
|
-
Most i18n tools make you maintain key files by hand: keys drift from the code, no type-safety, heavy setup. **Verbaly inverts the flow:** write the source text in your code, a build plugin extracts stable keys, types and per-locale modules. Compiler-grade safety, a **3.
|
|
19
|
+
Most i18n tools make you maintain key files by hand: keys drift from the code, no type-safety, heavy setup. **Verbaly inverts the flow:** write the source text in your code, a build plugin extracts stable keys, types and per-locale modules. Compiler-grade safety, a **3.31 KB** runtime, zero dependencies. That is `createVerbaly` on its own; a site with no framework, which also pulls the DOM interpreter and the url helpers, ships **6.09 KB**. Both are measured on every release by `pnpm --filter verbaly size`, never estimated.
|
|
20
20
|
|
|
21
21
|
```ts
|
|
22
22
|
// You write this:
|
package/dist/index.cjs
CHANGED
|
@@ -30,34 +30,44 @@ const prCache = /* @__PURE__ */ new Map();
|
|
|
30
30
|
const rtfCache = /* @__PURE__ */ new Map();
|
|
31
31
|
const lfCache = /* @__PURE__ */ new Map();
|
|
32
32
|
const dnCache = /* @__PURE__ */ new Map();
|
|
33
|
-
function cached(cache, key, make) {
|
|
33
|
+
function cached(cache, key, locale, make) {
|
|
34
34
|
let hit = cache.get(key);
|
|
35
35
|
if (!hit) {
|
|
36
36
|
if (cache.size >= CACHE_CAP) cache.delete(cache.keys().next().value);
|
|
37
|
-
|
|
37
|
+
try {
|
|
38
|
+
hit = make(locale);
|
|
39
|
+
} catch {
|
|
40
|
+
let tag = locale.replace(/_/g, "-");
|
|
41
|
+
try {
|
|
42
|
+
hit = make(tag);
|
|
43
|
+
} catch {
|
|
44
|
+
hit = make(tag = void 0);
|
|
45
|
+
}
|
|
46
|
+
warnOnce(`"${locale}" is not a locale tag, so Intl reads it as ${tag ?? "its default locale"}`);
|
|
47
|
+
}
|
|
38
48
|
cache.set(key, hit);
|
|
39
49
|
}
|
|
40
50
|
return hit;
|
|
41
51
|
}
|
|
42
52
|
function numberFormat(locale, options) {
|
|
43
53
|
const key = locale + (options ? JSON.stringify(options) : "");
|
|
44
|
-
return cached(nfCache, key, () => new Intl.NumberFormat(
|
|
54
|
+
return cached(nfCache, key, locale, (tag) => new Intl.NumberFormat(tag, options));
|
|
45
55
|
}
|
|
46
56
|
function dateTimeFormat(locale, options) {
|
|
47
57
|
const key = locale + (options ? JSON.stringify(options) : "");
|
|
48
|
-
return cached(dtfCache, key, () => new Intl.DateTimeFormat(
|
|
58
|
+
return cached(dtfCache, key, locale, (tag) => new Intl.DateTimeFormat(tag, options));
|
|
49
59
|
}
|
|
50
60
|
function pluralRules(locale, type = "cardinal") {
|
|
51
|
-
return cached(prCache, locale + type, () => new Intl.PluralRules(
|
|
61
|
+
return cached(prCache, locale + type, locale, (tag) => new Intl.PluralRules(tag, { type }));
|
|
52
62
|
}
|
|
53
63
|
function relativeTimeFormat(locale) {
|
|
54
|
-
return cached(rtfCache, locale, () => new Intl.RelativeTimeFormat(
|
|
64
|
+
return cached(rtfCache, locale, locale, (tag) => new Intl.RelativeTimeFormat(tag, { numeric: "auto" }));
|
|
55
65
|
}
|
|
56
66
|
function listFormat(locale, type) {
|
|
57
|
-
return cached(lfCache, locale + type, () => new Intl.ListFormat(
|
|
67
|
+
return cached(lfCache, locale + type, locale, (tag) => new Intl.ListFormat(tag, { type }));
|
|
58
68
|
}
|
|
59
69
|
function displayNames(locale) {
|
|
60
|
-
return cached(dnCache, locale, () => new Intl.DisplayNames(
|
|
70
|
+
return cached(dnCache, locale, locale, (tag) => new Intl.DisplayNames(tag, { type: "language" }));
|
|
61
71
|
}
|
|
62
72
|
//#endregion
|
|
63
73
|
//#region src/format.ts
|
|
@@ -69,7 +79,7 @@ function formatNodes(nodes, ctx) {
|
|
|
69
79
|
return out;
|
|
70
80
|
}
|
|
71
81
|
function where(ctx) {
|
|
72
|
-
return ctx
|
|
82
|
+
return ctx?.key ? ` in "${ctx.key}"` : "";
|
|
73
83
|
}
|
|
74
84
|
function formatParam(node, ctx) {
|
|
75
85
|
let value = ctx.params?.[node.name];
|
|
@@ -77,10 +87,7 @@ function formatParam(node, ctx) {
|
|
|
77
87
|
warnOnce(`missing param "${node.name}"${where(ctx)}`);
|
|
78
88
|
return `{${node.name}}`;
|
|
79
89
|
}
|
|
80
|
-
|
|
81
|
-
warnOnce(`cannot format ${describe$1(value)}${where(ctx)}`);
|
|
82
|
-
value = "";
|
|
83
|
-
}
|
|
90
|
+
value = usable(value, ctx);
|
|
84
91
|
if (node.variants) {
|
|
85
92
|
const chosen = pickVariant(node.variants, value, ctx.locale, node.ordinal);
|
|
86
93
|
if (!chosen) {
|
|
@@ -95,6 +102,11 @@ function formatParam(node, ctx) {
|
|
|
95
102
|
if (node.format) return applyFormat(value, node, ctx);
|
|
96
103
|
return autoFormat(value, ctx.locale, ctx);
|
|
97
104
|
}
|
|
105
|
+
function usable(value, ctx) {
|
|
106
|
+
if (coercible(value)) return value;
|
|
107
|
+
warnOnce(`cannot format ${describe$1(value)}${where(ctx)}`);
|
|
108
|
+
return "";
|
|
109
|
+
}
|
|
98
110
|
function coercible(value) {
|
|
99
111
|
try {
|
|
100
112
|
return typeof `${value}` === "string";
|
|
@@ -184,7 +196,7 @@ function applyFormat(value, node, ctx) {
|
|
|
184
196
|
function autoFormat(value, locale, ctx) {
|
|
185
197
|
if (value === null || value === void 0) return "";
|
|
186
198
|
if (Number.isNaN(value instanceof Date ? value.getTime() : value)) {
|
|
187
|
-
warnOnce(`${describe$1(value)}${
|
|
199
|
+
warnOnce(`${describe$1(value)}${where(ctx)} renders as plain text`);
|
|
188
200
|
return String(value);
|
|
189
201
|
}
|
|
190
202
|
if (typeof value === "number") return numberFormat(locale).format(value);
|
|
@@ -210,7 +222,7 @@ const RTL_SCRIPTS = /^(Arab|Aran|Hebr|Thaa|Nkoo|Syrc|Samr|Mand|Adlm|Rohg|Yezi)$/
|
|
|
210
222
|
const RTL_LANGS = /^(ar|he|fa|ur|ps|sd|ug|yi|dv|ckb)$/;
|
|
211
223
|
function localeDirection(locale) {
|
|
212
224
|
try {
|
|
213
|
-
const loc = new Intl.Locale(locale);
|
|
225
|
+
const loc = new Intl.Locale(locale.replace(/_/g, "-"));
|
|
214
226
|
const direction = (loc.getTextInfo?.() ?? loc.textInfo)?.direction;
|
|
215
227
|
if (direction === "rtl" || direction === "ltr") return direction;
|
|
216
228
|
const script = loc.script ?? loc.maximize().script;
|
|
@@ -395,7 +407,7 @@ async function switchLocale(instance, locale, options = {}) {
|
|
|
395
407
|
persistLocale(locale, storageKey);
|
|
396
408
|
if (!routed) return;
|
|
397
409
|
const target = localePath(locale, {
|
|
398
|
-
supported: options.supported ?? [locale],
|
|
410
|
+
supported: options.supported ?? instance.locales ?? [locale],
|
|
399
411
|
sourceLocale: options.sourceLocale,
|
|
400
412
|
base: options.base,
|
|
401
413
|
routing: options.routing
|
|
@@ -406,7 +418,9 @@ function assign(path) {
|
|
|
406
418
|
if (typeof location !== "undefined") location.assign(path);
|
|
407
419
|
}
|
|
408
420
|
function persistLocale(locale, storageKey = LOCALE_STORAGE_KEY) {
|
|
409
|
-
|
|
421
|
+
try {
|
|
422
|
+
if (storageKey) getStorage()?.setItem(storageKey, locale);
|
|
423
|
+
} catch {}
|
|
410
424
|
if (typeof document !== "undefined") {
|
|
411
425
|
document.documentElement.lang = locale;
|
|
412
426
|
document.documentElement.dir = localeDirection(locale);
|
|
@@ -594,7 +608,7 @@ function parse(message, icu) {
|
|
|
594
608
|
return cached;
|
|
595
609
|
}
|
|
596
610
|
function unparsedIcu(message) {
|
|
597
|
-
warnOnce("a message uses ICU syntax
|
|
611
|
+
warnOnce("a message uses ICU syntax and no ICU parser is loaded: set icu: true in your verbaly config, or pass icu to createVerbaly");
|
|
598
612
|
return [{
|
|
599
613
|
kind: "text",
|
|
600
614
|
value: message
|
|
@@ -745,10 +759,10 @@ function splitTop(input) {
|
|
|
745
759
|
//#endregion
|
|
746
760
|
//#region src/instance.ts
|
|
747
761
|
function createVerbaly(options = {}) {
|
|
748
|
-
const dict = {};
|
|
762
|
+
const dict = Object.setPrototypeOf({}, null);
|
|
749
763
|
const listeners = /* @__PURE__ */ new Set();
|
|
750
|
-
const formatters = options.formatters
|
|
751
|
-
const loaders = options.loaders
|
|
764
|
+
const formatters = Object.assign(Object.setPrototypeOf({}, null), options.formatters);
|
|
765
|
+
const loaders = Object.assign(Object.setPrototypeOf({}, null), options.loaders);
|
|
752
766
|
const loaded = /* @__PURE__ */ new Set();
|
|
753
767
|
const inFlight = /* @__PURE__ */ new Map();
|
|
754
768
|
const fallbacks = options.fallback ? Array.isArray(options.fallback) ? options.fallback : [options.fallback] : [];
|
|
@@ -806,7 +820,7 @@ function createVerbaly(options = {}) {
|
|
|
806
820
|
}
|
|
807
821
|
function tagged(strings, values) {
|
|
808
822
|
let out = strings[0] ?? "";
|
|
809
|
-
for (let i = 0; i < values.length; i++) out += autoFormat(values[i], locale) + (strings[i + 1] ?? "");
|
|
823
|
+
for (let i = 0; i < values.length; i++) out += autoFormat(usable(values[i]), locale) + (strings[i + 1] ?? "");
|
|
810
824
|
return out;
|
|
811
825
|
}
|
|
812
826
|
function notify() {
|
|
@@ -1038,7 +1052,7 @@ function bindDom(instance, options = {}) {
|
|
|
1038
1052
|
const richAttr = `${attr}-rich`;
|
|
1039
1053
|
const linksAttr = `${attr}-links`;
|
|
1040
1054
|
const richTags = new Set(options.richTags ?? RICH_TAGS);
|
|
1041
|
-
const globalLinks = options.richLinks;
|
|
1055
|
+
const globalLinks = options.richLinks && Object.assign(Object.create(null), options.richLinks);
|
|
1042
1056
|
const argsCache = /* @__PURE__ */ new WeakMap();
|
|
1043
1057
|
const linksCache = /* @__PURE__ */ new WeakMap();
|
|
1044
1058
|
const attrsCache = /* @__PURE__ */ new WeakMap();
|
|
@@ -1061,10 +1075,7 @@ function bindDom(instance, options = {}) {
|
|
|
1061
1075
|
if (!raw) return globalLinks;
|
|
1062
1076
|
return fromCache(linksCache, el, raw, () => {
|
|
1063
1077
|
const own = parseArgs(raw);
|
|
1064
|
-
return own ? globalLinks
|
|
1065
|
-
...globalLinks,
|
|
1066
|
-
...own
|
|
1067
|
-
} : own : globalLinks;
|
|
1078
|
+
return own ? Object.assign(Object.create(null), globalLinks, own) : globalLinks;
|
|
1068
1079
|
});
|
|
1069
1080
|
}
|
|
1070
1081
|
function renderRich(el, nodes, links) {
|
|
@@ -1161,7 +1172,7 @@ function warnOnUnboundHead(root, selector) {
|
|
|
1161
1172
|
function warnOnLangMismatch(locale) {
|
|
1162
1173
|
const lang = document.documentElement.lang;
|
|
1163
1174
|
if (!lang || narrowLocales(lang).includes(locale) || narrowLocales(locale).includes(lang)) return;
|
|
1164
|
-
warnOnce(`the page is <html lang="${lang}"> and this instance is in "${locale}"
|
|
1175
|
+
warnOnce(`the page is <html lang="${lang}"> and this instance is in "${locale}": read the url with localeFromPath, or call persistLocale first`);
|
|
1165
1176
|
}
|
|
1166
1177
|
function parseArgs(raw) {
|
|
1167
1178
|
if (!raw) return void 0;
|