verbaly 0.15.0 → 0.17.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 +8 -0
- package/dist/devtools.cjs +7 -4
- package/dist/devtools.cjs.map +1 -1
- package/dist/devtools.d.cts +1 -1
- package/dist/devtools.d.ts +1 -1
- package/dist/devtools.js +7 -4
- package/dist/devtools.js.map +1 -1
- package/dist/index.cjs +141 -60
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +16 -2
- package/dist/index.d.ts +16 -2
- package/dist/index.js +138 -61
- package/dist/index.js.map +1 -1
- package/dist/{types-B4g2nUEf.d.ts → types-BTWCYu7Y.d.cts} +2 -2
- package/dist/{types-B4g2nUEf.d.cts → types-BTWCYu7Y.d.ts} +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -98,6 +98,14 @@ v.setLocale('es');
|
|
|
98
98
|
persistLocale('es'); // localStorage + <html lang>
|
|
99
99
|
```
|
|
100
100
|
|
|
101
|
+
```ts
|
|
102
|
+
// server-side (SSR): one instance per request, locale from the request itself
|
|
103
|
+
import { createVerbaly, negotiateLocale } from 'verbaly';
|
|
104
|
+
|
|
105
|
+
const locale = negotiateLocale(request.headers.get('accept-language'), ['en', 'es', 'pt']);
|
|
106
|
+
const v = createVerbaly({ locale, fallback: 'en', messages });
|
|
107
|
+
```
|
|
108
|
+
|
|
101
109
|
## ✨ What you get
|
|
102
110
|
|
|
103
111
|
- **Hybrid compiler + runtime** — static text compiled (types + tree-shaking), dynamic content via a real runtime path.
|
package/dist/devtools.cjs
CHANGED
|
@@ -6,6 +6,9 @@ const HOTKEY = {
|
|
|
6
6
|
shift: "shiftKey",
|
|
7
7
|
meta: "metaKey"
|
|
8
8
|
};
|
|
9
|
+
function statusOf(info, currentLocale) {
|
|
10
|
+
return !info ? "miss" : info.from === currentLocale ? "hit" : "fallback";
|
|
11
|
+
}
|
|
9
12
|
function attachDevtools(instance, options = {}) {
|
|
10
13
|
if (typeof document === "undefined") throw new Error("[verbaly] devtools requires a DOM");
|
|
11
14
|
const root = options.root ?? document.body;
|
|
@@ -31,8 +34,8 @@ function attachDevtools(instance, options = {}) {
|
|
|
31
34
|
out.push({
|
|
32
35
|
el,
|
|
33
36
|
key,
|
|
34
|
-
status:
|
|
35
|
-
from: info?.
|
|
37
|
+
status: statusOf(info, instance.locale),
|
|
38
|
+
from: info?.from,
|
|
36
39
|
source: info?.source
|
|
37
40
|
});
|
|
38
41
|
}
|
|
@@ -57,8 +60,8 @@ function attachDevtools(instance, options = {}) {
|
|
|
57
60
|
}
|
|
58
61
|
const key = el.getAttribute(attr);
|
|
59
62
|
const info = instance.inspect(key);
|
|
60
|
-
const status =
|
|
61
|
-
tip.innerHTML = `<div class="verbaly-dt-key">${esc(key)}</div><div class="verbaly-dt-row ${status}">${status}${info && info.
|
|
63
|
+
const status = statusOf(info, instance.locale);
|
|
64
|
+
tip.innerHTML = `<div class="verbaly-dt-key">${esc(key)}</div><div class="verbaly-dt-row ${status}">${status}${info && info.from !== instance.locale ? ` · from ${esc(info.from)}` : ""}</div>` + (info ? `<div class="verbaly-dt-src">${esc(info.source)}</div>` : "");
|
|
62
65
|
tip.hidden = false;
|
|
63
66
|
tip.style.left = `${Math.min(e.clientX + 14, window.innerWidth - 320)}px`;
|
|
64
67
|
tip.style.top = `${e.clientY + 14}px`;
|
package/dist/devtools.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"devtools.cjs","names":[],"sources":["../src/devtools.ts"],"sourcesContent":["import type { ResolveStatus, Verbaly } from './types';\n\nexport interface DevtoolsOptions {\n root?: ParentNode;\n attribute?: string;\n hotkey?: 'alt' | 'ctrl' | 'shift' | 'meta';\n catalogDir?: string;\n}\n\ninterface Bound {\n el: Element;\n key: string;\n status: ResolveStatus;\n from?: string;\n source?: string;\n}\n\nconst HOTKEY: Record<NonNullable<DevtoolsOptions['hotkey']>, keyof MouseEvent> = {\n alt: 'altKey',\n ctrl: 'ctrlKey',\n shift: 'shiftKey',\n meta: 'metaKey',\n};\n\n// dev-only inspector — \"what key is this text?\" in the browser (opt-in, tree-shakeable)\nexport function attachDevtools(instance: Verbaly, options: DevtoolsOptions = {}): () => void {\n if (typeof document === 'undefined') throw new Error('[verbaly] devtools requires a DOM');\n const root = options.root ?? document.body;\n const attr = options.attribute ?? 'data-verbaly';\n const modifier = HOTKEY[options.hotkey ?? 'alt'];\n const dir = options.catalogDir ?? 'locales';\n\n const style = document.createElement('style');\n style.textContent = CSS;\n document.head.appendChild(style);\n\n const panel = document.createElement('div');\n panel.className = 'verbaly-dt';\n root.appendChild(panel);\n\n const tip = document.createElement('div');\n tip.className = 'verbaly-dt-tip';\n tip.hidden = true;\n root.appendChild(tip);\n\n function scan(): Bound[] {\n const out: Bound[] = [];\n for (const el of root.querySelectorAll(`[${attr}]`)) {\n const key = el.getAttribute(attr);\n if (!key) continue;\n const info = instance.inspect(key);\n out.push({\n el,\n key,\n status:
|
|
1
|
+
{"version":3,"file":"devtools.cjs","names":[],"sources":["../src/devtools.ts"],"sourcesContent":["import type { ResolveStatus, Verbaly } from './types';\n\nexport interface DevtoolsOptions {\n root?: ParentNode;\n attribute?: string;\n hotkey?: 'alt' | 'ctrl' | 'shift' | 'meta';\n catalogDir?: string;\n}\n\ninterface Bound {\n el: Element;\n key: string;\n status: ResolveStatus;\n from?: string;\n source?: string;\n}\n\nconst HOTKEY: Record<NonNullable<DevtoolsOptions['hotkey']>, keyof MouseEvent> = {\n alt: 'altKey',\n ctrl: 'ctrlKey',\n shift: 'shiftKey',\n meta: 'metaKey',\n};\n\nfunction statusOf(\n info: { from: string } | undefined,\n currentLocale: string,\n): ResolveStatus {\n return !info ? 'miss' : info.from === currentLocale ? 'hit' : 'fallback';\n}\n\n// dev-only inspector — \"what key is this text?\" in the browser (opt-in, tree-shakeable)\nexport function attachDevtools(instance: Verbaly, options: DevtoolsOptions = {}): () => void {\n if (typeof document === 'undefined') throw new Error('[verbaly] devtools requires a DOM');\n const root = options.root ?? document.body;\n const attr = options.attribute ?? 'data-verbaly';\n const modifier = HOTKEY[options.hotkey ?? 'alt'];\n const dir = options.catalogDir ?? 'locales';\n\n const style = document.createElement('style');\n style.textContent = CSS;\n document.head.appendChild(style);\n\n const panel = document.createElement('div');\n panel.className = 'verbaly-dt';\n root.appendChild(panel);\n\n const tip = document.createElement('div');\n tip.className = 'verbaly-dt-tip';\n tip.hidden = true;\n root.appendChild(tip);\n\n function scan(): Bound[] {\n const out: Bound[] = [];\n for (const el of root.querySelectorAll(`[${attr}]`)) {\n const key = el.getAttribute(attr);\n if (!key) continue;\n const info = instance.inspect(key);\n out.push({\n el,\n key,\n status: statusOf(info, instance.locale),\n from: info?.from,\n source: info?.source,\n });\n }\n return out;\n }\n\n function render(): void {\n const bound = scan();\n const miss = bound.filter((b) => b.status === 'miss');\n const fb = bound.filter((b) => b.status === 'fallback');\n const rows = miss\n .map(\n (b) =>\n `<li><code>${esc(b.key)}</code><span>add to <code>${esc(dir)}/${esc(instance.locale)}.json</code></span></li>`,\n )\n .join('');\n panel.innerHTML =\n `<div class=\"verbaly-dt-hd\"><b>verbaly</b> · ${esc(instance.locale)}</div>` +\n `<div class=\"verbaly-dt-stats\">` +\n `<span class=\"ok\">${bound.length - miss.length - fb.length} ok</span>` +\n `<span class=\"fb\">${fb.length} fallback</span>` +\n `<span class=\"miss\">${miss.length} missing</span></div>` +\n (rows ? `<ul class=\"verbaly-dt-miss\">${rows}</ul>` : '') +\n `<div class=\"verbaly-dt-hint\">hold <kbd>${options.hotkey ?? 'Alt'}</kbd> + hover to inspect</div>`;\n }\n\n function onMove(e: MouseEvent): void {\n if (!e[modifier]) {\n tip.hidden = true;\n return;\n }\n const el = (e.target as Element | null)?.closest?.(`[${attr}]`);\n if (!el) {\n tip.hidden = true;\n return;\n }\n const key = el.getAttribute(attr)!;\n const info = instance.inspect(key);\n const status = statusOf(info, instance.locale);\n tip.innerHTML =\n `<div class=\"verbaly-dt-key\">${esc(key)}</div>` +\n `<div class=\"verbaly-dt-row ${status}\">${status}${info && info.from !== instance.locale ? ` · from ${esc(info.from)}` : ''}</div>` +\n (info ? `<div class=\"verbaly-dt-src\">${esc(info.source)}</div>` : '');\n tip.hidden = false;\n tip.style.left = `${Math.min(e.clientX + 14, window.innerWidth - 320)}px`;\n tip.style.top = `${e.clientY + 14}px`;\n }\n\n const unsub = instance.subscribe(render);\n // own panel/tip writes must not re-trigger render (infinite microtask loop)\n const observer = new MutationObserver((records) => {\n if (records.every((r) => panel.contains(r.target) || tip.contains(r.target))) return;\n render();\n });\n observer.observe(root instanceof Node ? root : document.body, {\n childList: true,\n subtree: true,\n attributes: true,\n attributeFilter: [attr],\n });\n document.addEventListener('mousemove', onMove);\n render();\n\n return () => {\n unsub();\n observer.disconnect();\n document.removeEventListener('mousemove', onMove);\n panel.remove();\n tip.remove();\n style.remove();\n };\n}\n\nfunction esc(text: string): string {\n return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');\n}\n\nconst CSS = `\n.verbaly-dt{position:fixed;bottom:12px;right:12px;z-index:2147483646;max-width:300px;font:12px/1.4 ui-monospace,monospace;color:#e6f0e0;background:#0b120c;border:1px solid #2a3a2c;border-radius:10px;padding:10px;box-shadow:0 8px 30px rgba(0,0,0,.4)}\n.verbaly-dt-hd{font-size:11px;opacity:.85;margin-bottom:6px}\n.verbaly-dt-stats{display:flex;gap:8px;margin-bottom:6px}\n.verbaly-dt-stats span{padding:2px 6px;border-radius:6px;background:#152016}\n.verbaly-dt-stats .ok{color:#bef12d}\n.verbaly-dt-stats .fb{color:#f1c40f}\n.verbaly-dt-stats .miss{color:#ff6b6b}\n.verbaly-dt-miss{list-style:none;margin:0 0 6px;padding:0;max-height:180px;overflow:auto}\n.verbaly-dt-miss li{display:flex;flex-direction:column;gap:2px;padding:4px 0;border-top:1px solid #1c281d}\n.verbaly-dt-miss span{opacity:.6;font-size:11px}\n.verbaly-dt-hint{opacity:.55;font-size:11px}\n.verbaly-dt-hint kbd{background:#152016;border-radius:4px;padding:0 4px}\n.verbaly-dt-tip{position:fixed;z-index:2147483647;max-width:300px;font:12px/1.4 ui-monospace,monospace;color:#e6f0e0;background:#0b120c;border:1px solid #2a3a2c;border-radius:8px;padding:8px;pointer-events:none;box-shadow:0 8px 30px rgba(0,0,0,.4)}\n.verbaly-dt-key{color:#bef12d;font-weight:600;margin-bottom:4px}\n.verbaly-dt-row{margin-bottom:4px}\n.verbaly-dt-row.miss{color:#ff6b6b}\n.verbaly-dt-row.fallback{color:#f1c40f}\n.verbaly-dt-row.hit{color:#7bd88f}\n.verbaly-dt-src{opacity:.75;white-space:pre-wrap;word-break:break-word}\n`;\n"],"mappings":"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"}
|
package/dist/devtools.d.cts
CHANGED
package/dist/devtools.d.ts
CHANGED
package/dist/devtools.js
CHANGED
|
@@ -5,6 +5,9 @@ const HOTKEY = {
|
|
|
5
5
|
shift: "shiftKey",
|
|
6
6
|
meta: "metaKey"
|
|
7
7
|
};
|
|
8
|
+
function statusOf(info, currentLocale) {
|
|
9
|
+
return !info ? "miss" : info.from === currentLocale ? "hit" : "fallback";
|
|
10
|
+
}
|
|
8
11
|
function attachDevtools(instance, options = {}) {
|
|
9
12
|
if (typeof document === "undefined") throw new Error("[verbaly] devtools requires a DOM");
|
|
10
13
|
const root = options.root ?? document.body;
|
|
@@ -30,8 +33,8 @@ function attachDevtools(instance, options = {}) {
|
|
|
30
33
|
out.push({
|
|
31
34
|
el,
|
|
32
35
|
key,
|
|
33
|
-
status:
|
|
34
|
-
from: info?.
|
|
36
|
+
status: statusOf(info, instance.locale),
|
|
37
|
+
from: info?.from,
|
|
35
38
|
source: info?.source
|
|
36
39
|
});
|
|
37
40
|
}
|
|
@@ -56,8 +59,8 @@ function attachDevtools(instance, options = {}) {
|
|
|
56
59
|
}
|
|
57
60
|
const key = el.getAttribute(attr);
|
|
58
61
|
const info = instance.inspect(key);
|
|
59
|
-
const status =
|
|
60
|
-
tip.innerHTML = `<div class="verbaly-dt-key">${esc(key)}</div><div class="verbaly-dt-row ${status}">${status}${info && info.
|
|
62
|
+
const status = statusOf(info, instance.locale);
|
|
63
|
+
tip.innerHTML = `<div class="verbaly-dt-key">${esc(key)}</div><div class="verbaly-dt-row ${status}">${status}${info && info.from !== instance.locale ? ` · from ${esc(info.from)}` : ""}</div>` + (info ? `<div class="verbaly-dt-src">${esc(info.source)}</div>` : "");
|
|
61
64
|
tip.hidden = false;
|
|
62
65
|
tip.style.left = `${Math.min(e.clientX + 14, window.innerWidth - 320)}px`;
|
|
63
66
|
tip.style.top = `${e.clientY + 14}px`;
|
package/dist/devtools.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"devtools.js","names":[],"sources":["../src/devtools.ts"],"sourcesContent":["import type { ResolveStatus, Verbaly } from './types';\n\nexport interface DevtoolsOptions {\n root?: ParentNode;\n attribute?: string;\n hotkey?: 'alt' | 'ctrl' | 'shift' | 'meta';\n catalogDir?: string;\n}\n\ninterface Bound {\n el: Element;\n key: string;\n status: ResolveStatus;\n from?: string;\n source?: string;\n}\n\nconst HOTKEY: Record<NonNullable<DevtoolsOptions['hotkey']>, keyof MouseEvent> = {\n alt: 'altKey',\n ctrl: 'ctrlKey',\n shift: 'shiftKey',\n meta: 'metaKey',\n};\n\n// dev-only inspector — \"what key is this text?\" in the browser (opt-in, tree-shakeable)\nexport function attachDevtools(instance: Verbaly, options: DevtoolsOptions = {}): () => void {\n if (typeof document === 'undefined') throw new Error('[verbaly] devtools requires a DOM');\n const root = options.root ?? document.body;\n const attr = options.attribute ?? 'data-verbaly';\n const modifier = HOTKEY[options.hotkey ?? 'alt'];\n const dir = options.catalogDir ?? 'locales';\n\n const style = document.createElement('style');\n style.textContent = CSS;\n document.head.appendChild(style);\n\n const panel = document.createElement('div');\n panel.className = 'verbaly-dt';\n root.appendChild(panel);\n\n const tip = document.createElement('div');\n tip.className = 'verbaly-dt-tip';\n tip.hidden = true;\n root.appendChild(tip);\n\n function scan(): Bound[] {\n const out: Bound[] = [];\n for (const el of root.querySelectorAll(`[${attr}]`)) {\n const key = el.getAttribute(attr);\n if (!key) continue;\n const info = instance.inspect(key);\n out.push({\n el,\n key,\n status:
|
|
1
|
+
{"version":3,"file":"devtools.js","names":[],"sources":["../src/devtools.ts"],"sourcesContent":["import type { ResolveStatus, Verbaly } from './types';\n\nexport interface DevtoolsOptions {\n root?: ParentNode;\n attribute?: string;\n hotkey?: 'alt' | 'ctrl' | 'shift' | 'meta';\n catalogDir?: string;\n}\n\ninterface Bound {\n el: Element;\n key: string;\n status: ResolveStatus;\n from?: string;\n source?: string;\n}\n\nconst HOTKEY: Record<NonNullable<DevtoolsOptions['hotkey']>, keyof MouseEvent> = {\n alt: 'altKey',\n ctrl: 'ctrlKey',\n shift: 'shiftKey',\n meta: 'metaKey',\n};\n\nfunction statusOf(\n info: { from: string } | undefined,\n currentLocale: string,\n): ResolveStatus {\n return !info ? 'miss' : info.from === currentLocale ? 'hit' : 'fallback';\n}\n\n// dev-only inspector — \"what key is this text?\" in the browser (opt-in, tree-shakeable)\nexport function attachDevtools(instance: Verbaly, options: DevtoolsOptions = {}): () => void {\n if (typeof document === 'undefined') throw new Error('[verbaly] devtools requires a DOM');\n const root = options.root ?? document.body;\n const attr = options.attribute ?? 'data-verbaly';\n const modifier = HOTKEY[options.hotkey ?? 'alt'];\n const dir = options.catalogDir ?? 'locales';\n\n const style = document.createElement('style');\n style.textContent = CSS;\n document.head.appendChild(style);\n\n const panel = document.createElement('div');\n panel.className = 'verbaly-dt';\n root.appendChild(panel);\n\n const tip = document.createElement('div');\n tip.className = 'verbaly-dt-tip';\n tip.hidden = true;\n root.appendChild(tip);\n\n function scan(): Bound[] {\n const out: Bound[] = [];\n for (const el of root.querySelectorAll(`[${attr}]`)) {\n const key = el.getAttribute(attr);\n if (!key) continue;\n const info = instance.inspect(key);\n out.push({\n el,\n key,\n status: statusOf(info, instance.locale),\n from: info?.from,\n source: info?.source,\n });\n }\n return out;\n }\n\n function render(): void {\n const bound = scan();\n const miss = bound.filter((b) => b.status === 'miss');\n const fb = bound.filter((b) => b.status === 'fallback');\n const rows = miss\n .map(\n (b) =>\n `<li><code>${esc(b.key)}</code><span>add to <code>${esc(dir)}/${esc(instance.locale)}.json</code></span></li>`,\n )\n .join('');\n panel.innerHTML =\n `<div class=\"verbaly-dt-hd\"><b>verbaly</b> · ${esc(instance.locale)}</div>` +\n `<div class=\"verbaly-dt-stats\">` +\n `<span class=\"ok\">${bound.length - miss.length - fb.length} ok</span>` +\n `<span class=\"fb\">${fb.length} fallback</span>` +\n `<span class=\"miss\">${miss.length} missing</span></div>` +\n (rows ? `<ul class=\"verbaly-dt-miss\">${rows}</ul>` : '') +\n `<div class=\"verbaly-dt-hint\">hold <kbd>${options.hotkey ?? 'Alt'}</kbd> + hover to inspect</div>`;\n }\n\n function onMove(e: MouseEvent): void {\n if (!e[modifier]) {\n tip.hidden = true;\n return;\n }\n const el = (e.target as Element | null)?.closest?.(`[${attr}]`);\n if (!el) {\n tip.hidden = true;\n return;\n }\n const key = el.getAttribute(attr)!;\n const info = instance.inspect(key);\n const status = statusOf(info, instance.locale);\n tip.innerHTML =\n `<div class=\"verbaly-dt-key\">${esc(key)}</div>` +\n `<div class=\"verbaly-dt-row ${status}\">${status}${info && info.from !== instance.locale ? ` · from ${esc(info.from)}` : ''}</div>` +\n (info ? `<div class=\"verbaly-dt-src\">${esc(info.source)}</div>` : '');\n tip.hidden = false;\n tip.style.left = `${Math.min(e.clientX + 14, window.innerWidth - 320)}px`;\n tip.style.top = `${e.clientY + 14}px`;\n }\n\n const unsub = instance.subscribe(render);\n // own panel/tip writes must not re-trigger render (infinite microtask loop)\n const observer = new MutationObserver((records) => {\n if (records.every((r) => panel.contains(r.target) || tip.contains(r.target))) return;\n render();\n });\n observer.observe(root instanceof Node ? root : document.body, {\n childList: true,\n subtree: true,\n attributes: true,\n attributeFilter: [attr],\n });\n document.addEventListener('mousemove', onMove);\n render();\n\n return () => {\n unsub();\n observer.disconnect();\n document.removeEventListener('mousemove', onMove);\n panel.remove();\n tip.remove();\n style.remove();\n };\n}\n\nfunction esc(text: string): string {\n return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');\n}\n\nconst CSS = `\n.verbaly-dt{position:fixed;bottom:12px;right:12px;z-index:2147483646;max-width:300px;font:12px/1.4 ui-monospace,monospace;color:#e6f0e0;background:#0b120c;border:1px solid #2a3a2c;border-radius:10px;padding:10px;box-shadow:0 8px 30px rgba(0,0,0,.4)}\n.verbaly-dt-hd{font-size:11px;opacity:.85;margin-bottom:6px}\n.verbaly-dt-stats{display:flex;gap:8px;margin-bottom:6px}\n.verbaly-dt-stats span{padding:2px 6px;border-radius:6px;background:#152016}\n.verbaly-dt-stats .ok{color:#bef12d}\n.verbaly-dt-stats .fb{color:#f1c40f}\n.verbaly-dt-stats .miss{color:#ff6b6b}\n.verbaly-dt-miss{list-style:none;margin:0 0 6px;padding:0;max-height:180px;overflow:auto}\n.verbaly-dt-miss li{display:flex;flex-direction:column;gap:2px;padding:4px 0;border-top:1px solid #1c281d}\n.verbaly-dt-miss span{opacity:.6;font-size:11px}\n.verbaly-dt-hint{opacity:.55;font-size:11px}\n.verbaly-dt-hint kbd{background:#152016;border-radius:4px;padding:0 4px}\n.verbaly-dt-tip{position:fixed;z-index:2147483647;max-width:300px;font:12px/1.4 ui-monospace,monospace;color:#e6f0e0;background:#0b120c;border:1px solid #2a3a2c;border-radius:8px;padding:8px;pointer-events:none;box-shadow:0 8px 30px rgba(0,0,0,.4)}\n.verbaly-dt-key{color:#bef12d;font-weight:600;margin-bottom:4px}\n.verbaly-dt-row{margin-bottom:4px}\n.verbaly-dt-row.miss{color:#ff6b6b}\n.verbaly-dt-row.fallback{color:#f1c40f}\n.verbaly-dt-row.hit{color:#7bd88f}\n.verbaly-dt-src{opacity:.75;white-space:pre-wrap;word-break:break-word}\n`;\n"],"mappings":"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"}
|
package/dist/index.cjs
CHANGED
|
@@ -84,12 +84,27 @@ function applyFormat(value, format, arg, ctx) {
|
|
|
84
84
|
case "percent": return numberFormat(locale, { style: "percent" }).format(Number(value));
|
|
85
85
|
case "currency":
|
|
86
86
|
if (!arg) return String(value);
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
87
|
+
try {
|
|
88
|
+
return numberFormat(locale, {
|
|
89
|
+
style: "currency",
|
|
90
|
+
currency: arg
|
|
91
|
+
}).format(Number(value));
|
|
92
|
+
} catch {
|
|
93
|
+
warnOnce(`invalid currency "${arg}"`);
|
|
94
|
+
return String(value);
|
|
95
|
+
}
|
|
96
|
+
case "date": try {
|
|
97
|
+
return dateTimeFormat(locale, arg ? { dateStyle: arg } : void 0).format(toDate(value));
|
|
98
|
+
} catch {
|
|
99
|
+
warnOnce(`invalid date "${arg ?? value}"`);
|
|
100
|
+
return String(value);
|
|
101
|
+
}
|
|
102
|
+
case "time": try {
|
|
103
|
+
return dateTimeFormat(locale, { timeStyle: arg ?? "short" }).format(toDate(value));
|
|
104
|
+
} catch {
|
|
105
|
+
warnOnce(`invalid time "${arg ?? value}"`);
|
|
106
|
+
return String(value);
|
|
107
|
+
}
|
|
93
108
|
case "relative": return formatRelative(value, arg, locale);
|
|
94
109
|
case "list":
|
|
95
110
|
if (!Array.isArray(value)) return String(value);
|
|
@@ -153,6 +168,90 @@ function toDate(value) {
|
|
|
153
168
|
return value instanceof Date ? value : new Date(value);
|
|
154
169
|
}
|
|
155
170
|
//#endregion
|
|
171
|
+
//#region src/locale.ts
|
|
172
|
+
const LOCALE_STORAGE_KEY = "verbaly-locale";
|
|
173
|
+
function narrowLocales(lang) {
|
|
174
|
+
const out = [];
|
|
175
|
+
const parts = lang.split("-");
|
|
176
|
+
while (parts.length > 0) {
|
|
177
|
+
out.push(parts.join("-"));
|
|
178
|
+
parts.pop();
|
|
179
|
+
}
|
|
180
|
+
return out;
|
|
181
|
+
}
|
|
182
|
+
function resolveLocale(options) {
|
|
183
|
+
const { supported, fallback = supported[0] ?? "en", storageKey = LOCALE_STORAGE_KEY } = options;
|
|
184
|
+
if (storageKey) {
|
|
185
|
+
const stored = getStorage()?.getItem(storageKey);
|
|
186
|
+
const match = stored && matchSupported(stored, supported);
|
|
187
|
+
if (match) return match;
|
|
188
|
+
}
|
|
189
|
+
for (const lang of preferredLanguages()) {
|
|
190
|
+
const match = matchSupported(lang, supported);
|
|
191
|
+
if (match) return match;
|
|
192
|
+
}
|
|
193
|
+
return fallback;
|
|
194
|
+
}
|
|
195
|
+
function matchSupported(lang, supported) {
|
|
196
|
+
for (const candidate of narrowLocales(lang)) if (supported.includes(candidate)) return candidate;
|
|
197
|
+
}
|
|
198
|
+
function negotiateLocale(header, supported, fallback = supported[0] ?? "en") {
|
|
199
|
+
if (!header) return fallback;
|
|
200
|
+
const ranges = [];
|
|
201
|
+
for (const part of header.split(",")) {
|
|
202
|
+
const [rawTag, ...params] = part.trim().split(";");
|
|
203
|
+
const tag = rawTag?.trim();
|
|
204
|
+
if (!tag || tag === "*") continue;
|
|
205
|
+
let q = 1;
|
|
206
|
+
for (const param of params) {
|
|
207
|
+
const [name, value] = param.split("=");
|
|
208
|
+
if (name?.trim().toLowerCase() === "q") {
|
|
209
|
+
const parsed = Number(value?.trim());
|
|
210
|
+
q = Number.isFinite(parsed) ? parsed : 1;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (q > 0) ranges.push({
|
|
214
|
+
tag,
|
|
215
|
+
q
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
ranges.sort((a, b) => b.q - a.q);
|
|
219
|
+
for (const { tag } of ranges) {
|
|
220
|
+
const match = matchSupportedLoose(tag, supported);
|
|
221
|
+
if (match) return match;
|
|
222
|
+
}
|
|
223
|
+
return fallback;
|
|
224
|
+
}
|
|
225
|
+
function matchSupportedLoose(lang, supported) {
|
|
226
|
+
for (const candidate of narrowLocales(lang.toLowerCase())) {
|
|
227
|
+
const match = supported.find((s) => s.toLowerCase() === candidate);
|
|
228
|
+
if (match) return match;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
function resolveRequestLocale(options) {
|
|
232
|
+
const { supported, cookie, header, fallback = supported[0] ?? "en" } = options;
|
|
233
|
+
if (cookie) {
|
|
234
|
+
const match = negotiateLocale(cookie, supported, "");
|
|
235
|
+
if (match) return match;
|
|
236
|
+
}
|
|
237
|
+
return negotiateLocale(header, supported, fallback);
|
|
238
|
+
}
|
|
239
|
+
function persistLocale(locale, storageKey = LOCALE_STORAGE_KEY) {
|
|
240
|
+
if (storageKey) getStorage()?.setItem(storageKey, locale);
|
|
241
|
+
if (typeof document !== "undefined") document.documentElement.lang = locale;
|
|
242
|
+
}
|
|
243
|
+
function preferredLanguages() {
|
|
244
|
+
if (typeof document === "undefined" || typeof navigator === "undefined") return [];
|
|
245
|
+
return navigator.languages?.length ? navigator.languages : [navigator.language];
|
|
246
|
+
}
|
|
247
|
+
function getStorage() {
|
|
248
|
+
try {
|
|
249
|
+
return typeof localStorage === "undefined" ? null : localStorage;
|
|
250
|
+
} catch {
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
//#endregion
|
|
156
255
|
//#region src/icu.ts
|
|
157
256
|
const ICU_ARG = /\{\s*[a-zA-Z0-9_]+\s*,\s*(?:plural|selectordinal|select|number|date|time)\b/;
|
|
158
257
|
function isIcu(message) {
|
|
@@ -474,12 +573,7 @@ function createVerbaly(options = {}) {
|
|
|
474
573
|
if (options.messages) for (const [loc, tree] of Object.entries(options.messages)) dict[loc] = flatten(tree);
|
|
475
574
|
function chain() {
|
|
476
575
|
if (chainCache) return chainCache;
|
|
477
|
-
const result =
|
|
478
|
-
const parts = locale.split("-");
|
|
479
|
-
while (parts.length > 0) {
|
|
480
|
-
result.push(parts.join("-"));
|
|
481
|
-
parts.pop();
|
|
482
|
-
}
|
|
576
|
+
const result = narrowLocales(locale);
|
|
483
577
|
for (const fb of fallbacks) if (!result.includes(fb)) result.push(fb);
|
|
484
578
|
chainCache = result;
|
|
485
579
|
return result;
|
|
@@ -539,12 +633,9 @@ function createVerbaly(options = {}) {
|
|
|
539
633
|
});
|
|
540
634
|
t.id = () => (strings, ...values) => tagged(strings, values);
|
|
541
635
|
function pendingLoader(target) {
|
|
542
|
-
const
|
|
543
|
-
while (parts.length > 0) {
|
|
544
|
-
const candidate = parts.join("-");
|
|
636
|
+
for (const candidate of narrowLocales(target)) {
|
|
545
637
|
if (loaders[candidate] && !loaded.has(candidate)) return candidate;
|
|
546
638
|
if (loaders[candidate]) return void 0;
|
|
547
|
-
parts.pop();
|
|
548
639
|
}
|
|
549
640
|
}
|
|
550
641
|
function loadLocale(target) {
|
|
@@ -607,7 +698,7 @@ function createVerbaly(options = {}) {
|
|
|
607
698
|
inspect(key) {
|
|
608
699
|
const hit = lookup(key);
|
|
609
700
|
return hit ? {
|
|
610
|
-
|
|
701
|
+
from: hit.from,
|
|
611
702
|
source: hit.msg
|
|
612
703
|
} : void 0;
|
|
613
704
|
}
|
|
@@ -659,6 +750,8 @@ function parseTags(text) {
|
|
|
659
750
|
//#endregion
|
|
660
751
|
//#region src/dom.ts
|
|
661
752
|
const UNSAFE_HREF = /^\s*(javascript|data|vbscript):/i;
|
|
753
|
+
const URL_ATTR = /^(href|src|xlink:href|action|formaction)$/;
|
|
754
|
+
const BLOCKED_ATTR = /^(style|srcdoc)$/;
|
|
662
755
|
function safeHref(href) {
|
|
663
756
|
if (UNSAFE_HREF.test(href)) {
|
|
664
757
|
console.warn(`[verbaly] blocked unsafe href: ${href}`);
|
|
@@ -666,6 +759,14 @@ function safeHref(href) {
|
|
|
666
759
|
}
|
|
667
760
|
return href;
|
|
668
761
|
}
|
|
762
|
+
function normalizeLink(link) {
|
|
763
|
+
const { href, target, rel } = typeof link === "string" ? { href: link } : link;
|
|
764
|
+
return {
|
|
765
|
+
href: safeHref(href),
|
|
766
|
+
target,
|
|
767
|
+
rel
|
|
768
|
+
};
|
|
769
|
+
}
|
|
669
770
|
const RICH_TAGS = [
|
|
670
771
|
"em",
|
|
671
772
|
"strong",
|
|
@@ -697,6 +798,7 @@ function bindDom(instance, options = {}) {
|
|
|
697
798
|
const globalLinks = options.richLinks;
|
|
698
799
|
const argsCache = /* @__PURE__ */ new WeakMap();
|
|
699
800
|
const linksCache = /* @__PURE__ */ new WeakMap();
|
|
801
|
+
const attrsCache = /* @__PURE__ */ new WeakMap();
|
|
700
802
|
function cachedArgs(el) {
|
|
701
803
|
const raw = el.getAttribute(argsAttr);
|
|
702
804
|
const hit = argsCache.get(el);
|
|
@@ -727,11 +829,9 @@ function bindDom(instance, options = {}) {
|
|
|
727
829
|
function renderRich(el, nodes, links) {
|
|
728
830
|
for (const node of nodes) if (typeof node === "string") el.append(node);
|
|
729
831
|
else if (links?.[node.name] !== void 0) {
|
|
730
|
-
const
|
|
731
|
-
const { href, target, rel } = typeof link === "string" ? { href: link } : link;
|
|
832
|
+
const { href, target, rel } = normalizeLink(links[node.name]);
|
|
732
833
|
const a = el.ownerDocument.createElement("a");
|
|
733
|
-
|
|
734
|
-
if (safe !== void 0) a.setAttribute("href", safe);
|
|
834
|
+
if (href !== void 0) a.setAttribute("href", href);
|
|
735
835
|
if (target) a.setAttribute("target", target);
|
|
736
836
|
if (rel) a.setAttribute("rel", rel);
|
|
737
837
|
renderRich(a, node.children, links);
|
|
@@ -752,10 +852,23 @@ function bindDom(instance, options = {}) {
|
|
|
752
852
|
renderRich(el, parseTags(text), cachedLinks(el));
|
|
753
853
|
} else el.textContent = text;
|
|
754
854
|
}
|
|
755
|
-
const
|
|
855
|
+
const rawAttrs = el.getAttribute(attrsAttr);
|
|
856
|
+
const attrsHit = attrsCache.get(el);
|
|
857
|
+
const attrMap = attrsHit && attrsHit.raw === rawAttrs ? attrsHit.map : parseArgs(rawAttrs);
|
|
858
|
+
if (!attrsHit || attrsHit.raw !== rawAttrs) attrsCache.set(el, {
|
|
859
|
+
raw: rawAttrs,
|
|
860
|
+
map: attrMap
|
|
861
|
+
});
|
|
756
862
|
if (attrMap) for (const [name, attrKey] of Object.entries(attrMap)) {
|
|
757
|
-
|
|
758
|
-
|
|
863
|
+
const lower = name.toLowerCase();
|
|
864
|
+
if (typeof attrKey !== "string" || lower.startsWith("on") || BLOCKED_ATTR.test(lower)) continue;
|
|
865
|
+
let value = t(attrKey, args);
|
|
866
|
+
if (URL_ATTR.test(lower)) {
|
|
867
|
+
const safe = safeHref(value);
|
|
868
|
+
if (safe === void 0) continue;
|
|
869
|
+
value = safe;
|
|
870
|
+
}
|
|
871
|
+
el.setAttribute(name, value);
|
|
759
872
|
}
|
|
760
873
|
}
|
|
761
874
|
const selector = `[${attr}], [${attrsAttr}]`;
|
|
@@ -798,50 +911,18 @@ function parseArgs(raw) {
|
|
|
798
911
|
}
|
|
799
912
|
}
|
|
800
913
|
//#endregion
|
|
801
|
-
|
|
802
|
-
const DEFAULT_KEY = "verbaly-locale";
|
|
803
|
-
function resolveLocale(options) {
|
|
804
|
-
const { supported, fallback = supported[0] ?? "en", storageKey = DEFAULT_KEY } = options;
|
|
805
|
-
if (storageKey) {
|
|
806
|
-
const stored = getStorage()?.getItem(storageKey);
|
|
807
|
-
const match = stored && matchSupported(stored, supported);
|
|
808
|
-
if (match) return match;
|
|
809
|
-
}
|
|
810
|
-
for (const lang of preferredLanguages()) {
|
|
811
|
-
const match = matchSupported(lang, supported);
|
|
812
|
-
if (match) return match;
|
|
813
|
-
}
|
|
814
|
-
return fallback;
|
|
815
|
-
}
|
|
816
|
-
function matchSupported(lang, supported) {
|
|
817
|
-
if (supported.includes(lang)) return lang;
|
|
818
|
-
const base = lang.split("-")[0];
|
|
819
|
-
return supported.includes(base) ? base : void 0;
|
|
820
|
-
}
|
|
821
|
-
function persistLocale(locale, storageKey = DEFAULT_KEY) {
|
|
822
|
-
if (storageKey) getStorage()?.setItem(storageKey, locale);
|
|
823
|
-
if (typeof document !== "undefined") document.documentElement.lang = locale;
|
|
824
|
-
}
|
|
825
|
-
function preferredLanguages() {
|
|
826
|
-
if (typeof document === "undefined" || typeof navigator === "undefined") return [];
|
|
827
|
-
return navigator.languages?.length ? navigator.languages : [navigator.language];
|
|
828
|
-
}
|
|
829
|
-
function getStorage() {
|
|
830
|
-
try {
|
|
831
|
-
return typeof localStorage === "undefined" ? null : localStorage;
|
|
832
|
-
} catch {
|
|
833
|
-
return null;
|
|
834
|
-
}
|
|
835
|
-
}
|
|
836
|
-
//#endregion
|
|
914
|
+
exports.LOCALE_STORAGE_KEY = LOCALE_STORAGE_KEY;
|
|
837
915
|
exports.RICH_TAGS = RICH_TAGS;
|
|
838
916
|
exports.bindDom = bindDom;
|
|
839
917
|
exports.createVerbaly = createVerbaly;
|
|
840
918
|
exports.flatten = flatten;
|
|
919
|
+
exports.negotiateLocale = negotiateLocale;
|
|
920
|
+
exports.normalizeLink = normalizeLink;
|
|
841
921
|
exports.parse = parse;
|
|
842
922
|
exports.parseTags = parseTags;
|
|
843
923
|
exports.persistLocale = persistLocale;
|
|
844
924
|
exports.resolveLocale = resolveLocale;
|
|
925
|
+
exports.resolveRequestLocale = resolveRequestLocale;
|
|
845
926
|
exports.safeHref = safeHref;
|
|
846
927
|
|
|
847
928
|
//# sourceMappingURL=index.cjs.map
|