@basaltkit/deploy-ui 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -64,6 +64,7 @@ repository must not be able to repoint it. The file holds no secret.
64
64
  {
65
65
  "version": 1,
66
66
  "actor": "alice",
67
+ "locale": "pt",
67
68
  "targets": [
68
69
  { "name": "staging", "ssh": "deploy@203.0.113.10", "dir": "/srv/myapp" },
69
70
  { "name": "production", "ssh": "myapp-prod", "port": 2222, "dir": "/srv/myapp", "node": "/usr/local/bin/node", "protected": true }
@@ -81,6 +82,7 @@ repository must not be able to repoint it. The file holds no secret.
81
82
  | `config` | optional `--config` path, relative to the checkout |
82
83
  | `protected` | deploy and rollback need the target's name typed |
83
84
  | `actor` | recorded with each release (`--actor`); default: your OS user name |
85
+ | `locale` | the page's language, `en` or `pt`, as you last picked it; default: your browser's language |
84
86
 
85
87
  Every field has a closed grammar (no leading `-`, no spaces, no `..`); an
86
88
  unknown field is refused, not ignored. The UI refuses a targets file that is a
@@ -93,7 +95,7 @@ since then, nothing is sent, and the page shows the new values.
93
95
 
94
96
  ## What it does
95
97
 
96
- Per target: **check the connection**; **status** (current release, process,
98
+ Per target: **check the connection** (which also names the app and where its code comes from); **status** (current release, process,
97
99
  lock, active and last run, a hold); **plan** (findings, what a run would do, the
98
100
  questions it would ask, and the commits that will go out); the **`.env` form** (secrets in password fields, never
99
101
  stored, emptied after sending); **deploy** — detached by default, so a closed
@@ -107,6 +109,15 @@ with its subject and, one click away, the commits it brought in), with the
107
109
  commits that would leave production listed before you confirm; and
108
110
  "Write .env only" for a first setup.
109
111
 
112
+ When the server's `@basaltkit/deploy` is older than a feature the page uses,
113
+ the version in the target bar turns amber. **Status** and **Check
114
+ connection** also list what is missing, give the exact `pnpm add -D` for the
115
+ app, and say that the first deploy still runs the old version.
116
+
117
+ The page speaks **English and Portuguese**: your browser's language until you
118
+ pick one in the sidebar, then the one kept in the targets file. Only its own
119
+ words are translated; what the server's deploy prints is shown as it came.
120
+
110
121
  Each action is one `ssh` invocation of the app's own, lockfile-pinned
111
122
  `node node_modules/@basaltkit/deploy/dist/bin.js <command> --json=1`, with
112
123
  `--via=ui` so the release record says how it was made. `install`, `bootstrap`
@@ -1,3 +1,4 @@
1
+ import { t } from './i18n.js';
1
2
  /**
2
3
  * The page's calls to its local server: POST, JSON, the session token as a
3
4
  * bearer header (never a cookie: cookies are not port-scoped). Streams are
@@ -112,7 +113,7 @@ export class Api {
112
113
  const crypto = this.options.crypto;
113
114
  const serverKey = this.options.serverKey;
114
115
  if (!serverKey || !crypto?.subtle)
115
- throw this.stop('unverified', 'this page cannot check its local server (no server key, or no WebCrypto): nothing is sent');
116
+ throw this.stop('unverified', t('api.cannotCheck'));
116
117
  const nonce = toBase64Url(crypto.getRandomValues(new Uint8Array(32)));
117
118
  let proof;
118
119
  try {
@@ -121,7 +122,7 @@ export class Api {
121
122
  catch (error) {
122
123
  if (error instanceof ServerGone)
123
124
  throw error;
124
- throw this.stop('unverified', 'the local server refused to prove it is this UI: it is not the one this page was opened from');
125
+ throw this.stop('unverified', t('api.refusedProof'));
125
126
  }
126
127
  let valid = false;
127
128
  if (typeof proof === 'string' && /^[A-Za-z0-9_-]{43}$/.test(proof)) {
@@ -129,7 +130,7 @@ export class Api {
129
130
  valid = await crypto.subtle.verify('HMAC', key, fromBase64Url(proof), new TextEncoder().encode(`${PROOF_CONTEXT}${nonce}`));
130
131
  }
131
132
  if (!valid)
132
- throw this.stop('unverified', 'the local server could not prove it is this UI: another program may be listening on its port');
133
+ throw this.stop('unverified', t('api.wrongProof'));
133
134
  }
134
135
  /** A call; one that carries answers checks the server first. */
135
136
  async post(route, body = {}) {
@@ -171,7 +172,7 @@ export class Api {
171
172
  // The page's own bug is not the server going away.
172
173
  if (handlerFailed || signal?.aborted)
173
174
  throw error;
174
- throw this.stop('unreachable', 'the deploy UI stopped while this stream was open');
175
+ throw this.stop('unreachable', t('api.streamStopped'));
175
176
  }
176
177
  }
177
178
  async send(route, body) {
@@ -190,7 +191,7 @@ export class Api {
190
191
  catch (error) {
191
192
  if (signal?.aborted)
192
193
  throw error;
193
- throw this.stop('unreachable', 'the deploy UI is not running any more (it stopped after its idle time, from Quit, or with Ctrl-C)');
194
+ throw this.stop('unreachable', t('api.notRunning'));
194
195
  }
195
196
  }
196
197
  stop(why, message) {
@@ -1,6 +1,7 @@
1
1
  import { ApiError } from './api.js';
2
2
  import { confirmDialog } from './dialog.js';
3
3
  import { button, el, emptyState, quiet, replace } from './dom.js';
4
+ import { getLocale, isLocale, LOCALES, setLocale, t } from './i18n.js';
4
5
  import { icon } from './icons.js';
5
6
  import { describe } from './reply.js';
6
7
  import { renderHistory } from './view-history.js';
@@ -8,6 +9,7 @@ import { renderPlan } from './view-plan.js';
8
9
  import { RunView } from './view-run.js';
9
10
  import { renderStatus } from './view-status.js';
10
11
  import { renderTargets } from './view-targets.js';
12
+ import { behind, sourceText, validSource } from './version.js';
11
13
  /** This tab's session (sessionStorage): the bearer token, and the key the server proves itself with. */
12
14
  export const TOKEN_KEY = 'basalt-deploy-ui:token';
13
15
  export const SERVER_KEY = 'basalt-deploy-ui:server-key';
@@ -21,7 +23,20 @@ export function forgetSession(storage) {
21
23
  // No storage: nothing kept.
22
24
  }
23
25
  }
24
- const SCREENS = [['status', 'Status', 'status'], ['plan', 'Plan & deploy', 'plan'], ['history', 'History', 'history'], ['run', 'Run', 'run'], ['targets', 'Targets', 'targets']];
26
+ const SCREENS = [['status', 'screen.status', 'status'], ['plan', 'screen.plan', 'plan'], ['history', 'screen.history', 'history'], ['run', 'screen.run', 'run'], ['targets', 'screen.targets', 'targets']];
27
+ /**
28
+ * The page's fixed shell (page.ts) in the current language: the document's
29
+ * `lang`, the screens' landmark and the local-only note.
30
+ */
31
+ export function applyShell(doc) {
32
+ doc.documentElement.lang = getLocale();
33
+ doc.getElementById('nav')?.setAttribute('aria-label', t('shell.screens'));
34
+ const note = doc.querySelector('.local-note');
35
+ note?.setAttribute('title', t('shell.localOnlyTitle'));
36
+ const label = doc.querySelector('.local-label');
37
+ if (label)
38
+ replace(label, t('shell.localOnly'));
39
+ }
25
40
  /** Per-target state the screens render; every server string in it is shown as text only. */
26
41
  export class App {
27
42
  api;
@@ -58,7 +73,7 @@ export class App {
58
73
  this.stopped = true;
59
74
  this.run?.stop();
60
75
  forgetSession(this.storage);
61
- replace(this.shell.main, el('div', { class: 'stopped' }, emptyState('power', 'Disconnected', 'This page sends nothing from here on.'), el('div', { class: 'banner err', role: 'alert' }, icon('error', 'icon banner-icon'), el('div', { class: 'banner-body' }, el('strong', { class: 'banner-title' }, error.why === 'unverified' ? 'This page no longer talks to your deploy UI.' : 'The deploy UI has stopped.'), el('p', {}, error.message), el('p', {}, 'Nothing was sent. Detached runs go on on the server. Start basalt-deploy-ui again and open the URL it prints.')))));
76
+ replace(this.shell.main, el('div', { class: 'stopped' }, emptyState('power', t('gone.title'), t('gone.sendsNothing')), el('div', { class: 'banner err', role: 'alert' }, icon('error', 'icon banner-icon'), el('div', { class: 'banner-body' }, el('strong', { class: 'banner-title' }, error.why === 'unverified' ? t('gone.unverified') : t('gone.stopped')), el('p', {}, error.message), el('p', {}, t('gone.help'))))));
62
77
  replace(this.shell.nav);
63
78
  this.clearBar();
64
79
  replace(this.shell.session);
@@ -70,8 +85,10 @@ export class App {
70
85
  return this.planReply?.result;
71
86
  }
72
87
  async start() {
73
- replace(this.shell.session, el('button', { type: 'button', class: 'ghost danger', on: { click: () => { void this.quit(); } } }, icon('power'), 'Quit'));
74
88
  await this.loadTargets();
89
+ if (this.targets.locale && this.targets.locale !== getLocale())
90
+ this.useLocale(this.targets.locale);
91
+ this.renderSession();
75
92
  const first = this.targets.targets[0];
76
93
  if (first) {
77
94
  this.select(first.name);
@@ -81,6 +98,37 @@ export class App {
81
98
  else
82
99
  this.show('targets');
83
100
  }
101
+ /** The switcher and Quit, at the foot of the sidebar. */
102
+ renderSession() {
103
+ const select = el('select', { id: 'locale-select', aria: { label: t('language') }, on: { change: (event) => {
104
+ const value = event.target.value;
105
+ if (isLocale(value))
106
+ void this.chooseLocale(value);
107
+ } } }, ...LOCALES.map((locale) => {
108
+ const option = el('option', { value: locale }, t(`language.${locale}`));
109
+ option.lang = locale;
110
+ option.selected = locale === getLocale();
111
+ return option;
112
+ }));
113
+ replace(this.shell.session, el('div', { class: 'locale' }, icon('globe'), select), el('button', { type: 'button', class: 'ghost danger', on: { click: () => { void this.quit(); } } }, icon('power'), t('quit')));
114
+ }
115
+ /** Every screen again in another language; nothing is asked of the server. */
116
+ useLocale(locale) {
117
+ setLocale(locale);
118
+ applyShell(this.shell.main.ownerDocument);
119
+ }
120
+ /** The operator's choice: the page switches at once, and the targets file keeps it for the next start (best effort). */
121
+ async chooseLocale(locale) {
122
+ this.useLocale(locale);
123
+ this.renderSession();
124
+ this.refresh();
125
+ try {
126
+ this.targets = await this.api.post('targets/save', { locale });
127
+ }
128
+ catch {
129
+ // Not saved: this tab still speaks it.
130
+ }
131
+ }
84
132
  async loadTargets() {
85
133
  this.targets = await this.api.post('targets');
86
134
  if (this.current && !this.targets.targets.some((t) => t.name === this.current?.name))
@@ -118,30 +166,30 @@ export class App {
118
166
  if (screen === 'targets')
119
167
  return replace(main, renderTargets(this));
120
168
  if (!this.current)
121
- return replace(main, emptyState('targets', 'No target selected', 'Add a target first.', el('span', { class: 'row' }, button('Targets', () => this.show('targets'), { class: 'primary' }))));
169
+ return replace(main, emptyState('targets', t('app.noTarget'), t('app.addTargetFirst'), el('span', { class: 'row' }, button(t('screen.targets'), () => this.show('targets'), { class: 'primary' }))));
122
170
  if (screen === 'status')
123
171
  return replace(main, renderStatus(this));
124
172
  if (screen === 'plan')
125
173
  return replace(main, renderPlan(this));
126
174
  if (screen === 'history')
127
175
  return replace(main, renderHistory(this));
128
- replace(main, this.run ? this.run.element : el('section', { aria: { label: 'Run' } }, emptyState('run', 'Nothing running here', 'No run in this tab yet: start one from Plan & deploy, or attach from Status.')));
176
+ replace(main, this.run ? this.run.element : el('section', { aria: { label: t('screen.run') } }, emptyState('run', t('app.nothingRunning'), t('app.nothingRunningText'))));
129
177
  }
130
178
  /** Re-renders the current screen (after a state change). */
131
179
  refresh() {
132
180
  this.show(this.screen);
133
181
  }
134
182
  renderNav() {
135
- const select = el('select', { id: 'target-select', aria: { label: 'Target' }, on: { change: (event) => { this.select(event.target.value); this.refresh(); if (this.current)
183
+ const select = el('select', { id: 'target-select', aria: { label: t('target') }, on: { change: (event) => { this.select(event.target.value); this.refresh(); if (this.current)
136
184
  void this.refreshStatus(); } } }, ...this.targets.targets.map((t) => {
137
185
  const option = el('option', { value: t.name }, t.name);
138
186
  option.selected = t.name === this.current?.name;
139
187
  return option;
140
188
  }));
141
- replace(this.shell.nav, this.targets.targets.length > 0 ? el('div', { class: 'nav-target' }, el('label', { class: 'section-label', for: 'target-select' }, 'Target'), select) : null, el('div', { class: 'nav-list' }, ...SCREENS.map(([screen, label, name]) => el('button', {
189
+ replace(this.shell.nav, this.targets.targets.length > 0 ? el('div', { class: 'nav-target' }, el('label', { class: 'section-label', for: 'target-select' }, t('target')), select) : null, el('div', { class: 'nav-list' }, ...SCREENS.map(([screen, label, name]) => el('button', {
142
190
  type: 'button', class: 'nav-item', ...(screen === this.screen ? { aria: { current: 'page' } } : {}),
143
191
  on: { click: () => this.show(screen) },
144
- }, icon(name), label))));
192
+ }, icon(name), t(label)))));
145
193
  }
146
194
  /**
147
195
  * "Am I about to deploy production?": name, destination, the server's host
@@ -149,29 +197,30 @@ export class App {
149
197
  * screen readers and copy, drawn as rules.
150
198
  */
151
199
  renderBar() {
152
- const t = this.current;
153
- if (!t)
200
+ const target = this.current;
201
+ if (!target)
154
202
  return this.clearBar();
155
203
  const status = this.status;
204
+ const source = validSource(status?.source);
156
205
  const sep = () => el('span', { class: 'ctx-sep', aria: { hidden: 'true' } }, ' · ');
157
- const item = (name, value, label) => [sep(),
158
- el('span', { class: 'ctx-item' }, icon(name), label ? el('span', { class: 'ctx-label' }, label) : null, label ? ' ' : null, el('span', { class: 'ctx-val' }, value))];
159
- this.shell.bar.classList.toggle('is-protected', t.protected === true);
160
- replace(this.shell.bar, el('span', { class: 'ctx-target' }, el('strong', { class: 'ctx-name' }, t.name), t.protected ? el('span', { class: 'badge badge-protected', title: 'Deploy and rollback ask for the name to be typed' }, icon('shield'), quiet(' '), 'PROTECTED') : null), ...item('terminal', `${t.ssh}${t.port ? `:${t.port}` : ''}`), ...item('folder', t.dir), ...(this.hello ? [...item('server', this.hello.host, 'host'), ...item('tag', this.hello.version, 'deploy')] : []), ...(status ? item('app', status.app, 'app') : []));
206
+ const item = (name, value, label, extra = '') => [sep(),
207
+ el('span', { class: `ctx-item${extra}` }, icon(name), label ? el('span', { class: 'ctx-label' }, label) : null, label ? ' ' : null, el('span', { class: 'ctx-val' }, value))];
208
+ this.shell.bar.classList.toggle('is-protected', target.protected === true);
209
+ replace(this.shell.bar, el('span', { class: 'ctx-target' }, el('strong', { class: 'ctx-name' }, target.name), target.protected ? el('span', { class: 'badge badge-protected', title: t('protected.title') }, icon('shield'), quiet(' '), t('protected.badge')) : null), ...item('terminal', `${target.ssh}${target.port ? `:${target.port}` : ''}`), ...item('folder', target.dir), ...(this.hello ? [...item('server', this.hello.host, t('ctx.host')), ...item('tag', this.hello.version, t('ctx.deploy'), behind(this.hello.version) ? ' is-behind' : '')] : []), ...(status ? item('app', status.app, t('ctx.app')) : []), ...(source ? item('branch', sourceText(source), t('ctx.source')) : []));
161
210
  }
162
211
  clearBar() {
163
212
  this.shell.bar.classList.remove('is-protected');
164
213
  replace(this.shell.bar);
165
214
  }
166
215
  async refreshStatus() {
167
- const t = this.current;
168
- if (!t)
216
+ const target = this.current;
217
+ if (!target)
169
218
  return;
170
219
  this.statusBusy = true;
171
220
  if (this.screen === 'status')
172
221
  this.refresh();
173
222
  try {
174
- this.statusReply = await this.api.post('status', { target: t.name });
223
+ this.statusReply = await this.api.post('status', { target: target.name });
175
224
  if (this.statusReply.hello)
176
225
  this.hello = this.statusReply.hello;
177
226
  }
@@ -181,18 +230,18 @@ export class App {
181
230
  finally {
182
231
  this.statusBusy = false;
183
232
  }
184
- if (this.current?.name === t.name)
233
+ if (this.current?.name === target.name)
185
234
  this.refresh();
186
235
  }
187
236
  async runPlan() {
188
- const t = this.current;
189
- if (!t)
237
+ const target = this.current;
238
+ if (!target)
190
239
  return;
191
240
  this.planBusy = true;
192
241
  this.planReply = undefined;
193
242
  this.refresh();
194
243
  try {
195
- this.planReply = await this.api.post('plan', { target: t.name });
244
+ this.planReply = await this.api.post('plan', { target: target.name });
196
245
  if (this.planReply.hello)
197
246
  this.hello = this.planReply.hello;
198
247
  }
@@ -202,16 +251,16 @@ export class App {
202
251
  finally {
203
252
  this.planBusy = false;
204
253
  }
205
- if (this.current?.name === t.name)
254
+ if (this.current?.name === target.name)
206
255
  this.refresh();
207
256
  }
208
257
  /** The commits between two releases of the current target (read-only on the server). */
209
258
  commits(from, to) {
210
- const t = this.current;
211
- const key = `${t.name}\u0000${from}\u0000${to}`;
259
+ const target = this.current;
260
+ const key = `${target.name}\u0000${from}\u0000${to}`;
212
261
  let reply = this.ranges.get(key);
213
262
  if (!reply) {
214
- reply = this.api.post('commits', { target: t.name, from, to });
263
+ reply = this.api.post('commits', { target: target.name, from, to });
215
264
  // A failure is not remembered: the next click asks again.
216
265
  reply.then((r) => { if (!r.ok)
217
266
  this.ranges.delete(key); }, () => this.ranges.delete(key));
@@ -221,8 +270,8 @@ export class App {
221
270
  }
222
271
  /** What a rollback would take out of production, now (never cached: the releases move). */
223
272
  rollbackPreview(to) {
224
- const t = this.current;
225
- return this.api.post('commits', { target: t.name, rollback: true, ...(to ? { to } : {}) });
273
+ const target = this.current;
274
+ return this.api.post('commits', { target: target.name, rollback: true, ...(to ? { to } : {}) });
226
275
  }
227
276
  /** A run to watch: a new view, attached from the seq this tab last saw (or 0). */
228
277
  watch(runId, title, fromStart = false) {
@@ -231,8 +280,7 @@ export class App {
231
280
  return this.run;
232
281
  }
233
282
  this.run?.stop();
234
- const t = this.current;
235
- this.run = new RunView(this, t, title, runId);
283
+ this.run = new RunView(this, this.current, title, runId);
236
284
  this.show('run');
237
285
  void this.run.attach(fromStart ? 0 : this.run.resumeSeq());
238
286
  return this.run;
@@ -240,8 +288,7 @@ export class App {
240
288
  /** An attended stream (env, an attended run): it stops if this page closes. */
241
289
  attended(title, route, body) {
242
290
  this.run?.stop();
243
- const t = this.current;
244
- this.run = new RunView(this, t, title, undefined);
291
+ this.run = new RunView(this, this.current, title, undefined);
245
292
  this.show('run');
246
293
  void this.run.streamAttended(route, body);
247
294
  return this.run;
@@ -249,12 +296,9 @@ export class App {
249
296
  async quit() {
250
297
  const watching = this.run?.following === true;
251
298
  const ok = await confirmDialog({
252
- title: 'Quit the deploy UI?',
253
- lines: [
254
- 'The local server stops and this page stops working.',
255
- watching ? 'A run is being watched: detached runs go on on the server (re-attach later); an attended one stops at its next step.' : 'Detached runs go on on the server.',
256
- ],
257
- confirmLabel: 'Quit',
299
+ title: t('quit.title'),
300
+ lines: [t('quit.line'), watching ? t('quit.watching') : t('quit.detached')],
301
+ confirmLabel: t('quit'),
258
302
  });
259
303
  if (!ok)
260
304
  return;
@@ -267,7 +311,7 @@ export class App {
267
311
  }
268
312
  this.stopped = true;
269
313
  forgetSession(this.storage);
270
- replace(this.shell.main, el('div', { class: 'stopped' }, emptyState('power', 'Stopped', 'The deploy UI has stopped. You can close this tab.', 'Detached runs go on on the server; start basalt-deploy-ui again to re-attach.')));
314
+ replace(this.shell.main, el('div', { class: 'stopped' }, emptyState('power', t('quit.stopped'), t('quit.stoppedText'), t('quit.stoppedHelp'))));
271
315
  replace(this.shell.nav);
272
316
  this.clearBar();
273
317
  replace(this.shell.session);
@@ -1,4 +1,5 @@
1
1
  import { append, button, el } from './dom.js';
2
+ import { t } from './i18n.js';
2
3
  import { icon } from './icons.js';
3
4
  /**
4
5
  * A native modal `<dialog>` (focus stays inside, Escape cancels). Resolves
@@ -18,7 +19,7 @@ export function confirmDialog(options, host = document.body) {
18
19
  resolve(answer);
19
20
  };
20
21
  const confirm = button(options.confirmLabel, () => finish(true), { class: options.danger ? 'danger' : 'primary' });
21
- const dismiss = button('Cancel', () => finish(false));
22
+ const dismiss = button(t('cancel'), () => finish(false));
22
23
  let typed;
23
24
  if (options.typeToConfirm !== undefined) {
24
25
  const expected = options.typeToConfirm;
@@ -28,7 +29,7 @@ export function confirmDialog(options, host = document.body) {
28
29
  on: { input: () => { confirm.disabled = typed.value !== expected; } },
29
30
  });
30
31
  }
31
- append(dialog, el('div', { class: 'dialog-head' }, el('span', { class: 'dialog-icon' }, icon(options.danger ? 'alert' : 'info')), el('h2', { id: 'confirm-title' }, options.title)), el('div', { class: 'dialog-body' }, options.context ? el('p', { class: 'dialog-context' }, options.context) : null, options.typeToConfirm !== undefined ? el('p', { class: 'dialog-protected' }, el('span', { class: 'badge badge-protected' }, icon('shield'), 'PROTECTED'), ' This target is protected.') : null, ...options.lines.filter((line) => line !== null && line !== undefined && line !== false && line !== '').map((line) => el('p', {}, line)), typed ? el('div', { class: 'field' }, el('label', { for: 'confirm-name' }, 'Type ', el('code', {}, options.typeToConfirm), ' to confirm'), typed) : null), el('div', { class: 'dialog-actions' }, dismiss, confirm));
32
+ append(dialog, el('div', { class: 'dialog-head' }, el('span', { class: 'dialog-icon' }, icon(options.danger ? 'alert' : 'info')), el('h2', { id: 'confirm-title' }, options.title)), el('div', { class: 'dialog-body' }, options.context ? el('p', { class: 'dialog-context' }, options.context) : null, options.typeToConfirm !== undefined ? el('p', { class: 'dialog-protected' }, el('span', { class: 'badge badge-protected' }, icon('shield'), t('protected.badge')), t('dialog.protected')) : null, ...options.lines.filter((line) => line !== null && line !== undefined && line !== false && line !== '').map((line) => el('p', {}, line)), typed ? el('div', { class: 'field' }, el('label', { for: 'confirm-name' }, t('dialog.typeTo'), el('code', {}, options.typeToConfirm), t('dialog.toConfirm')), typed) : null), el('div', { class: 'dialog-actions' }, dismiss, confirm));
32
33
  dialog.addEventListener('cancel', (event) => { event.preventDefault(); finish(false); });
33
34
  dialog.addEventListener('keydown', (event) => { if (event.key === 'Escape')
34
35
  finish(false); });
@@ -0,0 +1,14 @@
1
+ /** Identity helper that pins a catalog's key/value types for `t()`. */
2
+ export function defineMessages(messages) {
3
+ return messages;
4
+ }
5
+ const interpolate = (template, params) => template.replace(/\{(\w+)\}/g, (_match, key) => params[key] === undefined ? `{${key}}` : String(params[key]));
6
+ /** Resolves a message value to a string for a locale, applying plurals + params. */
7
+ export function render(value, locale, params) {
8
+ if (typeof value === 'string')
9
+ return interpolate(value, params);
10
+ const count = Number(params['count'] ?? 0);
11
+ const category = new Intl.PluralRules(locale).select(count);
12
+ const template = value[category] ?? value.other ?? '';
13
+ return interpolate(template, params);
14
+ }