pi-todo-ledger 0.5.0 → 0.7.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.
@@ -5,9 +5,10 @@
5
5
  *
6
6
  * - `t(key, fallback)` is `scope("pi-todo-ledger")` if the SDK is
7
7
  * installed (live `/languages` updates propagate). If the SDK is missing
8
- * (standalone install without rpiv-i18n), `t` is an identity passthrough
9
- * that returns the inline English fallback at every call site, so the
10
- * extension stays online with English UI.
8
+ * (standalone install without rpiv-i18n), `t` returns the inline English
9
+ * fallback at every call site, so the extension stays online with English UI.
10
+ * - Traditional Chinese (`locales/zh-TW.json`) is served here, not by the SDK,
11
+ * whose picker lists only `zh`: see `renderLocale()` for when it applies.
11
12
  * - `formatStatusLabel(status)` resolves a TaskStatus to its locale-aware
12
13
  * label via the canonical `status.*` namespace, with the English literal
13
14
  * as fallback so nothing renders blank if the namespace isn't registered.
@@ -19,6 +20,8 @@
19
20
  * `const X = formatStatusLabel(...)`.
20
21
  */
21
22
 
23
+ import { readFileSync } from 'node:fs';
24
+ import { savedLocale } from "../config.ts";
22
25
  import type { TaskStatus } from "../tool/types.ts";
23
26
 
24
27
  export const I18N_NAMESPACE = "pi-todo-ledger";
@@ -38,6 +41,7 @@ type I18nSDK = { scope: (namespace: string) => ScopeFn };
38
41
  // Top-level await is required so a synchronous `t(...)` call from any
39
42
  // downstream module sees the resolved scope; ESM module loading awaits this
40
43
  // before evaluating any importer.
44
+ const traditional: Record<string,string> = JSON.parse(readFileSync(new URL('../locales/zh-TW.json', import.meta.url), 'utf8'));
41
45
  let scopeImpl: ScopeFn;
42
46
  try {
43
47
  const sdk = (await import(I18N_SDK)) as I18nSDK;
@@ -46,7 +50,51 @@ try {
46
50
  scopeImpl = (_key, fallback) => fallback;
47
51
  }
48
52
 
49
- export const t: ScopeFn = scopeImpl;
53
+ const TRADITIONAL = /^zh[-_](?:TW|Hant)(?:[-_.@]|$)/i;
54
+
55
+ // The SDK republishes this plain-data snapshot on every locale change, a /languages pick included.
56
+ const SDK_STATE = Symbol.for("rpiv-i18n");
57
+ type SdkState = { readonly locale?: string } | undefined;
58
+
59
+ /** The `--locale` value Pi was started with (`--locale x` or `--locale=x`). */
60
+ function flagLocale(argv = process.argv): string | undefined {
61
+ for (const [i, arg] of argv.entries()) {
62
+ if (arg.startsWith("--locale=")) return arg.slice(9) || undefined;
63
+ if (arg === "--locale" && argv[i + 1] && !argv[i + 1].startsWith("-")) return argv[i + 1];
64
+ }
65
+ return undefined;
66
+ }
67
+
68
+ let seenState: SdkState | null = null;
69
+ let chosen: string | undefined;
70
+ /** The user's explicit choice (`--locale`, then the saved /languages pick), re-read when the SDK applies a locale. */
71
+ function explicitLocale(state: SdkState): string | undefined {
72
+ if (state !== seenState) {
73
+ seenState = state;
74
+ chosen = flagLocale() ?? savedLocale();
75
+ }
76
+ return chosen;
77
+ }
78
+
79
+ /**
80
+ * The locale to render in. An explicit choice wins as given: `zh` stays Simplified and `zh-TW`
81
+ * is Traditional. Without one, the system locale is the default, read in POSIX order
82
+ * (`LC_ALL`, then `LC_MESSAGES`, then `LANG`). The SDK reads `LANG` first and drops the
83
+ * region, so a Traditional system locale decides here; any other keeps the SDK's code.
84
+ */
85
+ export function renderLocale(): string {
86
+ const sys = process.env.LC_ALL || process.env.LC_MESSAGES || process.env.LANG || '';
87
+ const state = (globalThis as { [k: symbol]: SdkState })[SDK_STATE];
88
+ const locale = state?.locale;
89
+ if (!locale) return sys;
90
+ if (explicitLocale(state) !== undefined) return locale;
91
+ return TRADITIONAL.test(sys) ? sys : locale;
92
+ }
93
+
94
+ export const t: ScopeFn = (key, fallback) => {
95
+ if (TRADITIONAL.test(renderLocale())) return traditional[key] ?? fallback;
96
+ return scopeImpl(key, fallback);
97
+ };
50
98
 
51
99
  const STATUS_LABEL_PENDING = "pending";
52
100
  const STATUS_LABEL_IN_PROGRESS = "in progress";
@@ -8,7 +8,12 @@ import { EMPTY_STATE, type TaskState } from "./state.ts";
8
8
  * The Map is the single mutation seam — only `commitState` / `replaceState` /
9
9
  * `evictSession` write it; the reducer (`state/state-reducer.ts`) stays pure.
10
10
  */
11
- const sessions = new Map<string, TaskState>();
11
+ interface SessionSlot {
12
+ state: TaskState;
13
+ /** In-process identity of the list, not the session. Never serialized. */
14
+ generation: symbol;
15
+ }
16
+ const sessions = new Map<string, SessionSlot>();
12
17
 
13
18
  /**
14
19
  * Ctx-less render pointer: which slot do the ctx-free readers (the overlay's
@@ -37,7 +42,7 @@ function freshState(): TaskState {
37
42
  /** Get-or-read a session's slot: the committed slot by identity, or a fresh
38
43
  * EMPTY_STATE copy (not stored) when the slot is absent. */
39
44
  function slotFor(sessionId: string): TaskState {
40
- return sessions.get(sessionId) ?? freshState();
45
+ return sessions.get(sessionId)?.state ?? freshState();
41
46
  }
42
47
 
43
48
  /**
@@ -64,7 +69,12 @@ export function getState(sessionId: string): TaskState {
64
69
  * `replayFromBranch` decodes the latest snapshot, keyed to the session.
65
70
  */
66
71
  export function replaceState(sessionId: string, next: TaskState): void {
67
- sessions.set(sessionId, next);
72
+ sessions.set(sessionId, { state: next, generation: Symbol() });
73
+ }
74
+
75
+ /** Together with the local id, identifies a legacy task until clear, replay, or eviction. */
76
+ export function listGeneration(sessionId: string): symbol | undefined {
77
+ return sessions.get(sessionId)?.generation;
68
78
  }
69
79
 
70
80
  /**
@@ -73,7 +83,9 @@ export function replaceState(sessionId: string, next: TaskState): void {
73
83
  * `/todos`, `renderCall`), keyed to the calling session.
74
84
  */
75
85
  export function commitState(sessionId: string, next: TaskState): void {
76
- sessions.set(sessionId, next);
86
+ const slot = sessions.get(sessionId);
87
+ if (slot) slot.state = next;
88
+ else replaceState(sessionId, next);
77
89
  }
78
90
 
79
91
  /** Drop a session's slot on `session_shutdown`. No-op if the slot is absent. */