@juicesharp/rpiv-i18n 1.9.1 → 1.9.2
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/loader.ts +72 -0
- package/package.json +5 -3
package/loader.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Locale-file loader for sibling Pi extensions.
|
|
3
|
+
*
|
|
4
|
+
* Consumers ship a `locales/<code>.json` directory mirroring `SUPPORTED_LOCALES`.
|
|
5
|
+
* Instead of hand-rolling the readFileSync + per-locale try/catch + 8-key
|
|
6
|
+
* `registerStrings` literal in every extension, they import this subpath and
|
|
7
|
+
* call `registerLocalesFromDir(namespace, import.meta.url, { label })`.
|
|
8
|
+
*
|
|
9
|
+
* Lives under the `@juicesharp/rpiv-i18n/loader` subpath (separate from the
|
|
10
|
+
* SDK entry) so a consumer doing `await import("@juicesharp/rpiv-i18n/loader")`
|
|
11
|
+
* does not pull `i18n-ui.ts` and the pi-tui dependency into its load graph
|
|
12
|
+
* just to register strings.
|
|
13
|
+
*
|
|
14
|
+
* Soft-peer contract preserved: when `@juicesharp/rpiv-i18n` is not installed
|
|
15
|
+
* at all, the consumer's outer `try { await import(...) }` rejects before this
|
|
16
|
+
* module is even loaded, so nothing here runs. When it IS installed but at a
|
|
17
|
+
* version that predates this file, the consumer's `sdk.registerLocalesFromDir`
|
|
18
|
+
* resolves to `undefined` and the consumer's outer catch swallows the TypeError —
|
|
19
|
+
* extension stays online, all strings fall back to inline English.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { readFileSync } from "node:fs";
|
|
23
|
+
import { fileURLToPath } from "node:url";
|
|
24
|
+
import { registerStrings, SUPPORTED_LOCALES, type TranslationMap } from "./i18n.js";
|
|
25
|
+
|
|
26
|
+
export interface RegisterLocalesFromDirOptions {
|
|
27
|
+
/**
|
|
28
|
+
* Prefix used in the `console.warn` message when a locale file fails to
|
|
29
|
+
* load. Defaults to the namespace. Set this when the namespace differs
|
|
30
|
+
* from the npm package name (most extensions pass their package name as
|
|
31
|
+
* the namespace, so the default is usually right).
|
|
32
|
+
*/
|
|
33
|
+
label?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Read `locales/<code>.json` for every code in `SUPPORTED_LOCALES` from the
|
|
38
|
+
* caller's package and call `registerStrings(namespace, byLocale)` once.
|
|
39
|
+
*
|
|
40
|
+
* Per-file failures (missing file, malformed JSON, EACCES) emit a `console.warn`
|
|
41
|
+
* and record an empty map for that locale — `tr()` falls back to English. The
|
|
42
|
+
* extension never crashes at module init from a locale-file mistake.
|
|
43
|
+
*
|
|
44
|
+
* @param namespace Translation namespace (typically the npm package name).
|
|
45
|
+
* @param packageUrl Pass `import.meta.url` from the caller — used as the
|
|
46
|
+
* anchor for resolving `./locales/<code>.json`. Files are read from the
|
|
47
|
+
* caller's package, not from rpiv-i18n.
|
|
48
|
+
*/
|
|
49
|
+
export function registerLocalesFromDir(
|
|
50
|
+
namespace: string,
|
|
51
|
+
packageUrl: string,
|
|
52
|
+
options?: RegisterLocalesFromDirOptions,
|
|
53
|
+
): void {
|
|
54
|
+
const label = options?.label ?? namespace;
|
|
55
|
+
const byLocale: Record<string, TranslationMap> = {};
|
|
56
|
+
for (const { code } of SUPPORTED_LOCALES) {
|
|
57
|
+
byLocale[code] = loadOneLocale(packageUrl, code, label);
|
|
58
|
+
}
|
|
59
|
+
registerStrings(namespace, byLocale);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function loadOneLocale(packageUrl: string, code: string, label: string): TranslationMap {
|
|
63
|
+
try {
|
|
64
|
+
const filePath = fileURLToPath(new URL(`./locales/${code}.json`, packageUrl));
|
|
65
|
+
return JSON.parse(readFileSync(filePath, "utf-8")) as TranslationMap;
|
|
66
|
+
} catch (err) {
|
|
67
|
+
console.warn(
|
|
68
|
+
`${label}: failed to load locales/${code}.json — falling back to English (${(err as Error).message})`,
|
|
69
|
+
);
|
|
70
|
+
return {};
|
|
71
|
+
}
|
|
72
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@juicesharp/rpiv-i18n",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.2",
|
|
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",
|
|
@@ -31,12 +31,14 @@
|
|
|
31
31
|
"index.ts",
|
|
32
32
|
"i18n.ts",
|
|
33
33
|
"i18n-ui.ts",
|
|
34
|
+
"loader.ts",
|
|
34
35
|
"docs/",
|
|
35
36
|
"README.md",
|
|
36
37
|
"LICENSE"
|
|
37
38
|
],
|
|
38
39
|
"exports": {
|
|
39
|
-
".": "./i18n.ts"
|
|
40
|
+
".": "./i18n.ts",
|
|
41
|
+
"./loader": "./loader.ts"
|
|
40
42
|
},
|
|
41
43
|
"pi": {
|
|
42
44
|
"extensions": [
|
|
@@ -44,7 +46,7 @@
|
|
|
44
46
|
]
|
|
45
47
|
},
|
|
46
48
|
"dependencies": {
|
|
47
|
-
"@juicesharp/rpiv-config": "^1.9.
|
|
49
|
+
"@juicesharp/rpiv-config": "^1.9.2"
|
|
48
50
|
},
|
|
49
51
|
"peerDependencies": {
|
|
50
52
|
"@earendil-works/pi-coding-agent": "*",
|