@juicesharp/rpiv-i18n 2.0.0 → 2.2.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 +110 -379
- package/docs/integration-guide.md +237 -0
- package/docs/sdk-reference.md +147 -0
- package/docs/translating.md +99 -0
- package/package.json +5 -2
- package/docs/cover.png +0 -0
- package/docs/cover.svg +0 -89
package/README.md
CHANGED
|
@@ -1,419 +1,150 @@
|
|
|
1
1
|
# @juicesharp/rpiv-i18n
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@juicesharp/rpiv-i18n)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
3
6
|
<div align="center">
|
|
4
7
|
<a href="https://github.com/juicesharp/rpiv-mono/tree/main/packages/rpiv-i18n">
|
|
5
|
-
<
|
|
6
|
-
<img src="https://raw.githubusercontent.com/juicesharp/rpiv-mono/main/packages/rpiv-i18n/docs/cover.png" alt="rpiv-i18n cover" width="50%">
|
|
7
|
-
</picture>
|
|
8
|
+
<img src="https://raw.githubusercontent.com/juicesharp/rpiv-mono/main/packages/rpiv-i18n/docs/cover.png" alt="rpiv-i18n cover: a three-line API stack — registerStrings, scope, translate — turning an English hint into its German equivalent" width="50%">
|
|
8
9
|
</a>
|
|
9
10
|
</div>
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
Pick the language every rpiv-\* extension speaks in your [Pi Agent](https://github.com/badlogic/pi-mono)
|
|
13
|
+
terminal. It adds a `/languages` picker and a `--locale` flag, and remembers your
|
|
14
|
+
choice between sessions. It is also the SDK any Pi extension author calls to make
|
|
15
|
+
their own TUI strings translatable — three lines, with English fallback per key.
|
|
14
16
|
|
|
15
|
-
|
|
16
|
-
- **`/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.
|
|
17
|
-
- **`--locale <code>` CLI flag** for one-shot or scripted launches.
|
|
18
|
-
- **Auto-detects** `process.env.LANG` / `LC_ALL` so most Unix users get a localized UI without configuration.
|
|
19
|
-
- **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.
|
|
20
|
-
- **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).
|
|
21
|
-
- **Live propagation** - locale changes via `/languages` apply to the next render with no restart, even when the SDK is loaded as multiple module instances.
|
|
22
|
-
- **Always-safe fallback** - if the SDK isn't installed at all, every `tr(...)` call returns the consumer's literal English fallback. Extensions stay usable.
|
|
23
|
-
- **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.
|
|
17
|
+
## Install
|
|
24
18
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
Install the SDK so its `/languages` command and `--locale` flag are wired into your Pi session:
|
|
28
|
-
|
|
29
|
-
```bash
|
|
19
|
+
```sh
|
|
30
20
|
pi install npm:@juicesharp/rpiv-i18n
|
|
31
21
|
```
|
|
32
22
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
Choose a language interactively:
|
|
36
|
-
|
|
37
|
-
```
|
|
38
|
-
/languages
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
Or pass a flag at startup:
|
|
42
|
-
|
|
43
|
-
```bash
|
|
44
|
-
pi --locale uk
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
Or edit the config file directly:
|
|
48
|
-
|
|
49
|
-
```bash
|
|
50
|
-
echo '{"locale":"uk"}' > ~/.config/rpiv-i18n/locale.json
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
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.
|
|
54
|
-
|
|
55
|
-
Other Pi extensions that integrate the SDK pick up your choice automatically.
|
|
56
|
-
|
|
57
|
-
## For Pi extension authors
|
|
58
|
-
|
|
59
|
-
The SDK gives every Pi extension a single shared locale dial. Register your translations once, then look them up at render time.
|
|
60
|
-
|
|
61
|
-
### Register strings at extension load
|
|
62
|
-
|
|
63
|
-
```ts
|
|
64
|
-
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
65
|
-
import { registerStrings, scope } from "@juicesharp/rpiv-i18n";
|
|
66
|
-
|
|
67
|
-
const NAMESPACE = "@my-org/cool-tool";
|
|
68
|
-
|
|
69
|
-
registerStrings(NAMESPACE, {
|
|
70
|
-
en: {
|
|
71
|
-
"welcome": "Welcome!",
|
|
72
|
-
"submit": "Submit",
|
|
73
|
-
},
|
|
74
|
-
uk: {
|
|
75
|
-
"welcome": "Ласкаво просимо!",
|
|
76
|
-
"submit": "Надіслати",
|
|
77
|
-
},
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
const t = scope(NAMESPACE);
|
|
81
|
-
|
|
82
|
-
export default function (pi: ExtensionAPI) {
|
|
83
|
-
pi.registerCommand("hello", {
|
|
84
|
-
handler: async (_args, ctx) => {
|
|
85
|
-
ctx.ui.notify(t("welcome", "Welcome!"));
|
|
86
|
-
},
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
### API
|
|
92
|
-
|
|
93
|
-
| Function | Purpose |
|
|
94
|
-
|---|---|
|
|
95
|
-
| `registerStrings(namespace, byLocale)` | Register a package's translation maps. The namespace SHOULD be your npm package name. Re-calling replaces the prior registration. |
|
|
96
|
-
| `scope(namespace)` | Returns a pre-bound `(key, fallback) => string` closure. |
|
|
97
|
-
| `tr(namespace, key, fallback)` | One-shot lookup. Returns `fallback` when the namespace or key isn't registered. |
|
|
98
|
-
| `getActiveLocale()` | Current locale code, or `undefined` for the English default. |
|
|
99
|
-
| `applyLocale(code)` | Set the active locale; rebuilds the active strings across all registered namespaces. |
|
|
100
|
-
|
|
101
|
-
### Behavior contract
|
|
102
|
-
|
|
103
|
-
- **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.
|
|
104
|
-
- **Always-safe fallback.** If neither this SDK nor your namespace is loaded, every `tr(...)` call returns its `fallback` literal - your extension keeps working.
|
|
105
|
-
- **Live locale changes.** When the user runs `/languages`, the next `tr(...)` call returns the new locale's string. No restart required.
|
|
106
|
-
- **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.
|
|
107
|
-
|
|
108
|
-
## Localizing your extension - step by step
|
|
109
|
-
|
|
110
|
-
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.
|
|
111
|
-
|
|
112
|
-
End state on disk:
|
|
113
|
-
|
|
114
|
-
```
|
|
115
|
-
my-extension/
|
|
116
|
-
├── index.ts ← default export + registerStrings(...)
|
|
117
|
-
├── state/
|
|
118
|
-
│ └── i18n-bridge.ts ← exports `t` + I18N_NAMESPACE
|
|
119
|
-
├── locales/
|
|
120
|
-
│ ├── en.json ← canonical baseline (required)
|
|
121
|
-
│ ├── uk.json ← optional additional locales
|
|
122
|
-
│ └── …
|
|
123
|
-
└── package.json ← peerDependencies + files[] + pi.extensions
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
### 0. Declare the SDK as an OPTIONAL peer dependency
|
|
127
|
-
|
|
128
|
-
```json
|
|
129
|
-
{
|
|
130
|
-
"peerDependencies": {
|
|
131
|
-
"@juicesharp/rpiv-i18n": "*",
|
|
132
|
-
"@earendil-works/pi-coding-agent": "*"
|
|
133
|
-
},
|
|
134
|
-
"peerDependenciesMeta": {
|
|
135
|
-
"@juicesharp/rpiv-i18n": { "optional": true }
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
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.
|
|
141
|
-
|
|
142
|
-
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.
|
|
143
|
-
|
|
144
|
-
### 1. Author a `locales/en.json` next to your source
|
|
145
|
-
|
|
146
|
-
```json
|
|
147
|
-
{
|
|
148
|
-
"_meta.notes": "English baseline. Any new key MUST land here first; other locales fall back to it.",
|
|
149
|
-
|
|
150
|
-
"welcome.title": "Welcome",
|
|
151
|
-
"submit.button": "Submit",
|
|
152
|
-
"hint.cancel": "Esc to cancel"
|
|
153
|
-
}
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
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.
|
|
157
|
-
|
|
158
|
-
### 2. Add a one-file bridge inside your package
|
|
159
|
-
|
|
160
|
-
`state/i18n-bridge.ts` (or wherever your package keeps cross-cutting helpers). Use a dynamic-import shim with top-level `await` so a missing peer degrades to English-only instead of failing module load:
|
|
161
|
-
|
|
162
|
-
```ts
|
|
163
|
-
export const I18N_NAMESPACE = "@my-org/cool-tool";
|
|
164
|
-
|
|
165
|
-
type ScopeFn = (key: string, fallback: string) => string;
|
|
166
|
-
type I18nSDK = { scope: (namespace: string) => ScopeFn };
|
|
167
|
-
|
|
168
|
-
let scopeImpl: ScopeFn;
|
|
169
|
-
try {
|
|
170
|
-
const sdk = (await import("@juicesharp/rpiv-i18n")) as I18nSDK;
|
|
171
|
-
scopeImpl = sdk.scope(I18N_NAMESPACE);
|
|
172
|
-
} catch {
|
|
173
|
-
// SDK not installed - every t(key, fallback) returns the fallback verbatim.
|
|
174
|
-
scopeImpl = (_key, fallback) => fallback;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
export const t: ScopeFn = scopeImpl;
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
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.
|
|
181
|
-
|
|
182
|
-
**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.
|
|
23
|
+
Restart your Pi session.
|
|
183
24
|
|
|
184
|
-
|
|
25
|
+
## Quick start
|
|
185
26
|
|
|
186
|
-
|
|
27
|
+
Run the picker and choose a language:
|
|
187
28
|
|
|
188
|
-
```ts
|
|
189
|
-
import { readFileSync } from "node:fs";
|
|
190
|
-
import { fileURLToPath } from "node:url";
|
|
191
|
-
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
192
|
-
import { I18N_NAMESPACE } from "./state/i18n-bridge.js";
|
|
193
|
-
|
|
194
|
-
type TranslationMap = Readonly<Record<string, string>>;
|
|
195
|
-
type I18nSDK = { registerStrings: (ns: string, byLocale: Record<string, TranslationMap>) => void };
|
|
196
|
-
|
|
197
|
-
function loadLocale(code: string): TranslationMap {
|
|
198
|
-
// A missing/malformed locale file MUST NOT crash module init - the bridge's
|
|
199
|
-
// literal English fallbacks will keep every render readable.
|
|
200
|
-
try {
|
|
201
|
-
return JSON.parse(
|
|
202
|
-
readFileSync(fileURLToPath(new URL(`./locales/${code}.json`, import.meta.url)), "utf-8"),
|
|
203
|
-
) as TranslationMap;
|
|
204
|
-
} catch (err) {
|
|
205
|
-
console.warn(`@my-org/cool-tool: failed to load locales/${code}.json (${(err as Error).message})`);
|
|
206
|
-
return {};
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
try {
|
|
211
|
-
const sdk = (await import("@juicesharp/rpiv-i18n")) as I18nSDK;
|
|
212
|
-
sdk.registerStrings(I18N_NAMESPACE, {
|
|
213
|
-
en: loadLocale("en"),
|
|
214
|
-
// Add more as files arrive. Order doesn't matter; keys missing from a
|
|
215
|
-
// non-en map fall back to en automatically.
|
|
216
|
-
});
|
|
217
|
-
} catch {
|
|
218
|
-
// SDK absent - extension still loads with English-only UI.
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
export default function (pi: ExtensionAPI): void {
|
|
222
|
-
// your tool/command/hook registrations here…
|
|
223
|
-
}
|
|
224
29
|
```
|
|
225
|
-
|
|
226
|
-
`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.
|
|
227
|
-
|
|
228
|
-
### 4. Use `t(key, fallback)` at the render call site
|
|
229
|
-
|
|
230
|
-
```ts
|
|
231
|
-
import { t } from "../state/i18n-bridge.js";
|
|
232
|
-
|
|
233
|
-
// ✓ Render-time - re-evaluated each render; live `/languages` switches apply
|
|
234
|
-
function renderHeader(theme) {
|
|
235
|
-
return new Text(theme.bold(t("welcome.title", "Welcome")));
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
// ✗ Module-init - captured ONCE, freezes English on first load
|
|
239
|
-
const HEADING = t("welcome.title", "Welcome");
|
|
30
|
+
/languages
|
|
240
31
|
```
|
|
241
32
|
|
|
242
|
-
|
|
33
|
+
Arrow keys move, `Enter` selects, `Esc` cancels. A `✓` marks your current choice,
|
|
34
|
+
and a **System default** row hands control back to your environment. The moment
|
|
35
|
+
you select, every installed extension that uses the SDK renders in the new
|
|
36
|
+
language — no restart.
|
|
243
37
|
|
|
244
|
-
|
|
38
|
+
To set it for one launch instead, pass the flag:
|
|
245
39
|
|
|
246
|
-
```
|
|
247
|
-
|
|
248
|
-
"files": [
|
|
249
|
-
"index.ts",
|
|
250
|
-
"state/i18n-bridge.ts",
|
|
251
|
-
"locales/",
|
|
252
|
-
"…"
|
|
253
|
-
]
|
|
254
|
-
}
|
|
40
|
+
```sh
|
|
41
|
+
pi --locale uk
|
|
255
42
|
```
|
|
256
43
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
44
|
+
| Code | Language |
|
|
45
|
+
| --- | --- |
|
|
46
|
+
| `de` | Deutsch |
|
|
47
|
+
| `en` | English |
|
|
48
|
+
| `es` | Español |
|
|
49
|
+
| `fr` | Français |
|
|
50
|
+
| `pt` | Português |
|
|
51
|
+
| `pt-BR` | Português (Brasil) |
|
|
52
|
+
| `ru` | Русский |
|
|
53
|
+
| `uk` | Українська |
|
|
54
|
+
| `zh` | 中文 |
|
|
264
55
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
### Optional: per-namespace `displayLabel` helper
|
|
268
|
-
|
|
269
|
-
If your extension has a small enum-typed set of "kind" rows (sentinels, statuses, modes), a one-line helper keeps render code tight:
|
|
56
|
+
To localize your own extension, add `@juicesharp/rpiv-i18n` to your
|
|
57
|
+
`peerDependencies` (marked optional), then at extension load:
|
|
270
58
|
|
|
271
59
|
```ts
|
|
272
|
-
|
|
273
|
-
import {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
```
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
2. **Module-init `tr(...)` capture** - picker switches, other extensions flip, yours doesn't. Fix: move the `t(...)` call inside the render function.
|
|
317
|
-
|
|
318
|
-
## Contributing translations
|
|
319
|
-
|
|
320
|
-
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.
|
|
321
|
-
|
|
322
|
-
### What to translate (and what NOT to)
|
|
323
|
-
|
|
324
|
-
**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.
|
|
325
|
-
|
|
326
|
-
**Do NOT translate**:
|
|
327
|
-
- 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.
|
|
328
|
-
- Validation errors that flow through `tool result` envelopes (e.g. `"Error: UI not available …"`) - same reason.
|
|
329
|
-
- `RESERVED_LABELS` and any keys checked by reserved-label validation - translating these lets a localized equivalent slip past the duplicate-detection guard.
|
|
330
|
-
|
|
331
|
-
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.
|
|
332
|
-
|
|
333
|
-
### File location and naming
|
|
334
|
-
|
|
335
|
-
- One JSON file per locale, named `<code>.json` (e.g. `es.json`, `fr.json`, `pt-BR.json`), inside the consumer package's `locales/` directory.
|
|
336
|
-
- 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.
|
|
337
|
-
- 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.
|
|
338
|
-
|
|
339
|
-
### Key naming convention
|
|
340
|
-
|
|
341
|
-
Flat dotted lowercase: `sentinel.next`, `submit.cancel`, `preview.no_preview`. Use `snake_case` only for multi-word leaves (`preview.no_preview`, never `preview.noPreview` or `preview.no-preview`). Mixing conventions inside one file is a rejection criterion.
|
|
342
|
-
|
|
343
|
-
### File shape
|
|
60
|
+
import { scope } from "@juicesharp/rpiv-i18n";
|
|
61
|
+
import { registerLocalesFromDir } from "@juicesharp/rpiv-i18n/loader";
|
|
62
|
+
|
|
63
|
+
registerLocalesFromDir("@my-org/cool-tool", import.meta.url);
|
|
64
|
+
export const t = scope("@my-org/cool-tool");
|
|
65
|
+
// then t("welcome.title", "Welcome") at every render site
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Wrap both imports in the dynamic-import shim from the integration guide below to
|
|
69
|
+
stay online in English when the SDK is absent.
|
|
70
|
+
|
|
71
|
+
## What you get
|
|
72
|
+
|
|
73
|
+
- **One dial for every extension** — `/languages` writes a single preference and
|
|
74
|
+
rebuilds the strings of every registered package at once, not one setting per
|
|
75
|
+
tool.
|
|
76
|
+
- **Nine languages, no file editing** — every locale in the table above ships
|
|
77
|
+
with the extension; nothing to download, compile, or configure.
|
|
78
|
+
- **A localized UI with zero setup on most Unix systems** — `LANG` and `LC_ALL`
|
|
79
|
+
are read at startup, so `uk_UA.UTF-8` gives you Ukrainian chrome before you
|
|
80
|
+
touch anything.
|
|
81
|
+
- **Your selection cannot silently revert** — the picker writes to disk *before*
|
|
82
|
+
applying in memory; if the write fails you get
|
|
83
|
+
`Failed to save locale preference — selection not persisted` and the old
|
|
84
|
+
locale stays put.
|
|
85
|
+
- **Localize your own extension in one call** — `registerLocalesFromDir` reads
|
|
86
|
+
your package's `locales/*.json`; `scope(ns)` gives you `t(key, fallback)`.
|
|
87
|
+
- **A broken translation never takes an extension down** — an unparseable locale
|
|
88
|
+
file warns and is skipped, a missing key falls back to English, and a key
|
|
89
|
+
missing everywhere returns the inline English literal you passed at the call
|
|
90
|
+
site.
|
|
91
|
+
- **Only the TUI is translated** — system prompts, tool descriptions, and other
|
|
92
|
+
LLM-facing copy stay English on purpose, so model behavior does not change
|
|
93
|
+
with your locale.
|
|
94
|
+
|
|
95
|
+
## Configuration
|
|
96
|
+
|
|
97
|
+
Your choice is stored at `$XDG_CONFIG_HOME/rpiv-i18n/locale.json`, defaulting to
|
|
98
|
+
`~/.config/rpiv-i18n/locale.json`. The file is created with mode `0600` and holds
|
|
99
|
+
exactly one key:
|
|
100
|
+
|
|
101
|
+
| Key | What it does | Default |
|
|
102
|
+
| --- | --- | --- |
|
|
103
|
+
| `locale` | UI locale code, e.g. `"uk"`. Omitted from the file entirely when you pick **System default**. | absent — falls through to environment detection, then English |
|
|
344
104
|
|
|
345
105
|
```json
|
|
346
|
-
{
|
|
347
|
-
"_meta.notes": "Optional contributor note - auto-translated, native review welcome, key gaps, etc.",
|
|
348
|
-
|
|
349
|
-
"<dotted.key.from.en.json>": "Localized string"
|
|
350
|
-
}
|
|
106
|
+
{ "locale": "uk" }
|
|
351
107
|
```
|
|
352
108
|
|
|
353
|
-
|
|
109
|
+
Surfaces and environment inputs:
|
|
354
110
|
|
|
355
|
-
|
|
111
|
+
| Surface | Effect |
|
|
112
|
+
| --- | --- |
|
|
113
|
+
| `/languages` | Opens the picker. Requires an interactive session; otherwise it reports `/languages requires interactive mode`. |
|
|
114
|
+
| `--locale <code>` | Sets the locale for that session, ahead of the config file. |
|
|
115
|
+
| `LANG`, `LC_ALL` | Used when no locale is configured. The language segment is taken from `<lang>_<REGION>.<charset>`; `C` and `POSIX` are ignored. |
|
|
116
|
+
| `XDG_CONFIG_HOME` | Relocates the config directory. Must be an absolute path or `~`-prefixed, per the XDG spec. |
|
|
356
117
|
|
|
357
|
-
|
|
358
|
-
- 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.
|
|
359
|
-
- Single-key shortcut letters (`n` to add notes): keep the letter unchanged - it maps to a literal keystroke handler, not to a label.
|
|
118
|
+
Resolution order: `--locale` → config file → `LANG` → `LC_ALL` → English.
|
|
360
119
|
|
|
361
|
-
|
|
120
|
+
## Reference
|
|
362
121
|
|
|
363
|
-
|
|
122
|
+
- [SDK reference](https://github.com/juicesharp/rpiv-mono/blob/main/packages/rpiv-i18n/docs/sdk-reference.md)
|
|
123
|
+
— every export from `@juicesharp/rpiv-i18n` and `/loader`, the fallback
|
|
124
|
+
contract, the detection chain, and the `globalThis` escape hatch.
|
|
125
|
+
- [Integration guide](https://github.com/juicesharp/rpiv-mono/blob/main/packages/rpiv-i18n/docs/integration-guide.md)
|
|
126
|
+
— step-by-step guide to localizing your own Pi extension, from optional peer
|
|
127
|
+
dependency to a live smoke test.
|
|
128
|
+
- [Contributing translations](https://github.com/juicesharp/rpiv-mono/blob/main/packages/rpiv-i18n/docs/translating.md)
|
|
129
|
+
— what to translate, key naming, file shape, PR checklist.
|
|
364
130
|
|
|
365
|
-
|
|
366
|
-
registerStrings(I18N_NAMESPACE, {
|
|
367
|
-
en: loadLocale("en"),
|
|
368
|
-
es: loadLocale("es"),
|
|
369
|
-
fr: loadLocale("fr"),
|
|
370
|
-
"pt-BR": loadLocale("pt-BR"),
|
|
371
|
-
});
|
|
372
|
-
```
|
|
131
|
+
## Requirements
|
|
373
132
|
|
|
374
|
-
|
|
133
|
+
An interactive terminal is needed for `/languages`; the flag, the config file,
|
|
134
|
+
and environment detection all work without one. No API keys, no network access,
|
|
135
|
+
no native dependencies. The `0600` permission bit and `LANG`/`LC_ALL` detection
|
|
136
|
+
are Unix conventions and are not exercised on Windows.
|
|
375
137
|
|
|
376
|
-
|
|
377
|
-
{ code: "fr", label: "Français" },
|
|
378
|
-
```
|
|
379
|
-
|
|
380
|
-
The `label` is the locale's endonym (the language's name in itself), not its English name.
|
|
381
|
-
|
|
382
|
-
### Submitting a PR
|
|
383
|
-
|
|
384
|
-
Include:
|
|
385
|
-
1. The new `locales/<code>.json` file.
|
|
386
|
-
2. The `registerStrings(...)` update in the consumer's entry point.
|
|
387
|
-
3. The `SUPPORTED_LOCALES` entry in `rpiv-i18n/i18n.ts`.
|
|
388
|
-
4. Run `npm run check && npm test` from the monorepo root - both must pass.
|
|
389
|
-
|
|
390
|
-
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.
|
|
391
|
-
|
|
392
|
-
## globalThis introspection (escape hatch)
|
|
138
|
+
## Related
|
|
393
139
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
// (frozen object) on every registerStrings/applyLocale, so a cached
|
|
402
|
-
// reference will silently serve stale strings after `/languages`.
|
|
403
|
-
const state = (globalThis as { [k: symbol]: unknown })[I18N] as
|
|
404
|
-
| { locale: string | undefined; namespaces: Record<string, Record<string, string>> }
|
|
405
|
-
| undefined;
|
|
406
|
-
return state?.namespaces["@my-org/cool-tool"]?.[key] ?? fallback;
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
lookup("welcome", "Welcome!");
|
|
410
|
-
```
|
|
411
|
-
|
|
412
|
-
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.
|
|
140
|
+
- [@juicesharp/rpiv-pi](https://www.npmjs.com/package/@juicesharp/rpiv-pi) — the
|
|
141
|
+
umbrella package; its `/rpiv-setup` installs this one for you.
|
|
142
|
+
- [@juicesharp/rpiv-todo](https://www.npmjs.com/package/@juicesharp/rpiv-todo),
|
|
143
|
+
[@juicesharp/rpiv-ask-user-question](https://www.npmjs.com/package/@juicesharp/rpiv-ask-user-question),
|
|
144
|
+
and [@juicesharp/rpiv-voice](https://www.npmjs.com/package/@juicesharp/rpiv-voice)
|
|
145
|
+
— extensions that follow your `/languages` choice today. Each treats this
|
|
146
|
+
package as an optional peer and stays online in English without it.
|
|
413
147
|
|
|
414
148
|
## License
|
|
415
149
|
|
|
416
|
-
[
|
|
417
|
-
[](https://opensource.org/licenses/MIT)
|
|
418
|
-
|
|
419
|
-
MIT
|
|
150
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
# Localizing your Pi extension
|
|
2
|
+
|
|
3
|
+
A step-by-step walkthrough for extension authors: declare the SDK as an optional
|
|
4
|
+
peer, author a locale directory, register it in one call, and look strings up at
|
|
5
|
+
render time. For the API surface alone, see
|
|
6
|
+
[sdk-reference.md](./sdk-reference.md).
|
|
7
|
+
|
|
8
|
+
Three production exemplars in this monorepo follow the exact shape below —
|
|
9
|
+
[rpiv-todo](https://github.com/juicesharp/rpiv-mono/tree/main/packages/rpiv-todo),
|
|
10
|
+
[rpiv-ask-user-question](https://github.com/juicesharp/rpiv-mono/tree/main/packages/rpiv-ask-user-question),
|
|
11
|
+
and [rpiv-voice](https://github.com/juicesharp/rpiv-mono/tree/main/packages/rpiv-voice).
|
|
12
|
+
Read one alongside this guide.
|
|
13
|
+
|
|
14
|
+
End state on disk:
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
my-extension/
|
|
18
|
+
├── index.ts ← default export + registerLocalesFromDir(...)
|
|
19
|
+
├── state/
|
|
20
|
+
│ └── i18n-bridge.ts ← exports `t` + I18N_NAMESPACE
|
|
21
|
+
├── locales/
|
|
22
|
+
│ ├── en.json ← canonical baseline (required)
|
|
23
|
+
│ ├── uk.json ← optional additional locales
|
|
24
|
+
│ └── …
|
|
25
|
+
└── package.json ← peerDependencies + files[] + pi.extensions
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## 1. Declare the SDK as an optional peer dependency
|
|
29
|
+
|
|
30
|
+
```json
|
|
31
|
+
{
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"@juicesharp/rpiv-i18n": "*",
|
|
34
|
+
"@earendil-works/pi-coding-agent": "*"
|
|
35
|
+
},
|
|
36
|
+
"peerDependenciesMeta": {
|
|
37
|
+
"@juicesharp/rpiv-i18n": { "optional": true }
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Use `peerDependencies`, not `dependencies`. The user's Pi session loads one copy
|
|
43
|
+
of the SDK; if you bundle your own, `/languages` toggles a different runtime
|
|
44
|
+
instance and your strings never switch.
|
|
45
|
+
|
|
46
|
+
Mark it `optional: true` so npm does not warn when someone installs your
|
|
47
|
+
extension standalone. Paired with the dynamic-import shim in step 3, your
|
|
48
|
+
extension stays online with an English-only UI when the SDK is absent and lights
|
|
49
|
+
up localization automatically when it is present.
|
|
50
|
+
|
|
51
|
+
## 2. Author `locales/en.json`
|
|
52
|
+
|
|
53
|
+
```json
|
|
54
|
+
{
|
|
55
|
+
"_meta.notes": "English baseline. Any new key MUST land here first; other locales fall back to it.",
|
|
56
|
+
|
|
57
|
+
"welcome.title": "Welcome",
|
|
58
|
+
"submit.button": "Submit",
|
|
59
|
+
"hint.cancel": "Esc to cancel"
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Keys are flat and dotted. `_meta.*` keys are never requested by a lookup — use
|
|
64
|
+
them for provenance and work-in-progress notes. Full key and file conventions
|
|
65
|
+
live in [translating.md](./translating.md).
|
|
66
|
+
|
|
67
|
+
## 3. Add a one-file bridge
|
|
68
|
+
|
|
69
|
+
`state/i18n-bridge.ts` (or wherever your package keeps cross-cutting helpers).
|
|
70
|
+
Use a dynamic-import shim with top-level `await` so a missing peer degrades to
|
|
71
|
+
English instead of failing module load:
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
export const I18N_NAMESPACE = "@my-org/cool-tool";
|
|
75
|
+
|
|
76
|
+
type ScopeFn = (key: string, fallback: string) => string;
|
|
77
|
+
type I18nSDK = { scope: (namespace: string) => ScopeFn };
|
|
78
|
+
|
|
79
|
+
let scopeImpl: ScopeFn;
|
|
80
|
+
try {
|
|
81
|
+
const sdk = (await import("@juicesharp/rpiv-i18n")) as I18nSDK;
|
|
82
|
+
scopeImpl = sdk.scope(I18N_NAMESPACE);
|
|
83
|
+
} catch {
|
|
84
|
+
// SDK not installed — every t(key, fallback) returns the fallback verbatim.
|
|
85
|
+
scopeImpl = (_key, fallback) => fallback;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export const t: ScopeFn = scopeImpl;
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Every render call site imports `t` from this one file. If you later switch
|
|
92
|
+
namespaces or add a convenience helper, you touch one place.
|
|
93
|
+
|
|
94
|
+
**Why dynamic import?** A static ESM import is hoisted and evaluated at module
|
|
95
|
+
load — if the SDK is not on disk, your whole extension fails to load with
|
|
96
|
+
`Cannot find module '@juicesharp/rpiv-i18n'`. The dynamic `await import()` inside
|
|
97
|
+
a try/catch lets module load proceed, and the identity-fallback closure keeps
|
|
98
|
+
render sites working in English. Top-level `await` is required because `t` is
|
|
99
|
+
consumed synchronously by downstream modules.
|
|
100
|
+
|
|
101
|
+
## 4. Register the locale directory at extension load
|
|
102
|
+
|
|
103
|
+
One call from the `/loader` subpath reads every `locales/<code>.json` your
|
|
104
|
+
package ships and registers them:
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
108
|
+
import { I18N_NAMESPACE } from "./state/i18n-bridge.js";
|
|
109
|
+
|
|
110
|
+
type I18nLoader = {
|
|
111
|
+
registerLocalesFromDir: (namespace: string, packageUrl: string, options?: { label?: string }) => void;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
const sdk = (await import("@juicesharp/rpiv-i18n/loader")) as I18nLoader;
|
|
116
|
+
sdk.registerLocalesFromDir(I18N_NAMESPACE, import.meta.url, { label: "my-extension" });
|
|
117
|
+
} catch {
|
|
118
|
+
// SDK absent — extension still loads with English-only UI.
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export default function (pi: ExtensionAPI): void {
|
|
122
|
+
// your tool/command/hook registrations here…
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Notes:
|
|
127
|
+
|
|
128
|
+
- Import `/loader`, not the package root. The subpath avoids pulling
|
|
129
|
+
`i18n-ui.ts` and `@earendil-works/pi-tui` into your load graph just to
|
|
130
|
+
register strings.
|
|
131
|
+
- `import.meta.url` anchors the lookup to **your** package's `locales/`.
|
|
132
|
+
- `label` only prefixes the `console.warn` emitted when a locale file fails to
|
|
133
|
+
parse; it defaults to the namespace.
|
|
134
|
+
- Adding a new locale later needs **no** code change here — the loader iterates
|
|
135
|
+
`SUPPORTED_LOCALES` from the SDK. Drop the JSON file and ship it.
|
|
136
|
+
- Registration runs at module top level, before Pi calls your default export, so
|
|
137
|
+
every locale map is in the registry before the first lookup fires.
|
|
138
|
+
|
|
139
|
+
## 5. Use `t(key, fallback)` at the render call site
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
import { t } from "../state/i18n-bridge.js";
|
|
143
|
+
|
|
144
|
+
// ✓ Render-time — re-evaluated each render; live `/languages` switches apply
|
|
145
|
+
function renderHeader(theme) {
|
|
146
|
+
return new Text(theme.bold(t("welcome.title", "Welcome")));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ✗ Module-init — captured ONCE, freezes English on first load
|
|
150
|
+
const HEADING = t("welcome.title", "Welcome");
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
The fallback is the canonical English literal — the same one in `en.json`. Keep
|
|
154
|
+
it inline so the file reads end-to-end without locale lookups, and so your
|
|
155
|
+
extension stays usable when the SDK is not installed at all.
|
|
156
|
+
|
|
157
|
+
## 6. Ship the locale files in `package.json`
|
|
158
|
+
|
|
159
|
+
```json
|
|
160
|
+
{
|
|
161
|
+
"files": [
|
|
162
|
+
"index.ts",
|
|
163
|
+
"state/i18n-bridge.ts",
|
|
164
|
+
"locales/",
|
|
165
|
+
"…"
|
|
166
|
+
]
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
The `files[]` manifest is the most common publish-time miss in this monorepo's
|
|
171
|
+
history. Ship it in the same commit as the locale JSONs.
|
|
172
|
+
|
|
173
|
+
## What stays English
|
|
174
|
+
|
|
175
|
+
Do **not** route these through `t(...)`:
|
|
176
|
+
|
|
177
|
+
- Tool descriptions, TypeBox `description` fields, prompt guidelines and
|
|
178
|
+
snippets — they go to the LLM. Localizing them risks the model emitting
|
|
179
|
+
localized option labels that bypass exact-string validation.
|
|
180
|
+
- Validation errors that flow back through `tool result` envelopes — same
|
|
181
|
+
reason.
|
|
182
|
+
- Anything checked by exact-string matching (reserved labels, dispatcher
|
|
183
|
+
discriminants) — keep both sides in canonical English.
|
|
184
|
+
|
|
185
|
+
The recommended pattern: keep a top-level `const X = "literal"` for the
|
|
186
|
+
canonical English so reserved-label checks and tests stay stable, then route the
|
|
187
|
+
**render call site** through `t("key", X)`. The SDK never sees `X`; the LLM never
|
|
188
|
+
sees `t(...)`.
|
|
189
|
+
|
|
190
|
+
## Optional: a per-namespace `displayLabel` helper
|
|
191
|
+
|
|
192
|
+
If your extension has a small enum-typed set of "kind" rows (sentinels,
|
|
193
|
+
statuses, modes), a one-line helper keeps render code tight:
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
// state/i18n-bridge.ts
|
|
197
|
+
import { ROW_INTENT_META, type SentinelKind } from "./row-intent.js";
|
|
198
|
+
|
|
199
|
+
export function displayLabel(kind: SentinelKind): string {
|
|
200
|
+
return t(`sentinel.${kind}`, ROW_INTENT_META[kind].label);
|
|
201
|
+
}
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
Render code becomes `displayLabel("next")` instead of
|
|
205
|
+
`t("sentinel.next", "Next")` — same lookup, but the canonical English fallback
|
|
206
|
+
comes from the same metadata table the rest of your code uses.
|
|
207
|
+
|
|
208
|
+
## Verify it works before publishing
|
|
209
|
+
|
|
210
|
+
The SDK only flips strings inside a real Pi session — `npm test` will not catch
|
|
211
|
+
a missing `files[]` entry or a wrong namespace. Smoke-test against a live shell:
|
|
212
|
+
|
|
213
|
+
```sh
|
|
214
|
+
# from your extension's directory
|
|
215
|
+
pi install ./ # register the package directory
|
|
216
|
+
pi install npm:@juicesharp/rpiv-i18n # if not already installed
|
|
217
|
+
pi # launch the session
|
|
218
|
+
> /languages # pick a non-English locale
|
|
219
|
+
> <invoke a command from your extension> # confirm the strings flip
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
`pi install` takes an npm spec, a git/https/ssh URL, or a local path — it never
|
|
223
|
+
unpacks a tarball, so `npm pack` output is not an install source. A local path is
|
|
224
|
+
registered in place, which means your working tree is what the session loads.
|
|
225
|
+
|
|
226
|
+
That is what makes it a runtime check, and also what it cannot check: a local
|
|
227
|
+
install reads your files regardless of `package.json` `files[]`. Verify the
|
|
228
|
+
publish manifest separately — `npm pack --dry-run` lists exactly what would ship,
|
|
229
|
+
and `locales/` must be in it.
|
|
230
|
+
|
|
231
|
+
The failure mode this session catches that unit tests do not is **module-init
|
|
232
|
+
lookup capture** — the picker switches, other extensions flip, yours does not.
|
|
233
|
+
Fix: move the `t(...)` call inside the render function.
|
|
234
|
+
|
|
235
|
+
To make a locale appear in the `/languages` picker itself, it must exist in
|
|
236
|
+
`SUPPORTED_LOCALES` in this package's `i18n.ts` — open a PR, or an issue if your
|
|
237
|
+
extension lives outside this repo.
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# SDK reference
|
|
2
|
+
|
|
3
|
+
The complete public surface of `@juicesharp/rpiv-i18n` — every exported function,
|
|
4
|
+
its behavior contract, the locale detection chain, and the `globalThis` escape
|
|
5
|
+
hatch. For a task-oriented walkthrough of adding localization to your own
|
|
6
|
+
extension, read [integration-guide.md](./integration-guide.md) first.
|
|
7
|
+
|
|
8
|
+
## Import surface
|
|
9
|
+
|
|
10
|
+
The package ships two entry points:
|
|
11
|
+
|
|
12
|
+
| Specifier | Module | Use it for |
|
|
13
|
+
| --- | --- | --- |
|
|
14
|
+
| `@juicesharp/rpiv-i18n` | `i18n.ts` | Lookup and locale state — `scope`, `tr`, `registerStrings`, `applyLocale` |
|
|
15
|
+
| `@juicesharp/rpiv-i18n/loader` | `loader.ts` | Registering a `locales/` directory — `registerLocalesFromDir` |
|
|
16
|
+
|
|
17
|
+
The `/loader` subpath exists so a consumer that only needs to register strings
|
|
18
|
+
does not pull `i18n-ui.ts` — and with it the `@earendil-works/pi-tui`
|
|
19
|
+
dependency — into its load graph.
|
|
20
|
+
|
|
21
|
+
## `@juicesharp/rpiv-i18n`
|
|
22
|
+
|
|
23
|
+
### Registration and lookup
|
|
24
|
+
|
|
25
|
+
| Export | Signature | Behavior |
|
|
26
|
+
| --- | --- | --- |
|
|
27
|
+
| `registerStrings` | `(namespace: string, byLocale: LocaleStrings) => void` | Registers a package's translation maps under `namespace` (use your npm package name). Each map is frozen on registration. Re-calling with the same namespace **replaces** the prior registration. |
|
|
28
|
+
| `tr` | `(namespace: string, key: string, fallback: string) => string` | One-shot render-time lookup. Never throws. |
|
|
29
|
+
| `scope` | `(namespace: string) => (key: string, fallback: string) => string` | Binds a namespace once and returns a closure. The closure reads live state on every call, so it follows locale changes. |
|
|
30
|
+
|
|
31
|
+
### Locale state
|
|
32
|
+
|
|
33
|
+
| Export | Signature | Behavior |
|
|
34
|
+
| --- | --- | --- |
|
|
35
|
+
| `applyLocale` | `(locale: string \| undefined) => void` | Sets the active locale and rebuilds the active string set across **all** registered namespaces. `undefined` means the English default. |
|
|
36
|
+
| `getActiveLocale` | `() => string \| undefined` | The active locale code, or `undefined` for the English default. |
|
|
37
|
+
| `detectLocaleFromConfigAndEnv` | `() => string \| undefined` | Resolves persisted config, then `LANG`, then `LC_ALL`. Does not read the `--locale` flag. |
|
|
38
|
+
|
|
39
|
+
### Config persistence
|
|
40
|
+
|
|
41
|
+
| Export | Signature | Behavior |
|
|
42
|
+
| --- | --- | --- |
|
|
43
|
+
| `loadLocaleConfig` | `() => LocaleConfig` | Reads the XDG config path; falls back to the legacy `~/.config` path only when the XDG path does not exist. Returns `{}` when both are absent. |
|
|
44
|
+
| `saveLocaleConfig` | `(locale: string \| undefined) => boolean` | Writes `{"locale":"<code>"}` to the XDG path. Passing `undefined` omits the key entirely. Returns `false` when the write fails — callers must react. |
|
|
45
|
+
|
|
46
|
+
### Constants and types
|
|
47
|
+
|
|
48
|
+
| Export | Value |
|
|
49
|
+
| --- | --- |
|
|
50
|
+
| `SUPPORTED_LOCALES` | `readonly { code: string; label: string }[]` — the nine picker entries, alphabetical by code, labelled with endonyms. |
|
|
51
|
+
| `I18N_STATE_KEY` | `Symbol.for("rpiv-i18n")` — the key of the public snapshot on `globalThis`. |
|
|
52
|
+
| `__resetState` | Test-only hook that clears the registry, active strings, and locale. |
|
|
53
|
+
| Types | `TranslationMap`, `LocaleStrings`, `I18nState`, `LocaleConfig` |
|
|
54
|
+
|
|
55
|
+
## `@juicesharp/rpiv-i18n/loader`
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
registerLocalesFromDir(
|
|
59
|
+
namespace: string,
|
|
60
|
+
packageUrl: string, // pass import.meta.url from YOUR package
|
|
61
|
+
options?: { label?: string }, // warn-message prefix; defaults to namespace
|
|
62
|
+
): void
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Reads `./locales/<code>.json` relative to `packageUrl` for every code in
|
|
66
|
+
`SUPPORTED_LOCALES`, then calls `registerStrings(namespace, byLocale)` once.
|
|
67
|
+
Files are read from the *caller's* package, never from rpiv-i18n.
|
|
68
|
+
|
|
69
|
+
A per-file failure (missing file, malformed JSON, `EACCES`) emits
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
<label>: failed to load locales/<code>.json — falling back to English (<message>)
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
on `console.warn` and records an empty map for that locale. Module init never
|
|
76
|
+
crashes because of a locale-file mistake.
|
|
77
|
+
|
|
78
|
+
Also exported: the type `RegisterLocalesFromDirOptions`.
|
|
79
|
+
|
|
80
|
+
## Behavior contract
|
|
81
|
+
|
|
82
|
+
- **English fallback per key.** The active locale's map is spread over the `en`
|
|
83
|
+
map and the result frozen. A key present in `en` but missing from the active
|
|
84
|
+
locale resolves to the English string, not to blank.
|
|
85
|
+
- **Caller fallback as the last resort.** If the namespace is unregistered, the
|
|
86
|
+
key is missing from both maps, **or the stored value is an empty string**,
|
|
87
|
+
`tr` returns the `fallback` argument you passed at the call site.
|
|
88
|
+
- **Live locale changes.** After `/languages` writes a new locale, the next
|
|
89
|
+
`tr(...)` call returns the new string. No restart.
|
|
90
|
+
- **Render-time only.** Call `tr(...)` inside the render function. A top-level
|
|
91
|
+
`const HEADING = t("welcome.title", "Welcome")` is evaluated at module init
|
|
92
|
+
and freezes English before the user's locale is ever applied.
|
|
93
|
+
- **Multi-instance safe.** All mutable state lives on
|
|
94
|
+
`globalThis[Symbol.for("rpiv-i18n.runtime")]`, so a locale change propagates
|
|
95
|
+
even when Pi's TypeScript loader resolves the module under two paths and each
|
|
96
|
+
`import { tr }` would otherwise see a private copy.
|
|
97
|
+
|
|
98
|
+
## Locale detection chain
|
|
99
|
+
|
|
100
|
+
`applyLocale` runs at exactly three points: module init, the `session_start`
|
|
101
|
+
hook, and each `/languages` selection. The value it receives resolves in this
|
|
102
|
+
order:
|
|
103
|
+
|
|
104
|
+
1. `--locale <code>` — read from the flag, but **only** during `session_start`.
|
|
105
|
+
Module-init resolution never sees it.
|
|
106
|
+
2. The persisted config file (XDG path, then the legacy path).
|
|
107
|
+
3. `process.env.LANG`, then `process.env.LC_ALL`. Parsed as the language
|
|
108
|
+
segment of `<lang>_<REGION>.<charset>` — `uk_UA.UTF-8` yields `uk`. The
|
|
109
|
+
values `C` and `POSIX` are rejected.
|
|
110
|
+
4. `undefined` — the silent English default.
|
|
111
|
+
|
|
112
|
+
`SUPPORTED_LOCALES` gates the `/languages` picker list *and* the set of files
|
|
113
|
+
`registerLocalesFromDir` reads — it iterates the list, so a `locales/<code>.json`
|
|
114
|
+
whose code is absent is never read or registered. A direct `registerStrings` call
|
|
115
|
+
bypasses that: it may register any locale code, and codes outside the list simply
|
|
116
|
+
do not appear in the picker.
|
|
117
|
+
|
|
118
|
+
The persisted config is read from the XDG path, falling back to the pre-2.0.0
|
|
119
|
+
`~/.config/rpiv-i18n/locale.json` only when the XDG path does not exist. Writes
|
|
120
|
+
always go to the XDG path.
|
|
121
|
+
|
|
122
|
+
## `globalThis` introspection escape hatch
|
|
123
|
+
|
|
124
|
+
For tools that prefer not to import this package, the active state is published
|
|
125
|
+
at `globalThis[Symbol.for("rpiv-i18n")]` as a frozen
|
|
126
|
+
`{ locale, namespaces }` plain-data snapshot:
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
const I18N = Symbol.for("rpiv-i18n");
|
|
130
|
+
|
|
131
|
+
function lookup(key: string, fallback: string): string {
|
|
132
|
+
// Re-read the symbol on every call — the SDK *replaces* the snapshot on
|
|
133
|
+
// every registerStrings/applyLocale, so a cached reference silently serves
|
|
134
|
+
// stale strings after `/languages`.
|
|
135
|
+
const state = (globalThis as { [k: symbol]: unknown })[I18N] as
|
|
136
|
+
| { locale: string | undefined; namespaces: Record<string, Record<string, string>> }
|
|
137
|
+
| undefined;
|
|
138
|
+
return state?.namespaces["@my-org/cool-tool"]?.[key] ?? fallback;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
lookup("welcome", "Welcome!");
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Each locale change produces a **new** frozen object — read the symbol at call
|
|
145
|
+
time, never hoist it into a module-scope `const`. Registration must still go
|
|
146
|
+
through `registerStrings(...)`; writing into `globalThis[I18N]` directly is
|
|
147
|
+
unsupported.
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# Contributing translations
|
|
2
|
+
|
|
3
|
+
How to add or improve a translation for any Pi extension that uses
|
|
4
|
+
`@juicesharp/rpiv-i18n`. The contract is small and uniform across packages, so
|
|
5
|
+
what you learn here applies to every one of them.
|
|
6
|
+
|
|
7
|
+
## What to translate
|
|
8
|
+
|
|
9
|
+
Translate every key listed in the package's `locales/en.json`. Those are the
|
|
10
|
+
TUI-facing strings — labels, hints, prompts, headings — that a human reads on
|
|
11
|
+
screen.
|
|
12
|
+
|
|
13
|
+
Do **not** translate:
|
|
14
|
+
|
|
15
|
+
- Tool descriptions, TypeBox `description` fields, prompt guidelines and
|
|
16
|
+
snippets. They go to the LLM and stay English so the model parses them
|
|
17
|
+
deterministically across sessions and providers.
|
|
18
|
+
- Validation errors that flow through `tool result` envelopes (for example
|
|
19
|
+
`"Error: UI not available …"`) — same reason.
|
|
20
|
+
- Reserved labels and any string checked by exact-match validation. Translating
|
|
21
|
+
those lets a localized equivalent slip past duplicate-detection guards.
|
|
22
|
+
|
|
23
|
+
If a key is not in the package's `locales/en.json`, it is intentionally
|
|
24
|
+
English-only. Do not invent new keys; open an issue first if you think a string
|
|
25
|
+
should be made localizable.
|
|
26
|
+
|
|
27
|
+
## File location and naming
|
|
28
|
+
|
|
29
|
+
- One JSON file per locale, named `<code>.json` — `es.json`, `fr.json`,
|
|
30
|
+
`pt-BR.json` — inside the consumer package's `locales/` directory.
|
|
31
|
+
- Locale codes follow BCP-47-ish convention: language only (`es`, `fr`, `de`) or
|
|
32
|
+
`language-Region` for variants (`pt-BR`). Hyphenated, never underscored.
|
|
33
|
+
- Mirror the exact key set from `en.json`. Missing keys fall back to English
|
|
34
|
+
silently — acceptable, but note the gap in `_meta.notes`.
|
|
35
|
+
|
|
36
|
+
## Key naming
|
|
37
|
+
|
|
38
|
+
Keys are flat and dotted, lowercase: `sentinel.next`, `submit.cancel`,
|
|
39
|
+
`preview.no_preview`. Prefer `snake_case` for multi-word leaves. Whatever the
|
|
40
|
+
shape, the binding rule is that your file mirrors `en.json` key for key — you do
|
|
41
|
+
not choose key names, you copy them.
|
|
42
|
+
|
|
43
|
+
## File shape
|
|
44
|
+
|
|
45
|
+
```json
|
|
46
|
+
{
|
|
47
|
+
"_meta.notes": "Optional contributor note — auto-translated, native review welcome, key gaps, etc.",
|
|
48
|
+
|
|
49
|
+
"<dotted.key.from.en.json>": "Localized string"
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
`_meta.*` keys are never requested by a lookup, so they are inert at runtime.
|
|
54
|
+
Use them for provenance, change notes, or "WIP — N keys missing".
|
|
55
|
+
|
|
56
|
+
Values matter: an **empty string resolves to the English fallback**, not to
|
|
57
|
+
blank. Leave a key out entirely rather than shipping `""` as a placeholder.
|
|
58
|
+
|
|
59
|
+
## Universal CLI conventions — leave untranslated
|
|
60
|
+
|
|
61
|
+
- Symbols: `↑/↓`, `⚠`, `✓`. They render the same in every locale.
|
|
62
|
+
- Keyboard names: `Enter`, `Esc`, `Tab`, `Space` — the labels printed on
|
|
63
|
+
physical keyboards worldwide. Some locales (French, for instance) write
|
|
64
|
+
`Entrée` / `Échap`; that is acceptable when it matches the desktop convention
|
|
65
|
+
you are targeting, but be consistent across the whole file.
|
|
66
|
+
- Single-key shortcut letters (`n` to add notes): keep the letter unchanged. It
|
|
67
|
+
maps to a literal keystroke handler, not to a label.
|
|
68
|
+
|
|
69
|
+
## Wiring a new locale
|
|
70
|
+
|
|
71
|
+
Dropping the file in `locales/` is all a consumer package needs **when the code
|
|
72
|
+
is already listed in `SUPPORTED_LOCALES`** — its `registerLocalesFromDir(...)`
|
|
73
|
+
call iterates that list and picks up every matching file automatically.
|
|
74
|
+
|
|
75
|
+
For a *new* locale code, the `SUPPORTED_LOCALES` entry in
|
|
76
|
+
`packages/rpiv-i18n/i18n.ts` is required. Without it the loader never reads your
|
|
77
|
+
file, so the strings are never registered — the locale does nothing even when a
|
|
78
|
+
user selects it with `--locale` or `LANG`, and it never appears in the
|
|
79
|
+
`/languages` picker:
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
{ code: "fr", label: "Français" },
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
The `label` is the endonym — the language's name in itself — not its English
|
|
86
|
+
name. The list is kept alphabetical by code.
|
|
87
|
+
|
|
88
|
+
## Submitting a PR
|
|
89
|
+
|
|
90
|
+
Include:
|
|
91
|
+
|
|
92
|
+
1. The new `locales/<code>.json` file.
|
|
93
|
+
2. The `SUPPORTED_LOCALES` entry in `packages/rpiv-i18n/i18n.ts`, if the locale
|
|
94
|
+
is not already listed.
|
|
95
|
+
3. A passing `npm run check && npm test` from the monorepo root.
|
|
96
|
+
|
|
97
|
+
A native-speaker reviewer will land it. Auto-translated drafts are accepted —
|
|
98
|
+
mark them in `_meta.notes`. English fallbacks make any gap or error invisible to
|
|
99
|
+
users until a fix arrives, so a partial translation is never a regression.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@juicesharp/rpiv-i18n",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
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",
|
|
@@ -33,6 +33,9 @@
|
|
|
33
33
|
"i18n-ui.ts",
|
|
34
34
|
"loader.ts",
|
|
35
35
|
"docs/",
|
|
36
|
+
"!docs/*.png",
|
|
37
|
+
"!docs/*.jpg",
|
|
38
|
+
"!docs/*.svg",
|
|
36
39
|
"README.md",
|
|
37
40
|
"LICENSE"
|
|
38
41
|
],
|
|
@@ -46,7 +49,7 @@
|
|
|
46
49
|
]
|
|
47
50
|
},
|
|
48
51
|
"dependencies": {
|
|
49
|
-
"@juicesharp/rpiv-config": "^2.
|
|
52
|
+
"@juicesharp/rpiv-config": "^2.2.0"
|
|
50
53
|
},
|
|
51
54
|
"peerDependencies": {
|
|
52
55
|
"@earendil-works/pi-coding-agent": "*",
|
package/docs/cover.png
DELETED
|
Binary file
|
package/docs/cover.svg
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1280 800" role="img" aria-label="rpiv-i18n — i18n for any Pi extension in 3 lines" preserveAspectRatio="xMidYMid meet">
|
|
2
|
-
<title>rpiv-i18n — i18n for any Pi extension in 3 lines</title>
|
|
3
|
-
<defs>
|
|
4
|
-
<style>
|
|
5
|
-
.mono { font-family: 'JetBrains Mono', 'Fira Code', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
|
|
6
|
-
.serif { font-family: 'Iowan Old Style', 'Hoefler Text', Charter, 'Source Serif Pro', Georgia, serif; }
|
|
7
|
-
.ink { fill: #ede6d3; }
|
|
8
|
-
.text-quiet { fill: #a8a29e; }
|
|
9
|
-
.text-distant { fill: #9c968d; }
|
|
10
|
-
.ochre { fill: #a8896c; }
|
|
11
|
-
.sage { fill: #8a9580; }
|
|
12
|
-
.sage-deep { fill: #5e6b56; }
|
|
13
|
-
</style>
|
|
14
|
-
<radialGradient id="wash-ochre" cx="25%" cy="10%" r="65%">
|
|
15
|
-
<stop offset="0%" stop-color="#a8896c" stop-opacity="0.10"/>
|
|
16
|
-
<stop offset="100%" stop-color="#a8896c" stop-opacity="0"/>
|
|
17
|
-
</radialGradient>
|
|
18
|
-
<radialGradient id="wash-sage" cx="85%" cy="90%" r="55%">
|
|
19
|
-
<stop offset="0%" stop-color="#8a9580" stop-opacity="0.07"/>
|
|
20
|
-
<stop offset="100%" stop-color="#8a9580" stop-opacity="0"/>
|
|
21
|
-
</radialGradient>
|
|
22
|
-
</defs>
|
|
23
|
-
|
|
24
|
-
<rect width="1280" height="800" fill="#1c1a17"/>
|
|
25
|
-
<rect width="1280" height="800" fill="url(#wash-ochre)"/>
|
|
26
|
-
<rect width="1280" height="800" fill="url(#wash-sage)"/>
|
|
27
|
-
|
|
28
|
-
<g fill="none" stroke="#8a9580" stroke-linecap="round" opacity="0.45">
|
|
29
|
-
<path d="M1120 60 q36 -52 88 -60 q40 -5 68 14" stroke-width="1.1"/>
|
|
30
|
-
<path d="M1140 80 q28 -18 60 -14 q24 3 42 14" stroke-width="0.7" opacity="0.6"/>
|
|
31
|
-
<circle cx="1210" cy="32" r="2" opacity="0.5"/>
|
|
32
|
-
<circle cx="1160" cy="60" r="1.4" opacity="0.4"/>
|
|
33
|
-
</g>
|
|
34
|
-
|
|
35
|
-
<text class="mono text-distant" x="80" y="64" font-size="11" letter-spacing="0.18em">PI AGENT EXTENSION</text>
|
|
36
|
-
|
|
37
|
-
<text class="serif ink" x="80" y="152" font-size="80" font-weight="700" letter-spacing="-2.5">rpiv<tspan class="ochre">-</tspan>i18n</text>
|
|
38
|
-
<text class="mono text-quiet" x="80" y="196" font-size="19" letter-spacing="0.2">i18n for any Pi extension — in 3 lines.</text>
|
|
39
|
-
|
|
40
|
-
<line x1="80" y1="228" x2="1200" y2="228" stroke="#2e2b27" stroke-width="1"/>
|
|
41
|
-
|
|
42
|
-
<rect x="80" y="264" width="1120" height="440" rx="3" fill="#1c1a17" stroke="#3a3631" stroke-width="1"/>
|
|
43
|
-
|
|
44
|
-
<!-- Locales strip -->
|
|
45
|
-
<text class="mono text-distant" x="120" y="300" font-size="11" letter-spacing="0.2em">LOCALES</text>
|
|
46
|
-
|
|
47
|
-
<rect x="220" y="284" width="68" height="24" rx="2" fill="#2d2925" stroke="#8a9580" stroke-width="1.2"/>
|
|
48
|
-
<text class="mono sage" x="254" y="301" font-size="13" font-weight="700" text-anchor="middle" xml:space="preserve">✓ EN</text>
|
|
49
|
-
|
|
50
|
-
<rect x="296" y="284" width="56" height="24" rx="2" fill="none" stroke="#3a3631" stroke-width="1"/>
|
|
51
|
-
<text class="mono text-distant" x="324" y="301" font-size="13" text-anchor="middle">DE</text>
|
|
52
|
-
|
|
53
|
-
<text class="mono text-distant" x="364" y="301" font-size="12" letter-spacing="0.1em">+ any other</text>
|
|
54
|
-
|
|
55
|
-
<text class="mono text-distant" x="1160" y="301" font-size="11" letter-spacing="0.15em" text-anchor="end">/languages · --locale</text>
|
|
56
|
-
|
|
57
|
-
<line x1="120" y1="324" x2="1160" y2="324" stroke="#2e2b27" stroke-width="1"/>
|
|
58
|
-
|
|
59
|
-
<!-- 01 register -->
|
|
60
|
-
<text class="mono text-distant" x="120" y="372" font-size="11" letter-spacing="0.2em">01</text>
|
|
61
|
-
<text class="mono" x="170" y="372" font-size="22"><tspan class="ochre" font-weight="700">registerStrings</tspan><tspan class="text-quiet">(</tspan><tspan class="sage">'@me/tool'</tspan><tspan class="text-quiet">, { </tspan><tspan class="sage">en</tspan><tspan class="text-quiet">, </tspan><tspan class="sage">de</tspan><tspan class="text-quiet"> })</tspan></text>
|
|
62
|
-
|
|
63
|
-
<text class="mono sage" x="170" y="412" font-size="22" font-weight="700">↓</text>
|
|
64
|
-
|
|
65
|
-
<!-- 02 scope -->
|
|
66
|
-
<text class="mono text-distant" x="120" y="460" font-size="11" letter-spacing="0.2em">02</text>
|
|
67
|
-
<text class="mono" x="170" y="460" font-size="22"><tspan class="text-distant">const </tspan><tspan class="ink" font-weight="700">t</tspan><tspan class="text-distant"> = </tspan><tspan class="ochre" font-weight="700">scope</tspan><tspan class="text-quiet">(</tspan><tspan class="sage">'@me/tool'</tspan><tspan class="text-quiet">)</tspan></text>
|
|
68
|
-
|
|
69
|
-
<text class="mono sage" x="170" y="500" font-size="22" font-weight="700">↓</text>
|
|
70
|
-
|
|
71
|
-
<!-- 03 translate -->
|
|
72
|
-
<text class="mono text-distant" x="120" y="548" font-size="11" letter-spacing="0.2em">03</text>
|
|
73
|
-
<text class="mono" x="170" y="548" font-size="22"><tspan class="ink" font-weight="700">t</tspan><tspan class="text-quiet">(</tspan><tspan class="sage">'hint.enter'</tspan><tspan class="text-quiet">, </tspan><tspan class="sage">'Enter to select'</tspan><tspan class="text-quiet">)</tspan></text>
|
|
74
|
-
|
|
75
|
-
<text class="mono" x="170" y="586" font-size="20"><tspan class="text-distant">➞ </tspan><tspan class="sage" font-weight="700">"Eingabe zum Auswählen"</tspan></text>
|
|
76
|
-
|
|
77
|
-
<!-- Caption -->
|
|
78
|
-
<text class="mono" x="120" y="660" font-size="13" letter-spacing="0.12em"><tspan class="sage" font-weight="700">[3 lines]</tspan> <tspan class="text-distant">REGISTER · SCOPE · TRANSLATE · ENGLISH FALLBACK PER KEY</tspan></text>
|
|
79
|
-
|
|
80
|
-
<line x1="80" y1="740" x2="1200" y2="740" stroke="#2e2b27" stroke-width="1"/>
|
|
81
|
-
|
|
82
|
-
<text class="mono text-distant" x="80" y="776" font-size="12" letter-spacing="0.12em">
|
|
83
|
-
<tspan class="ochre" font-weight="700">▌ rpiv-i18n</tspan>
|
|
84
|
-
<tspan dx="12">│</tspan>
|
|
85
|
-
<tspan dx="12" class="sage">EN · DE · + any other</tspan>
|
|
86
|
-
<tspan dx="12">│</tspan>
|
|
87
|
-
<tspan dx="12">npm:@juicesharp/rpiv-i18n</tspan>
|
|
88
|
-
</text>
|
|
89
|
-
</svg>
|