@juicesharp/rpiv-i18n 1.1.4 → 1.1.5
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 +45 -45
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -15,15 +15,15 @@ i18n/localization SDK for Pi extensions. Pick a UI language interactively or via
|
|
|
15
15
|
|
|
16
16
|
## Features
|
|
17
17
|
|
|
18
|
-
- **One shared locale dial** for every Pi extension that adopts the SDK
|
|
18
|
+
- **One shared locale dial** for every Pi extension that adopts the SDK - `/languages` switches them all at once.
|
|
19
19
|
- **`/languages` slash command** with an interactive picker; persists the choice to `~/.config/rpiv-i18n/locale.json` (chmod `0600`) and reports a clear error if disk persistence fails.
|
|
20
20
|
- **`--locale <code>` CLI flag** for one-shot or scripted launches.
|
|
21
21
|
- **Auto-detects** `process.env.LANG` / `LC_ALL` so most Unix users get a localized UI without configuration.
|
|
22
|
-
- **Tiny SDK surface for authors**
|
|
23
|
-
- **Ships picker entries for** German, English, Spanish, French, Portuguese (European), Portuguese (Brazilian), Russian, and Ukrainian
|
|
24
|
-
- **Live propagation**
|
|
25
|
-
- **Always-safe fallback**
|
|
26
|
-
- **Zero-import escape hatch**
|
|
22
|
+
- **Tiny SDK surface for authors** - `registerStrings(namespace, byLocale)`, `scope(namespace)`, `tr(namespace, key, fallback)`. Render-time lookups, English fallback per missing key, no module-init baking.
|
|
23
|
+
- **Ships picker entries for** German, English, Spanish, French, Portuguese (European), Portuguese (Brazilian), Russian, and Ukrainian - alphabetical by locale code (consumers contribute their own translation maps; the SDK ships infrastructure, not strings).
|
|
24
|
+
- **Live propagation** - locale changes via `/languages` apply to the next render with no restart, even when the SDK is loaded as multiple module instances.
|
|
25
|
+
- **Always-safe fallback** - if the SDK isn't installed at all, every `tr(...)` call returns the consumer's literal English fallback. Extensions stay usable.
|
|
26
|
+
- **Zero-import escape hatch** - `globalThis[Symbol.for("rpiv-i18n")]` exposes a frozen `{ locale, namespaces }` snapshot for tools that prefer not to depend on this package.
|
|
27
27
|
|
|
28
28
|
## For users
|
|
29
29
|
|
|
@@ -33,7 +33,7 @@ Install the SDK so its `/languages` command and `--locale` flag are wired into y
|
|
|
33
33
|
pi install npm:@juicesharp/rpiv-i18n
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
-
Then restart Pi. (If you installed via `pi install npm:@juicesharp/rpiv-pi` + `/rpiv-setup`, this is already done
|
|
36
|
+
Then restart Pi. (If you installed via `pi install npm:@juicesharp/rpiv-pi` + `/rpiv-setup`, this is already done - `/rpiv-setup` auto-wires every sibling.)
|
|
37
37
|
|
|
38
38
|
Choose a language interactively:
|
|
39
39
|
|
|
@@ -53,7 +53,7 @@ Or edit the config file directly:
|
|
|
53
53
|
echo '{"locale":"uk"}' > ~/.config/rpiv-i18n/locale.json
|
|
54
54
|
```
|
|
55
55
|
|
|
56
|
-
Locale detection priority: `--locale` flag → `~/.config/rpiv-i18n/locale.json` → `process.env.LANG` / `LC_ALL` → English default. The auto-detection paths (config file, env vars) work even without this package installed
|
|
56
|
+
Locale detection priority: `--locale` flag → `~/.config/rpiv-i18n/locale.json` → `process.env.LANG` / `LC_ALL` → English default. The auto-detection paths (config file, env vars) work even without this package installed - any extension that `import`s the SDK as a peer dep gets the registered locale at module init. Only the picker (`/languages`) and the flag (`--locale`) require this package to be loaded as a Pi extension.
|
|
57
57
|
|
|
58
58
|
Other Pi extensions that integrate the SDK pick up your choice automatically.
|
|
59
59
|
|
|
@@ -104,11 +104,11 @@ export default function (pi: ExtensionAPI) {
|
|
|
104
104
|
### Behavior contract
|
|
105
105
|
|
|
106
106
|
- **English fallback per key.** If a key exists in `en` but not in the active locale's map, the English entry is returned. Missing-only-in-current-locale strings stay readable.
|
|
107
|
-
- **Always-safe fallback.** If neither this SDK nor your namespace is loaded, every `tr(...)` call returns its `fallback` literal
|
|
107
|
+
- **Always-safe fallback.** If neither this SDK nor your namespace is loaded, every `tr(...)` call returns its `fallback` literal - your extension keeps working.
|
|
108
108
|
- **Live locale changes.** When the user runs `/languages`, the next `tr(...)` call returns the new locale's string. No restart required.
|
|
109
|
-
- **Render-time only.** Call `tr(...)` at render time
|
|
109
|
+
- **Render-time only.** Call `tr(...)` at render time - never bake the result into a top-level `const X = tr(...)`. Module-init evaluation freezes the string before the user has a chance to set their locale.
|
|
110
110
|
|
|
111
|
-
## Localizing your extension
|
|
111
|
+
## Localizing your extension - step by step
|
|
112
112
|
|
|
113
113
|
The inline example above is fine for a one-key smoke test. For a real Pi extension, use the file-based pattern that scales to dozens of strings and ten contributors. Two production exemplars in this monorepo follow this exact shape: `packages/rpiv-ask-user-question/` (questionnaire UI; bridge owns `t` + `displayLabel(kind)` for sentinel rows) and `packages/rpiv-todo/` (todo overlay + `/todos` command; bridge owns `t` + `formatStatusLabel(status)` reused across overlay and command). Read either alongside this guide.
|
|
114
114
|
|
|
@@ -140,9 +140,9 @@ my-extension/
|
|
|
140
140
|
}
|
|
141
141
|
```
|
|
142
142
|
|
|
143
|
-
Use `peerDependencies`, not `dependencies`
|
|
143
|
+
Use `peerDependencies`, not `dependencies` - the user's Pi session loads one copy of the SDK; if you bundle your own, `/languages` toggles a different runtime instance and your strings never switch.
|
|
144
144
|
|
|
145
|
-
Mark it `optional: true` in `peerDependenciesMeta` so npm doesn't warn when a user installs your extension standalone without rpiv-i18n. Pair this with the dynamic-import shim shown in step 3
|
|
145
|
+
Mark it `optional: true` in `peerDependenciesMeta` so npm doesn't warn when a user installs your extension standalone without rpiv-i18n. Pair this with the dynamic-import shim shown in step 3 - your extension stays online with English-only UI when the SDK isn't installed, and lights up localization automatically when it is.
|
|
146
146
|
|
|
147
147
|
### 1. Author a `locales/en.json` next to your source
|
|
148
148
|
|
|
@@ -156,7 +156,7 @@ Mark it `optional: true` in `peerDependenciesMeta` so npm doesn't warn when a us
|
|
|
156
156
|
}
|
|
157
157
|
```
|
|
158
158
|
|
|
159
|
-
Conventions: flat dotted lowercase keys, `snake_case` only for multi-word leaves (`hint.cancel` ✓, `hint.cancelKey` ✗). `_meta.*` keys are ignored at lookup time
|
|
159
|
+
Conventions: flat dotted lowercase keys, `snake_case` only for multi-word leaves (`hint.cancel` ✓, `hint.cancelKey` ✗). `_meta.*` keys are ignored at lookup time - use them for provenance / WIP notes.
|
|
160
160
|
|
|
161
161
|
### 2. Add a one-file bridge inside your package
|
|
162
162
|
|
|
@@ -173,7 +173,7 @@ try {
|
|
|
173
173
|
const sdk = (await import("@juicesharp/rpiv-i18n")) as I18nSDK;
|
|
174
174
|
scopeImpl = sdk.scope(I18N_NAMESPACE);
|
|
175
175
|
} catch {
|
|
176
|
-
// SDK not installed
|
|
176
|
+
// SDK not installed - every t(key, fallback) returns the fallback verbatim.
|
|
177
177
|
scopeImpl = (_key, fallback) => fallback;
|
|
178
178
|
}
|
|
179
179
|
|
|
@@ -182,11 +182,11 @@ export const t: ScopeFn = scopeImpl;
|
|
|
182
182
|
|
|
183
183
|
Every render-time call site imports `t` from this one file. If you ever switch namespaces, change the SDK, or add a `displayLabel(kind)` convenience for sentinel-row enums, you touch one place.
|
|
184
184
|
|
|
185
|
-
**Why dynamic import instead of static `import { scope } from "@juicesharp/rpiv-i18n"`?** A static ESM import is hoisted and evaluated at module load
|
|
185
|
+
**Why dynamic import instead of static `import { scope } from "@juicesharp/rpiv-i18n"`?** A static ESM import is hoisted and evaluated at module load - if the SDK isn't on disk, your entire extension fails to load with `Cannot find module '@juicesharp/rpiv-i18n'`. The dynamic `await import()` inside a try/catch lets module load proceed, and the identity-fallback closure keeps your render call sites working with English. Top-level await is required because the `t` export is consumed synchronously by every render call site downstream.
|
|
186
186
|
|
|
187
187
|
### 3. Register strings at extension load
|
|
188
188
|
|
|
189
|
-
Use the same dynamic-import shim pattern as the bridge
|
|
189
|
+
Use the same dynamic-import shim pattern as the bridge - `registerStrings` is a runtime call, so it goes inside a try/catch that no-ops when the SDK is missing:
|
|
190
190
|
|
|
191
191
|
```ts
|
|
192
192
|
import { readFileSync } from "node:fs";
|
|
@@ -198,7 +198,7 @@ type TranslationMap = Readonly<Record<string, string>>;
|
|
|
198
198
|
type I18nSDK = { registerStrings: (ns: string, byLocale: Record<string, TranslationMap>) => void };
|
|
199
199
|
|
|
200
200
|
function loadLocale(code: string): TranslationMap {
|
|
201
|
-
// A missing/malformed locale file MUST NOT crash module init
|
|
201
|
+
// A missing/malformed locale file MUST NOT crash module init - the bridge's
|
|
202
202
|
// literal English fallbacks will keep every render readable.
|
|
203
203
|
try {
|
|
204
204
|
return JSON.parse(
|
|
@@ -218,7 +218,7 @@ try {
|
|
|
218
218
|
// non-en map fall back to en automatically.
|
|
219
219
|
});
|
|
220
220
|
} catch {
|
|
221
|
-
// SDK absent
|
|
221
|
+
// SDK absent - extension still loads with English-only UI.
|
|
222
222
|
}
|
|
223
223
|
|
|
224
224
|
export default function (pi: ExtensionAPI): void {
|
|
@@ -226,23 +226,23 @@ export default function (pi: ExtensionAPI): void {
|
|
|
226
226
|
}
|
|
227
227
|
```
|
|
228
228
|
|
|
229
|
-
`registerStrings` runs at module top-level, before Pi calls your default export. That timing is intentional
|
|
229
|
+
`registerStrings` runs at module top-level, before Pi calls your default export. That timing is intentional - by the time the first `tr(...)` fires, every locale map is in the registry.
|
|
230
230
|
|
|
231
231
|
### 4. Use `t(key, fallback)` at the render call site
|
|
232
232
|
|
|
233
233
|
```ts
|
|
234
234
|
import { t } from "../state/i18n-bridge.js";
|
|
235
235
|
|
|
236
|
-
// ✓ Render-time
|
|
236
|
+
// ✓ Render-time - re-evaluated each render; live `/languages` switches apply
|
|
237
237
|
function renderHeader(theme) {
|
|
238
238
|
return new Text(theme.bold(t("welcome.title", "Welcome")));
|
|
239
239
|
}
|
|
240
240
|
|
|
241
|
-
// ✗ Module-init
|
|
241
|
+
// ✗ Module-init - captured ONCE, freezes English on first load
|
|
242
242
|
const HEADING = t("welcome.title", "Welcome");
|
|
243
243
|
```
|
|
244
244
|
|
|
245
|
-
The fallback string is the canonical English literal
|
|
245
|
+
The fallback string is the canonical English literal - same one you put in `en.json`. Keep it inline at the call site so the file reads end-to-end without locale lookups, and so your extension stays usable when the SDK isn't installed at all.
|
|
246
246
|
|
|
247
247
|
### 5. Ship the locale files in `package.json`
|
|
248
248
|
|
|
@@ -261,9 +261,9 @@ The `files[]` manifest is the #1 publish-time miss in this monorepo's history. S
|
|
|
261
261
|
|
|
262
262
|
### What stays English (do NOT route through `t(...)`)
|
|
263
263
|
|
|
264
|
-
- Tool descriptions, TypeBox `description` fields, prompt guidelines / snippets
|
|
265
|
-
- Validation errors that flow back through `tool result` envelopes
|
|
266
|
-
- Anything checked by exact-string matching (reserved labels, dispatcher discriminants)
|
|
264
|
+
- Tool descriptions, TypeBox `description` fields, prompt guidelines / snippets - these go to the LLM. Localizing them risks the model emitting localized option labels that bypass your `RESERVED_LABEL_SET` validation.
|
|
265
|
+
- Validation errors that flow back through `tool result` envelopes - same reason.
|
|
266
|
+
- Anything checked by exact-string matching (reserved labels, dispatcher discriminants) - keep both sides in canonical English.
|
|
267
267
|
|
|
268
268
|
The recommended pattern is to keep a top-level `const X = "literal"` for the canonical English (so reserved-label checks and tests stay stable), then route the **render call site** through `t("key", X)`. The SDK never sees `X`; the LLM never sees `t(...)`.
|
|
269
269
|
|
|
@@ -280,7 +280,7 @@ export function displayLabel(kind: SentinelKind): string {
|
|
|
280
280
|
}
|
|
281
281
|
```
|
|
282
282
|
|
|
283
|
-
Render code becomes `displayLabel("next")` instead of `t("sentinel.next", "Next")`
|
|
283
|
+
Render code becomes `displayLabel("next")` instead of `t("sentinel.next", "Next")` - same lookup, but the canonical English fallback is sourced from the same metadata table the rest of your code uses. One source of truth per kind.
|
|
284
284
|
|
|
285
285
|
### Optional: a non-English locale file
|
|
286
286
|
|
|
@@ -292,7 +292,7 @@ Add `locales/uk.json` mirroring the en key set:
|
|
|
292
292
|
|
|
293
293
|
"welcome.title": "Ласкаво просимо",
|
|
294
294
|
"submit.button": "Надіслати",
|
|
295
|
-
"hint.cancel": "Esc
|
|
295
|
+
"hint.cancel": "Esc - скасувати"
|
|
296
296
|
}
|
|
297
297
|
```
|
|
298
298
|
|
|
@@ -302,7 +302,7 @@ Done. `/languages` now switches your extension's UI alongside every other extens
|
|
|
302
302
|
|
|
303
303
|
### Verify it works locally before publishing
|
|
304
304
|
|
|
305
|
-
The SDK only flips strings when it's loaded by a real Pi session
|
|
305
|
+
The SDK only flips strings when it's loaded by a real Pi session - `npm test` won't catch a missing `files[]` entry or a wrong namespace. Smoke-test against a live `pi` shell:
|
|
306
306
|
|
|
307
307
|
```bash
|
|
308
308
|
# from your extension's directory
|
|
@@ -315,21 +315,21 @@ pi # launch the session
|
|
|
315
315
|
```
|
|
316
316
|
|
|
317
317
|
Two failure modes this catches that unit tests do not:
|
|
318
|
-
1. **Locale JSON not shipped**
|
|
319
|
-
2. **Module-init `tr(...)` capture**
|
|
318
|
+
1. **Locale JSON not shipped** - `/languages` switches but your strings stay English. Fix: add `"locales/"` to `package.json` `files[]`.
|
|
319
|
+
2. **Module-init `tr(...)` capture** - picker switches, other extensions flip, yours doesn't. Fix: move the `t(...)` call inside the render function.
|
|
320
320
|
|
|
321
321
|
## Contributing translations
|
|
322
322
|
|
|
323
|
-
Want to add or improve a translation for a Pi extension that uses this SDK? Open a PR
|
|
323
|
+
Want to add or improve a translation for a Pi extension that uses this SDK? Open a PR - the contract is small and uniform across packages.
|
|
324
324
|
|
|
325
325
|
### What to translate (and what NOT to)
|
|
326
326
|
|
|
327
|
-
**Translate**: every key listed in the package's `locales/en.json`. These are TUI-facing strings
|
|
327
|
+
**Translate**: every key listed in the package's `locales/en.json`. These are TUI-facing strings - labels, hints, prompts, headings - read by humans on screen.
|
|
328
328
|
|
|
329
329
|
**Do NOT translate**:
|
|
330
|
-
- Tool descriptions, TypeBox `description` fields, prompt guidelines, prompt snippets
|
|
331
|
-
- Validation errors that flow through `tool result` envelopes (e.g. `"Error: UI not available …"`)
|
|
332
|
-
- `RESERVED_LABELS` and any keys checked by reserved-label validation
|
|
330
|
+
- Tool descriptions, TypeBox `description` fields, prompt guidelines, prompt snippets - they go to the LLM and stay English so the model parses them deterministically across sessions and providers.
|
|
331
|
+
- Validation errors that flow through `tool result` envelopes (e.g. `"Error: UI not available …"`) - same reason.
|
|
332
|
+
- `RESERVED_LABELS` and any keys checked by reserved-label validation - translating these lets a localized equivalent slip past the duplicate-detection guard.
|
|
333
333
|
|
|
334
334
|
If a key isn't in the package's `locales/en.json`, it's intentionally English-only. Don't invent new keys; open an issue first if you think a string should be made localizable.
|
|
335
335
|
|
|
@@ -337,7 +337,7 @@ If a key isn't in the package's `locales/en.json`, it's intentionally English-on
|
|
|
337
337
|
|
|
338
338
|
- One JSON file per locale, named `<code>.json` (e.g. `es.json`, `fr.json`, `pt-BR.json`), inside the consumer package's `locales/` directory.
|
|
339
339
|
- Locale codes follow BCP-47-ish convention: language only (`es`, `fr`, `de`) or `language-Region` for variants (`pt-BR`, `zh-CN`). Keep hyphenated, not underscored.
|
|
340
|
-
- Mirror the exact key set from `en.json`. Missing keys fall back to English silently
|
|
340
|
+
- Mirror the exact key set from `en.json`. Missing keys fall back to English silently - that's fine, but the `_meta.notes` field below should mention the gap.
|
|
341
341
|
|
|
342
342
|
### Key naming convention
|
|
343
343
|
|
|
@@ -347,19 +347,19 @@ Flat dotted lowercase: `sentinel.next`, `submit.cancel`, `preview.no_preview`. U
|
|
|
347
347
|
|
|
348
348
|
```json
|
|
349
349
|
{
|
|
350
|
-
"_meta.notes": "Optional contributor note
|
|
350
|
+
"_meta.notes": "Optional contributor note - auto-translated, native review welcome, key gaps, etc.",
|
|
351
351
|
|
|
352
352
|
"<dotted.key.from.en.json>": "Localized string"
|
|
353
353
|
}
|
|
354
354
|
```
|
|
355
355
|
|
|
356
|
-
`_meta.*` keys are ignored by `tr(...)` lookups (no consumer requests them). Use them for provenance, change notes, or "WIP
|
|
356
|
+
`_meta.*` keys are ignored by `tr(...)` lookups (no consumer requests them). Use them for provenance, change notes, or "WIP - N keys missing".
|
|
357
357
|
|
|
358
358
|
### Universal CLI conventions (do NOT translate)
|
|
359
359
|
|
|
360
|
-
- Symbols: `↑/↓`, `⚠`, `✓`
|
|
361
|
-
- Keyboard names: `Enter`, `Esc`, `Tab`, `Space`
|
|
362
|
-
- Single-key shortcut letters (`n` to add notes): keep the letter unchanged
|
|
360
|
+
- Symbols: `↑/↓`, `⚠`, `✓` - render the same in every locale.
|
|
361
|
+
- Keyboard names: `Enter`, `Esc`, `Tab`, `Space` - these are the labels printed on physical keyboards worldwide. Some locales (e.g. French) write `Entrée` / `Échap`; that's acceptable when paired with the convention you're targeting (KDE/GNOME do this), but be consistent across the file.
|
|
362
|
+
- Single-key shortcut letters (`n` to add notes): keep the letter unchanged - it maps to a literal keystroke handler, not to a label.
|
|
363
363
|
|
|
364
364
|
### Registering the locale
|
|
365
365
|
|
|
@@ -388,7 +388,7 @@ Include:
|
|
|
388
388
|
1. The new `locales/<code>.json` file.
|
|
389
389
|
2. The `registerStrings(...)` update in the consumer's entry point.
|
|
390
390
|
3. The `SUPPORTED_LOCALES` entry in `rpiv-i18n/i18n.ts`.
|
|
391
|
-
4. Run `npm run check && npm test` from the monorepo root
|
|
391
|
+
4. Run `npm run check && npm test` from the monorepo root - both must pass.
|
|
392
392
|
|
|
393
393
|
A native-speaker reviewer will land it. Auto-translated drafts are accepted (mark them in `_meta.notes`); English fallbacks make any gap or error invisible to users until a fix arrives.
|
|
394
394
|
|
|
@@ -400,7 +400,7 @@ For tools that prefer not to import this package, the active state is also publi
|
|
|
400
400
|
const I18N = Symbol.for("rpiv-i18n");
|
|
401
401
|
|
|
402
402
|
function lookup(key: string, fallback: string): string {
|
|
403
|
-
// Re-read the symbol on every call
|
|
403
|
+
// Re-read the symbol on every call - the SDK *replaces* the snapshot
|
|
404
404
|
// (frozen object) on every registerStrings/applyLocale, so a cached
|
|
405
405
|
// reference will silently serve stale strings after `/languages`.
|
|
406
406
|
const state = (globalThis as { [k: symbol]: unknown })[I18N] as
|
|
@@ -412,4 +412,4 @@ function lookup(key: string, fallback: string): string {
|
|
|
412
412
|
lookup("welcome", "Welcome!");
|
|
413
413
|
```
|
|
414
414
|
|
|
415
|
-
The published object is frozen *per snapshot*, so each new locale produces a new frozen object
|
|
415
|
+
The published object is frozen *per snapshot*, so each new locale produces a new frozen object - read the symbol at call time, never hoist it into a module-scope `const`. Registration MUST still go through `registerStrings(...)`; writing into `globalThis[I18N]` directly is unsupported.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@juicesharp/rpiv-i18n",
|
|
3
|
-
"version": "1.1.
|
|
4
|
-
"description": "Pi extension
|
|
3
|
+
"version": "1.1.5",
|
|
4
|
+
"description": "Pi extension. The localization foundation for rpiv-* skills: locale detection, the /languages command, the --locale flag, and a cross-package locale registry.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|
|
7
7
|
"pi-extension",
|