Skip to content

Commit ccad5cf

Browse files
committed
Move UI strings into copydecks, for localisation
1 parent 11297a0 commit ccad5cf

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

copydecks/en/copydeck.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
"language": "en",
44
"version": 1
55
},
6+
"ui": {
7+
"line": "line",
8+
"in": "in",
9+
"thisFile": "this file",
10+
"errorDetails": "Error details",
11+
"error": "Error",
12+
"copydeckNotLoaded": "Copydeck not loaded",
13+
"pythonError": "Python error",
14+
"fallbackSummary": "Start with the last line of the message and the highlighted code line.",
15+
"fallbackWhy": "The last line of the traceback tells you the error type and main cause.",
16+
"fallbackStep": "Fix one small thing and run again."
17+
},
618
"glossary": {
719
"kid": {
820
"variable": "name",

src/engine.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const S: InternalState = { adapters: {} };
1010

1111
export const loadCopydeck = (deck: CopyDeck) => (S.copy = deck);
1212

13+
const getUiString = (key: keyof NonNullable<CopyDeck["ui"]>, fallback: string): string => {
14+
return S.copy?.ui?.[key] || fallback;
15+
};
16+
1317
export const registerAdapter = (name: string, fn: (raw: string, code?: string) => Trace | null) =>
1418
(S.adapters[name] = fn);
1519

@@ -56,10 +60,13 @@ const pickVariant = (trace: Trace, code: string | undefined, audience: string) =
5660
return true;
5761
};
5862

63+
const lineStr = getUiString("line", "line");
64+
const inStr = getUiString("in", "in");
65+
const thisFileStr = getUiString("thisFile", "this file");
5966
const loc =
60-
trace.line && trace.file ? `line ${trace.line} in ${trace.file}` :
61-
trace.line ? `line ${trace.line}` :
62-
trace.file || "this file";
67+
trace.line && trace.file ? `${lineStr} ${trace.line} ${inStr} ${trace.file}` :
68+
trace.line ? `${lineStr} ${trace.line}` :
69+
trace.file || thisFileStr;
6370

6471
const vars = {
6572
loc,
@@ -95,8 +102,8 @@ const pickVariant = (trace: Trace, code: string | undefined, audience: string) =
95102
why ? `<div class="pfem__why">${why}</div>` : "",
96103
steps?.length ? `<ul class="pfem__steps">${steps.map((s) => `<li>${s}</li>`).join("")}</ul>` : "",
97104
patch ? `<pre class="pfem__patch">${escapeHtml(patch)}</pre>` : "",
98-
`<details class="pfem__details"><summary>Error details</summary><pre>${escapeHtml(
99-
(trace.type || "Error") + ": " + trace.message
105+
`<details class="pfem__details"><summary>${escapeHtml(getUiString("errorDetails", "Error details"))}</summary><pre>${escapeHtml(
106+
(trace.type || getUiString("error", "Error")) + ": " + trace.message
100107
)}</pre></details>`
101108
].filter(Boolean).join("\n");
102109

@@ -131,15 +138,15 @@ export const friendlyExplain = (opts: ExplainOptions): ExplainResult => {
131138
return {
132139
trace,
133140
variantId: "Other/variants/0",
134-
title: "Python error",
135-
summary: "Start with the last line of the message and the highlighted code line.",
136-
why: "The last line of the traceback tells you the error type and main cause.",
137-
steps: ["Fix one small thing and run again."],
141+
title: getUiString("pythonError", "Python error"),
142+
summary: getUiString("fallbackSummary", "Start with the last line of the message and the highlighted code line."),
143+
why: getUiString("fallbackWhy", "The last line of the traceback tells you the error type and main cause."),
144+
steps: [getUiString("fallbackStep", "Fix one small thing and run again.")],
138145
html: undefined
139146
};
140147
}
141148

142-
// not convinced by this implementation, and may decide to drop the verbosity option...
149+
// not convinced by this implementation, and may decide to drop the verbosity option entirely...
143150
if (verbosity === "brief") {
144151
chosen.why = undefined;
145152
chosen.steps = chosen.steps?.slice(0, 1);

src/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ export type CopyDeck = {
5555
meta: { language: string; version: number };
5656
glossary?: Record<string, Record<string, string>>;
5757
errors: Record<string, { variants: CopyVariant[] }>;
58+
ui?: {
59+
line?: string;
60+
in?: string;
61+
thisFile?: string;
62+
errorDetails?: string;
63+
error?: string;
64+
copydeckNotLoaded?: string;
65+
pythonError?: string;
66+
fallbackSummary?: string;
67+
fallbackWhy?: string;
68+
fallbackStep?: string;
69+
};
5870
};
5971

6072
export type AdapterFn = (raw: string, code?: string) => Trace | null;

0 commit comments

Comments
 (0)