@proveanything/smartlinks 2.2.4 → 2.2.6

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.
@@ -1,6 +1,6 @@
1
1
  # Smartlinks API Summary
2
2
 
3
- Version: 2.2.4 | Generated: 2026-12-02T15:08:47.460Z
3
+ Version: 2.2.6 | Generated: 2026-12-04T13:25:02.911Z
4
4
 
5
5
  This is a concise summary of all available API functions and types.
6
6
 
@@ -220,7 +220,7 @@ All standard widget props apply:
220
220
  | `tag` | `TagContext` | ❌ | **Deprecated** — use `itemContext.tag` instead. Kept for one release. |
221
221
  | `onNavigate` | function | ❌ | Navigation callback (accepts `NavigationRequest` or legacy string) |
222
222
  | `size` | string | ❌ | `"compact"`, `"standard"`, or `"large"` |
223
- | `lang` | string | ❌ | Language code (e.g., `"en"`) |
223
+ | `lang` | string | ❌ | Language code (e.g., `"en"`). Your own words: `i18n.forApp` (see [i18n.md](i18n.md)), never `i18n.init` or `i18n.t`, which are the host's |
224
224
  | `translations` | object | ❌ | Translation overrides |
225
225
  | `className` | string | ❌ | CSS class for the wrapper element |
226
226
 
package/dist/docs/i18n.md CHANGED
@@ -16,6 +16,48 @@ Forge's step 21 moves an app off it.
16
16
  3. **The host says which language.** Studio passes the person's own Studio language as `?lang=`, and later
17
17
  changes arrive live (`smartlinks:root-state` with `lang`). A key with no translation shows its English.
18
18
 
19
+ **Where the other languages come from at run time.** Only `en.json` is in your source. The other
20
+ languages are files the platform adds to the published bundle, and the SDK **downloads** the one in use
21
+ (`GET <folder>/i18n/fr.json`) when the app starts or the language changes. It is a static file fetch, not
22
+ an API call. `<folder>` is beside the page by default, or what you gave as `baseUrl` / `moduleUrl`. So when
23
+ a key that has a translation still shows English, ask: **which `fr.json` was fetched, from where, and was it
24
+ yours?** (In the browser's network list, filter on `i18n/`.) A local dev server or stand-in usually can't
25
+ show this fault: there the page and the bundle are the same folder.
26
+
27
+ ## Mounted inside another app's page: use `i18n.forApp`
28
+
29
+ A **container** or **widget** (mounted by Portal or Hub into their own page), or anything else that runs
30
+ inside another app's page, shares that page's SDK. There `i18n.t` reads the **host's** words, and
31
+ `i18n.init` fetches from beside the **host's** page (Portal's own `fr.json`, or its web page), so your keys
32
+ are never found and everything shows in English. Keep your own words instead:
33
+
34
+ ```ts
35
+ // src/i18n.ts — used by your pages, containers and widgets alike
36
+ import { useSyncExternalStore } from 'react'
37
+ import { i18n } from '@proveanything/smartlinks'
38
+ import en from '../public/i18n/en.json'
39
+
40
+ // Your English bundled in; other languages fetched from i18n/ beside YOUR bundle (import.meta.url).
41
+ export const words = i18n.forApp({ catalogs: { en }, moduleUrl: import.meta.url })
42
+ export function useT() {
43
+ useSyncExternalStore(words.subscribe, words.snapshot)
44
+ return words.t
45
+ }
46
+ ```
47
+
48
+ - The **language** stays the page's: the host sets it, and every app's words follow (each loads its own
49
+ catalog before the switch shows). In a container, set it from the host's `lang` prop with
50
+ `i18n.setLanguage(lang)`.
51
+ - `words.ready` resolves once the words for the current language are loaded.
52
+ - `moduleUrl: import.meta.url` finds `i18n/` beside the bundle (`containers.esm.js` at the bundle root; a
53
+ page chunk under `assets/` looks one folder up), then beside the page (a dev server). Pass `baseUrl`
54
+ instead when you know the folder exactly.
55
+ - Don't call `i18n.init` from a container or widget: it changes the host's settings.
56
+ - A container or widget runs on the **host's** SDK, not the one in your `package.json`. A host older than
57
+ 2.2.5 has no `forApp`, and calling it there stops your whole panel. Check first and fall back to your
58
+ bundled English: `typeof i18n.forApp === 'function' ? i18n.forApp(...) : <your English only>`.
59
+ - `forApp` works for a standalone page too, so one `src/i18n.ts` can serve every way the app is shown.
60
+
19
61
  In an admin app, `lang` is the person's **Studio language** (buttons and labels). The site's content
