@juicesharp/rpiv-i18n 1.1.2 → 1.1.3
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 +59 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -112,6 +112,33 @@ export default function (pi: ExtensionAPI) {
|
|
|
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. Same shape proven in production.
|
|
114
114
|
|
|
115
|
+
End state on disk:
|
|
116
|
+
|
|
117
|
+
```
|
|
118
|
+
my-extension/
|
|
119
|
+
├── index.ts ← default export + registerStrings(...)
|
|
120
|
+
├── state/
|
|
121
|
+
│ └── i18n-bridge.ts ← exports `t` + I18N_NAMESPACE
|
|
122
|
+
├── locales/
|
|
123
|
+
│ ├── en.json ← canonical baseline (required)
|
|
124
|
+
│ ├── uk.json ← optional additional locales
|
|
125
|
+
│ └── …
|
|
126
|
+
└── package.json ← peerDependencies + files[] + pi.extensions
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### 0. Declare the SDK as a peer dependency
|
|
130
|
+
|
|
131
|
+
```json
|
|
132
|
+
{
|
|
133
|
+
"peerDependencies": {
|
|
134
|
+
"@juicesharp/rpiv-i18n": "*",
|
|
135
|
+
"@mariozechner/pi-coding-agent": "*"
|
|
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
|
+
|
|
115
142
|
### 1. Author a `locales/en.json` next to your source
|
|
116
143
|
|
|
117
144
|
```json
|
|
@@ -145,6 +172,7 @@ Every render-time call site imports `t` from this one file. If you ever switch n
|
|
|
145
172
|
```ts
|
|
146
173
|
import { readFileSync } from "node:fs";
|
|
147
174
|
import { fileURLToPath } from "node:url";
|
|
175
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
148
176
|
import { registerStrings, type TranslationMap } from "@juicesharp/rpiv-i18n";
|
|
149
177
|
import { I18N_NAMESPACE } from "./state/i18n-bridge.js";
|
|
150
178
|
|
|
@@ -167,7 +195,7 @@ registerStrings(I18N_NAMESPACE, {
|
|
|
167
195
|
// map fall back to en automatically.
|
|
168
196
|
});
|
|
169
197
|
|
|
170
|
-
export default function (pi
|
|
198
|
+
export default function (pi: ExtensionAPI): void {
|
|
171
199
|
// your tool/command/hook registrations here…
|
|
172
200
|
}
|
|
173
201
|
```
|
|
@@ -246,6 +274,24 @@ Wire it into `registerStrings({ en: …, uk: loadLocale("uk") })`. To make the l
|
|
|
246
274
|
|
|
247
275
|
Done. `/languages` now switches your extension's UI alongside every other extension that adopts the SDK.
|
|
248
276
|
|
|
277
|
+
### Verify it works locally before publishing
|
|
278
|
+
|
|
279
|
+
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:
|
|
280
|
+
|
|
281
|
+
```bash
|
|
282
|
+
# from your extension's directory
|
|
283
|
+
npm pack # produces my-extension-x.y.z.tgz
|
|
284
|
+
pi install file:./my-extension-x.y.z.tgz # install into your Pi session
|
|
285
|
+
pi install npm:@juicesharp/rpiv-i18n # if not already installed
|
|
286
|
+
pi # launch the session
|
|
287
|
+
> /languages # pick a non-English locale
|
|
288
|
+
> <invoke a command/tool from your extension> # confirm the strings flip
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
Two failure modes this catches that unit tests do not:
|
|
292
|
+
1. **Locale JSON not shipped** — `/languages` switches but your strings stay English. Fix: add `"locales/"` to `package.json` `files[]`.
|
|
293
|
+
2. **Module-init `tr(...)` capture** — picker switches, other extensions flip, yours doesn't. Fix: move the `t(...)` call inside the render function.
|
|
294
|
+
|
|
249
295
|
## Contributing translations
|
|
250
296
|
|
|
251
297
|
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.
|
|
@@ -326,11 +372,18 @@ For tools that prefer not to import this package, the active state is also publi
|
|
|
326
372
|
|
|
327
373
|
```ts
|
|
328
374
|
const I18N = Symbol.for("rpiv-i18n");
|
|
329
|
-
const state = (globalThis as any)[I18N] as
|
|
330
|
-
| { locale: string | undefined; namespaces: Record<string, Record<string, string>> }
|
|
331
|
-
| undefined;
|
|
332
375
|
|
|
333
|
-
|
|
376
|
+
function lookup(key: string, fallback: string): string {
|
|
377
|
+
// Re-read the symbol on every call — the SDK *replaces* the snapshot
|
|
378
|
+
// (frozen object) on every registerStrings/applyLocale, so a cached
|
|
379
|
+
// reference will silently serve stale strings after `/languages`.
|
|
380
|
+
const state = (globalThis as { [k: symbol]: unknown })[I18N] as
|
|
381
|
+
| { locale: string | undefined; namespaces: Record<string, Record<string, string>> }
|
|
382
|
+
| undefined;
|
|
383
|
+
return state?.namespaces["@my-org/cool-tool"]?.[key] ?? fallback;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
lookup("welcome", "Welcome!");
|
|
334
387
|
```
|
|
335
388
|
|
|
336
|
-
|
|
389
|
+
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