@juicesharp/rpiv-i18n 1.1.3 → 1.1.4
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 +38 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -110,7 +110,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
110
110
|
|
|
111
111
|
## Localizing your extension — step by step
|
|
112
112
|
|
|
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.
|
|
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
|
|
|
115
115
|
End state on disk:
|
|
116
116
|
|
|
@@ -126,19 +126,24 @@ my-extension/
|
|
|
126
126
|
└── package.json ← peerDependencies + files[] + pi.extensions
|
|
127
127
|
```
|
|
128
128
|
|
|
129
|
-
### 0. Declare the SDK as
|
|
129
|
+
### 0. Declare the SDK as an OPTIONAL peer dependency
|
|
130
130
|
|
|
131
131
|
```json
|
|
132
132
|
{
|
|
133
133
|
"peerDependencies": {
|
|
134
134
|
"@juicesharp/rpiv-i18n": "*",
|
|
135
135
|
"@mariozechner/pi-coding-agent": "*"
|
|
136
|
+
},
|
|
137
|
+
"peerDependenciesMeta": {
|
|
138
|
+
"@juicesharp/rpiv-i18n": { "optional": true }
|
|
136
139
|
}
|
|
137
140
|
}
|
|
138
141
|
```
|
|
139
142
|
|
|
140
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.
|
|
141
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 — your extension stays online with English-only UI when the SDK isn't installed, and lights up localization automatically when it is.
|
|
146
|
+
|
|
142
147
|
### 1. Author a `locales/en.json` next to your source
|
|
143
148
|
|
|
144
149
|
```json
|
|
@@ -155,27 +160,43 @@ Conventions: flat dotted lowercase keys, `snake_case` only for multi-word leaves
|
|
|
155
160
|
|
|
156
161
|
### 2. Add a one-file bridge inside your package
|
|
157
162
|
|
|
158
|
-
`state/i18n-bridge.ts` (or wherever your package keeps cross-cutting helpers):
|
|
163
|
+
`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:
|
|
159
164
|
|
|
160
165
|
```ts
|
|
161
|
-
import { scope } from "@juicesharp/rpiv-i18n";
|
|
162
|
-
|
|
163
166
|
export const I18N_NAMESPACE = "@my-org/cool-tool";
|
|
164
167
|
|
|
165
|
-
|
|
168
|
+
type ScopeFn = (key: string, fallback: string) => string;
|
|
169
|
+
type I18nSDK = { scope: (namespace: string) => ScopeFn };
|
|
170
|
+
|
|
171
|
+
let scopeImpl: ScopeFn;
|
|
172
|
+
try {
|
|
173
|
+
const sdk = (await import("@juicesharp/rpiv-i18n")) as I18nSDK;
|
|
174
|
+
scopeImpl = sdk.scope(I18N_NAMESPACE);
|
|
175
|
+
} catch {
|
|
176
|
+
// SDK not installed — every t(key, fallback) returns the fallback verbatim.
|
|
177
|
+
scopeImpl = (_key, fallback) => fallback;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export const t: ScopeFn = scopeImpl;
|
|
166
181
|
```
|
|
167
182
|
|
|
168
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.
|
|
169
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 — 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
|
+
|
|
170
187
|
### 3. Register strings at extension load
|
|
171
188
|
|
|
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
|
+
|
|
172
191
|
```ts
|
|
173
192
|
import { readFileSync } from "node:fs";
|
|
174
193
|
import { fileURLToPath } from "node:url";
|
|
175
194
|
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
176
|
-
import { registerStrings, type TranslationMap } from "@juicesharp/rpiv-i18n";
|
|
177
195
|
import { I18N_NAMESPACE } from "./state/i18n-bridge.js";
|
|
178
196
|
|
|
197
|
+
type TranslationMap = Readonly<Record<string, string>>;
|
|
198
|
+
type I18nSDK = { registerStrings: (ns: string, byLocale: Record<string, TranslationMap>) => void };
|
|
199
|
+
|
|
179
200
|
function loadLocale(code: string): TranslationMap {
|
|
180
201
|
// A missing/malformed locale file MUST NOT crash module init — the bridge's
|
|
181
202
|
// literal English fallbacks will keep every render readable.
|
|
@@ -189,11 +210,16 @@ function loadLocale(code: string): TranslationMap {
|
|
|
189
210
|
}
|
|
190
211
|
}
|
|
191
212
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
213
|
+
try {
|
|
214
|
+
const sdk = (await import("@juicesharp/rpiv-i18n")) as I18nSDK;
|
|
215
|
+
sdk.registerStrings(I18N_NAMESPACE, {
|
|
216
|
+
en: loadLocale("en"),
|
|
217
|
+
// Add more as files arrive. Order doesn't matter; keys missing from a
|
|
218
|
+
// non-en map fall back to en automatically.
|
|
219
|
+
});
|
|
220
|
+
} catch {
|
|
221
|
+
// SDK absent — extension still loads with English-only UI.
|
|
222
|
+
}
|
|
197
223
|
|
|
198
224
|
export default function (pi: ExtensionAPI): void {
|
|
199
225
|
// your tool/command/hook registrations here…
|
package/package.json
CHANGED