20
62
  language arrives separately as `contentLang`: use it for the language your product data is written in.
21
63
 
package/dist/i18n.d.ts CHANGED
@@ -63,5 +63,52 @@ export declare const format: {
63
63
  /** Minor units as money in the current language: `money(129950, 'EUR')` → "1.299,50 €" in German. */
64
64
  money: (minor: number, currency: string) => string;
65
65
  };
66
+ export interface AppWordsOptions {
67
+ /** This app's English (and any other languages it bundles), keyed by language. */
68
+ catalogs?: Record<string, Catalog>;
69
+ /**
70
+ * The URL of the app's own code: pass `import.meta.url`. Its catalogs are fetched from `i18n/` beside
71
+ * the bundle (a page chunk under `assets/` looks one folder up), so a container mounted in Portal reads
72
+ * its own `fr.json`, not Portal's. In a dev server, where the code isn't in a bundle, the page's own
73
+ * `i18n/` folder is tried next.
74
+ */
75
+ moduleUrl?: string;
76
+ /** Where the catalogs are, when you know exactly (overrides moduleUrl). */
77
+ baseUrl?: string;
78
+ /** The app's source language. Default 'en'. */
79
+ sourceLanguage?: string;
80
+ }
81
+ export interface AppWords {
82
+ /** Loads this app's words for the language in use. Await it (or wait on `snapshot`) before first render. */
83
+ ready: Promise<void>;
84
+ /** Translate with this app's words, in the page's language. Same rules as `i18n.t`. */
85
+ t: typeof t;
86
+ /** Is there a translation for `key` in the language in use (not just the English fallback)? */
87
+ has: (key: string) => boolean;
88
+ /** The page's language (shared by every app on the page). */
89
+ language: () => string;
90
+ /** Language or catalog changes. For useSyncExternalStore. */
91
+ subscribe: (fn: () => void) => () => void;
92
+ /** Changes on every language or catalog change. */
93
+ snapshot: () => number;
94
+ }
95
+ /**
96
+ * One app's own words, kept apart from the page's. Use it in any code that may run inside another app's
97
+ * page: a container or widget mounted by Portal or Hub, a panel in Studio. There the SDK is the host's, so
98
+ * `i18n.t` reads the host's words and `i18n.init` would fetch from beside the host's page.
99
+ *
100
+ * // src/i18n.ts
101
+ * import en from '../public/i18n/en.json'
102
+ * export const words = i18n.forApp({ catalogs: { en }, moduleUrl: import.meta.url })
103
+ * export function useT() {
104
+ * useSyncExternalStore(words.subscribe, words.snapshot)
105
+ * return words.t
106
+ * }
107
+ *
108
+ * The language stays the page's: the host sets it (`i18n.setLanguage`, or its `smartlinks:language`
109
+ * message), and every app's words follow, each loading its own catalog first. Never throws: with nothing
110
+ * found, `t` returns the app's English, then the key.
111
+ */
112
+ export declare function forApp(options?: AppWordsOptions): AppWords;
66
113
  /** Forget loaded catalogs and listeners (tests). */
67
114
  export declare function _reset(): void;
package/dist/i18n.js CHANGED
@@ -23,6 +23,11 @@
23
23
  // }
24
24
  // const t = useT(); t('contacts.add') t('contacts.count', { count: 3 })
25
25
  //
26
+ // An app mounted INSIDE another app's page (a container or widget in Portal, a panel in Studio) shares
27
+ // that page's SDK, so `i18n.t` there reads the HOST's words, fetched from beside the host's page. Such an
28
+ // app keeps its own words with `i18n.forApp` instead: its own catalogs, fetched from its own bundle,
29
+ // following the page's language. See forApp below.
30
+ //
26
31
  // Catalog format (the same shape i18next uses, so files move between tools):
27
32
  // { "contacts": { "add": "Add contact", "count_one": "{{count}} contact", "count_other": "{{count}} contacts" } }
28
33
  // Nested keys are read with dots. `{{name}}` is replaced from the values. With `count`, the plural
@@ -194,7 +199,11 @@ export async function init(options = {}) {
194
199
  }
195
200
  current = normalizeLanguage(options.language) || detectLanguage(options.languages);
196
201
  listen();
197
- await Promise.all([fetchCatalog(source), current === source ? null : fetchCatalog(current)]);
202
+ await Promise.all([
203
+ fetchCatalog(source),
204
+ current === source ? null : fetchCatalog(current),
205
+ ...Array.from(appSets, (set) => fetchAppCatalog(set, current)),
206
+ ]);
198
207
  setPageLanguage(current);
199
208
  emit();
200
209
  }
@@ -203,7 +212,8 @@ export async function setLanguage(lang) {
203
212
  const next = normalizeLanguage(lang);
204
213
  if (!next || next === current)
205
214
  return;
206
- await fetchCatalog(next);
215
+ // The page's words and every app's own (forApp), so the whole page switches in one step.
216
+ await Promise.all([fetchCatalog(next), ...Array.from(appSets, (set) => fetchAppCatalog(set, next))]);
207
217
  current = next;
208
218
  setPageLanguage(current);
209
219
  emit();
@@ -313,6 +323,138 @@ export const format = {
313
323
  /** Minor units as money in the current language: `money(129950, 'EUR')` → "1.299,50 €" in German. */
314
324
  money: (minor, currency) => formatMinor(minor, currency, current),
315
325
  };
326
+ const appSets = new Set();
327
+ const withSlash = (u) => (u.endsWith('/') ? u : `${u}/`);
328
+ /** Where an app's catalogs may be, in order: an exact base, else beside its bundle, else beside the page. */
329
+ function appBases(options) {
330
+ if (options.baseUrl)
331
+ return [withSlash(options.baseUrl)];
332
+ const out = [];
333
+ if (options.moduleUrl) {
334
+ try {
335
+ const folder = new URL('.', options.moduleUrl);
336
+ // Vite puts a page's chunks in assets/; the catalogs are beside the page, one folder up.
337
+ const home = /\/assets\/$/.test(folder.pathname) ? new URL('..', folder) : folder;
338
+ out.push(new URL('i18n/', home).toString());
339
+ }
340
+ catch ( /* not a URL */_a) { /* not a URL */ }
341
+ }
342
+ try {
343
+ out.push(new URL('i18n/', document.baseURI).toString());
344
+ }
345
+ catch (_b) {
346
+ if (!out.length)
347
+ out.push('i18n/');
348
+ }
349
+ return Array.from(new Set(out));
350
+ }
351
+ async function fetchAppCatalog(set, lang) {
352
+ const known = (name) => set.supplied.has(name) || set.loaded.has(name);
353
+ if (candidates(lang).some(known))
354
+ return;
355
+ const pending = set.fetching.get(lang);
356
+ if (pending)
357
+ return pending;
358
+ const job = (async () => {
359
+ for (const name of candidates(lang)) {
360
+ for (const at of set.bases) {
361
+ try {
362
+ const res = await fetch(`${at}${name}.json`, { cache: 'default' });
363
+ if (!res.ok)
364
+ continue;
365
+ const type = res.headers.get('content-type') || '';
366
+ if (type && !type.includes('json'))
367
+ continue; // a host's index page isn't a catalog
368
+ const data = (await res.json());
369
+ if (data && typeof data === 'object') {
370
+ set.loaded.set(name, data);
371
+ return;
372
+ }
373
+ }
374
+ catch ( /* not there, or offline: try the next place */_a) { /* not there, or offline: try the next place */ }
375
+ }
376
+ }
377
+ })().finally(() => { set.fetching.delete(lang); });
378
+ set.fetching.set(lang, job);
379
+ return job;
380
+ }
381
+ function appCatalogFor(set, lang) {
382
+ for (const name of candidates(lang)) {
383
+ const cat = set.supplied.get(name) || set.loaded.get(name);
384
+ if (cat)
385
+ return cat;
386
+ }
387
+ return undefined;
388
+ }
389
+ function appFind(set, lang, key, count) {
390
+ var _a;
391
+ const cat = appCatalogFor(set, lang);
392
+ if (typeof count === 'number') {
393
+ const form = (_a = lookup(cat, `${key}_${pluralCategory(lang, count)}`)) !== null && _a !== void 0 ? _a : lookup(cat, `${key}_other`);
394
+ if (form !== undefined)
395
+ return form;
396
+ }
397
+ return lookup(cat, key);
398
+ }
399
+ function fill(text, values) {
400
+ if (!values)
401
+ return text;
402
+ return text.replace(/\{\{\s*([\w.]+)\s*\}\}/g, (m, name) => {
403
+ const v = values[name];
404
+ if (v === undefined || v === null)
405
+ return m;
406
+ return typeof v === 'number' ? v.toLocaleString(current) : String(v);
407
+ });
408
+ }
409
+ /**
410
+ * One app's own words, kept apart from the page's. Use it in any code that may run inside another app's
411
+ * page: a container or widget mounted by Portal or Hub, a panel in Studio. There the SDK is the host's, so
412
+ * `i18n.t` reads the host's words and `i18n.init` would fetch from beside the host's page.
413
+ *
414
+ * // src/i18n.ts
415
+ * import en from '../public/i18n/en.json'
416
+ * export const words = i18n.forApp({ catalogs: { en }, moduleUrl: import.meta.url })
417
+ * export function useT() {
418
+ * useSyncExternalStore(words.subscribe, words.snapshot)
419
+ * return words.t
420
+ * }
421
+ *
422
+ * The language stays the page's: the host sets it (`i18n.setLanguage`, or its `smartlinks:language`
423
+ * message), and every app's words follow, each loading its own catalog first. Never throws: with nothing
424
+ * found, `t` returns the app's English, then the key.
425
+ */
426
+ export function forApp(options = {}) {
427
+ const set = {
428
+ bases: appBases(options),
429
+ source: normalizeLanguage(options.sourceLanguage) || 'en',
430
+ supplied: new Map(),
431
+ loaded: new Map(),
432
+ fetching: new Map(),
433
+ };
434
+ if (options.catalogs)
435
+ for (const [lang, cat] of Object.entries(options.catalogs)) {
436
+ set.supplied.set(normalizeLanguage(lang) || lang, cat);
437
+ }
438
+ appSets.add(set);
439
+ listen();
440
+ const ready = Promise.all([fetchAppCatalog(set, set.source), fetchAppCatalog(set, current)])
441
+ .then(() => { emit(); })
442
+ .catch(() => undefined);
443
+ const appT = (key, values) => {
444
+ var _a, _b, _c;
445
+ const count = values && typeof values.count === 'number' ? values.count : undefined;
446
+ const text = (_c = (_b = (_a = appFind(set, current, key, count)) !== null && _a !== void 0 ? _a : (current === set.source ? undefined : appFind(set, set.source, key, count))) !== null && _b !== void 0 ? _b : values === null || values === void 0 ? void 0 : values.defaultValue) !== null && _c !== void 0 ? _c : key;
447
+ return fill(text, values);
448
+ };
449
+ return {
450
+ ready,
451
+ t: appT,
452
+ has: (key) => appFind(set, current, key) !== undefined,
453
+ language,
454
+ subscribe,
455
+ snapshot,
456
+ };
457
+ }
316
458
  /** Forget loaded catalogs and listeners (tests). */
317
459
  export function _reset() {
318
460
  source = 'en';
@@ -321,5 +463,6 @@ export function _reset() {
321
463
  loaded.clear();
322
464
  supplied.clear();
323
465
  listeners.clear();
466
+ appSets.clear();
324
467
  version = 0;
325
468
  }
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "2.2.4";
1
+ export declare const SDK_VERSION = "2.2.6";
package/dist/version.js CHANGED
@@ -1,2 +1,2 @@
1
1
  // Generated by scripts/write-version.mjs from package.json. Do not edit.
2
- export const SDK_VERSION = "2.2.4";
2
+ export const SDK_VERSION = "2.2.6";
@@ -1,6 +1,6 @@
1
1
  # Smartlinks API Summary
2
2
 
3
- Version: 2.2.4 | Generated: 2026-12-02T15:08:47.460Z
3
+ Version: 2.2.6 | Generated: 2026-12-04T13:25:02.911Z
4
4
 
5
5
  This is a concise summary of all available API functions and types.
6
6
 
@@ -220,7 +220,7 @@ All standard widget props apply:
220
220
  | `tag` | `TagContext` | ❌ | **Deprecated** — use `itemContext.tag` instead. Kept for one release. |
221
221
  | `onNavigate` | function | ❌ | Navigation callback (accepts `NavigationRequest` or legacy string) |
222
222
  | `size` | string | ❌ | `"compact"`, `"standard"`, or `"large"` |
223
- | `lang` | string | ❌ | Language code (e.g., `"en"`) |
223
+ | `lang` | string | ❌ | Language code (e.g., `"en"`). Your own words: `i18n.forApp` (see [i18n.md](i18n.md)), never `i18n.init` or `i18n.t`, which are the host's |
224
224
  | `translations` | object | ❌ | Translation overrides |
225
225
  | `className` | string | ❌ | CSS class for the wrapper element |
226
226
 
package/docs/i18n.md CHANGED
@@ -16,6 +16,48 @@ Forge's step 21 moves an app off it.
16
16
  3. **The host says which language.** Studio passes the person's own Studio language as `?lang=`, and later
17
17
  changes arrive live (`smartlinks:root-state` with `lang`). A key with no translation shows its English.
18
18
 
19
+ **Where the other languages come from at run time.** Only `en.json` is in your source. The other
20
+ languages are files the platform adds to the published bundle, and the SDK **downloads** the one in use
21
+ (`GET <folder>/i18n/fr.json`) when the app starts or the language changes. It is a static file fetch, not
22
+ an API call. `<folder>` is beside the page by default, or what you gave as `baseUrl` / `moduleUrl`. So when
23
+ a key that has a translation still shows English, ask: **which `fr.json` was fetched, from where, and was it
24
+ yours?** (In the browser's network list, filter on `i18n/`.) A local dev server or stand-in usually can't
25
+ show this fault: there the page and the bundle are the same folder.
26
+
27
+ ## Mounted inside another app's page: use `i18n.forApp`
28
+
29
+ A **container** or **widget** (mounted by Portal or Hub into their own page), or anything else that runs
30
+ inside another app's page, shares that page's SDK. There `i18n.t` reads the **host's** words, and
31
+ `i18n.init` fetches from beside the **host's** page (Portal's own `fr.json`, or its web page), so your keys
32
+ are never found and everything shows in English. Keep your own words instead:
33
+
34
+ ```ts
35
+ // src/i18n.ts — used by your pages, containers and widgets alike
36
+ import { useSyncExternalStore } from 'react'
37
+ import { i18n } from '@proveanything/smartlinks'
38
+ import en from '../public/i18n/en.json'
39
+
40
+ // Your English bundled in; other languages fetched from i18n/ beside YOUR bundle (import.meta.url).
41
+ export const words = i18n.forApp({ catalogs: { en }, moduleUrl: import.meta.url })
42
+ export function useT() {
43
+ useSyncExternalStore(words.subscribe, words.snapshot)
44
+ return words.t
45
+ }
46
+ ```
47
+
48
+ - The **language** stays the page's: the host sets it, and every app's words follow (each loads its own
49
+ catalog before the switch shows). In a container, set it from the host's `lang` prop with
50
+ `i18n.setLanguage(lang)`.
51
+ - `words.ready` resolves once the words for the current language are loaded.
52
+ - `moduleUrl: import.meta.url` finds `i18n/` beside the bundle (`containers.esm.js` at the bundle root; a
53
+ page chunk under `assets/` looks one folder up), then beside the page (a dev server). Pass `baseUrl`
54
+ instead when you know the folder exactly.
55
+ - Don't call `i18n.init` from a container or widget: it changes the host's settings.
56
+ - A container or widget runs on the **host's** SDK, not the one in your `package.json`. A host older than
57
+ 2.2.5 has no `forApp`, and calling it there stops your whole panel. Check first and fall back to your
58
+ bundled English: `typeof i18n.forApp === 'function' ? i18n.forApp(...) : <your English only>`.
59
+ - `forApp` works for a standalone page too, so one `src/i18n.ts` can serve every way the app is shown.
60
+
19
61
  In an admin app, `lang` is the person's **Studio language** (buttons and labels). The site's content
20
62
  language arrives separately as `contentLang`: use it for the language your product data is written in.
21
63
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@proveanything/smartlinks",
3
- "version": "2.2.4",
3
+ "version": "2.2.6",
4
4
  "description": "Official JavaScript/TypeScript SDK for the Smartlinks API",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",