@bison-lab/payload-core 4.36.1 → 4.39.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.
- package/README.md +40 -3
- package/dist/admin.d.mts +19 -1
- package/dist/admin.d.mts.map +1 -1
- package/dist/admin.mjs +92 -17
- package/dist/admin.mjs.map +1 -1
- package/dist/block-library.d.mts +1 -1
- package/dist/{catalogue-PvKGCih7.d.mts → catalogue-FxH10X3v.d.mts} +1 -1
- package/dist/{catalogue-PvKGCih7.d.mts.map → catalogue-FxH10X3v.d.mts.map} +1 -1
- package/dist/{facet-field-CZbLDAQz.d.mts → facet-field-DjaZ0C7D.d.mts} +1 -1
- package/dist/{facet-field-CZbLDAQz.d.mts.map → facet-field-DjaZ0C7D.d.mts.map} +1 -1
- package/dist/index.d.mts +6 -6
- package/dist/index.mjs +4 -4
- package/dist/index.mjs.map +1 -1
- package/dist/{locale-CxFNy8ex.d.mts → locale-CWiIcGuU.d.mts} +1 -1
- package/dist/locale-CWiIcGuU.d.mts.map +1 -0
- package/dist/messages-Fl1spIwN.mjs +229 -0
- package/dist/messages-Fl1spIwN.mjs.map +1 -0
- package/dist/metadata.d.mts +1 -1
- package/dist/navigation.d.mts +2 -2
- package/dist/navigation.mjs +1 -1
- package/dist/og.d.mts +2 -2
- package/dist/{preview-v-GlYuqa.d.mts → preview-CNBVfmM7.d.mts} +2 -2
- package/dist/{preview-v-GlYuqa.d.mts.map → preview-CNBVfmM7.d.mts.map} +1 -1
- package/dist/{published-DACERo16.mjs → published-D3yVnbxs.mjs} +6 -4
- package/dist/published-D3yVnbxs.mjs.map +1 -0
- package/dist/{published-B3IYStMQ.d.mts → published-DXjnElZn.d.mts} +4 -4
- package/dist/published-DXjnElZn.d.mts.map +1 -0
- package/dist/{published-Cow49CSZ.d.mts → published-_1rrTj_B.d.mts} +3 -3
- package/dist/{published-Cow49CSZ.d.mts.map → published-_1rrTj_B.d.mts.map} +1 -1
- package/dist/taxonomy.d.mts +1 -1
- package/dist/theme.d.mts +4 -4
- package/dist/{title-DFKu53nG.d.mts → title-D_jixG36.d.mts} +1 -1
- package/dist/{title-DFKu53nG.d.mts.map → title-D_jixG36.d.mts.map} +1 -1
- package/dist/translate-messages-cli.d.mts +1 -0
- package/dist/translate-messages-cli.mjs +151 -0
- package/dist/translate-messages-cli.mjs.map +1 -0
- package/dist/translate.d.mts +463 -0
- package/dist/translate.d.mts.map +1 -0
- package/dist/translate.mjs +1071 -0
- package/dist/translate.mjs.map +1 -0
- package/dist/{types-B7Hy1Ho-.d.mts → types-Db6Iy3hJ.d.mts} +1 -1
- package/dist/{types-B7Hy1Ho-.d.mts.map → types-Db6Iy3hJ.d.mts.map} +1 -1
- package/package.json +15 -5
- package/dist/locale-CxFNy8ex.d.mts.map +0 -1
- package/dist/published-B3IYStMQ.d.mts.map +0 -1
- package/dist/published-DACERo16.mjs.map +0 -1
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { r as anthropicTranslator, t as fillMessages } from "./messages-Fl1spIwN.mjs";
|
|
3
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { basename, resolve } from "node:path";
|
|
5
|
+
import { parseArgs } from "node:util";
|
|
6
|
+
//#region src/translate/messages-cli.ts
|
|
7
|
+
const TRANSLATE_MESSAGES_USAGE = `Usage: bison-translate-messages --source <en.json> --out <es.json> [--force]
|
|
8
|
+
[--from <code>] [--to <code>] [--language <name>] [--site-name <name>] [--about <line>]
|
|
9
|
+
[--voice <words>] [--keep <a,b,c>] [--context <hint>]
|
|
10
|
+
|
|
11
|
+
--language names the --to locale in the prompt, as the plugin's
|
|
12
|
+
site.languages does ("Latin American Spanish"), so message files and CMS
|
|
13
|
+
copy get the same dialect.
|
|
14
|
+
|
|
15
|
+
Fills keys that are missing or empty in --out from --source. --force
|
|
16
|
+
re-translates every key. Reads ANTHROPIC_API_KEY (and the optional
|
|
17
|
+
ANTHROPIC_TRANSLATE_MODEL) from the environment, .env.local or .env.`;
|
|
18
|
+
/** Locale code from a message file name: `src/messages/es.json` is `es`. */
|
|
19
|
+
const codeOf = (path) => basename(path).replace(/\.json$/i, "");
|
|
20
|
+
/**
|
|
21
|
+
* The `bison-translate-messages` run, as a function: returns the exit code.
|
|
22
|
+
* Writes the target only after every batch has run. A batch that fails (or a
|
|
23
|
+
* translation that loses a `{placeholder}`) keeps the source text, so the
|
|
24
|
+
* file stays complete, and the run exits 1 and says how to redo those keys.
|
|
25
|
+
*/
|
|
26
|
+
async function runTranslateMessagesCli(argv, deps) {
|
|
27
|
+
let values;
|
|
28
|
+
try {
|
|
29
|
+
({values} = parseArgs({
|
|
30
|
+
args: argv,
|
|
31
|
+
options: {
|
|
32
|
+
source: { type: "string" },
|
|
33
|
+
out: { type: "string" },
|
|
34
|
+
force: {
|
|
35
|
+
type: "boolean",
|
|
36
|
+
default: false
|
|
37
|
+
},
|
|
38
|
+
from: { type: "string" },
|
|
39
|
+
to: { type: "string" },
|
|
40
|
+
language: { type: "string" },
|
|
41
|
+
"site-name": { type: "string" },
|
|
42
|
+
about: { type: "string" },
|
|
43
|
+
voice: { type: "string" },
|
|
44
|
+
keep: { type: "string" },
|
|
45
|
+
context: { type: "string" },
|
|
46
|
+
help: {
|
|
47
|
+
type: "boolean",
|
|
48
|
+
default: false
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}));
|
|
52
|
+
} catch (error) {
|
|
53
|
+
deps.error(`${error.message}\n\n${TRANSLATE_MESSAGES_USAGE}`);
|
|
54
|
+
return 2;
|
|
55
|
+
}
|
|
56
|
+
if (values.help) {
|
|
57
|
+
deps.log(TRANSLATE_MESSAGES_USAGE);
|
|
58
|
+
return 0;
|
|
59
|
+
}
|
|
60
|
+
const sourcePath = typeof values.source === "string" ? resolve(values.source) : void 0;
|
|
61
|
+
const outPath = typeof values.out === "string" ? resolve(values.out) : void 0;
|
|
62
|
+
if (!sourcePath || !outPath) {
|
|
63
|
+
deps.error(TRANSLATE_MESSAGES_USAGE);
|
|
64
|
+
return 2;
|
|
65
|
+
}
|
|
66
|
+
const siteName = typeof values["site-name"] === "string" ? values["site-name"] : void 0;
|
|
67
|
+
const to = typeof values.to === "string" ? values.to : codeOf(outPath);
|
|
68
|
+
const language = typeof values.language === "string" && values.language ? values.language : void 0;
|
|
69
|
+
const site = siteName || language ? {
|
|
70
|
+
name: siteName ?? "",
|
|
71
|
+
...language ? { languages: { [to]: language } } : {},
|
|
72
|
+
...typeof values.about === "string" ? { about: values.about } : {},
|
|
73
|
+
...typeof values.voice === "string" ? { voice: values.voice } : {},
|
|
74
|
+
...typeof values.keep === "string" ? { keepVerbatim: values.keep.split(",").map((word) => word.trim()).filter(Boolean) } : {}
|
|
75
|
+
} : void 0;
|
|
76
|
+
const translator = deps.translator ?? anthropicTranslator({
|
|
77
|
+
apiKey: deps.env.ANTHROPIC_API_KEY,
|
|
78
|
+
model: deps.env.ANTHROPIC_TRANSLATE_MODEL
|
|
79
|
+
});
|
|
80
|
+
if (translator.configured === false) {
|
|
81
|
+
deps.error("ANTHROPIC_API_KEY is not set. Add it to .env.local and retry.");
|
|
82
|
+
return 1;
|
|
83
|
+
}
|
|
84
|
+
const source = JSON.parse(readFileSync(sourcePath, "utf8"));
|
|
85
|
+
const target = !values.force && existsSync(outPath) ? JSON.parse(readFileSync(outPath, "utf8")) : void 0;
|
|
86
|
+
const from = typeof values.from === "string" ? values.from : codeOf(sourcePath);
|
|
87
|
+
const result = await fillMessages({
|
|
88
|
+
source,
|
|
89
|
+
...target !== void 0 ? { target } : {},
|
|
90
|
+
translator,
|
|
91
|
+
from,
|
|
92
|
+
to,
|
|
93
|
+
force: values.force === true,
|
|
94
|
+
...typeof values.context === "string" ? { context: values.context } : {},
|
|
95
|
+
...site ? { site } : {},
|
|
96
|
+
onProgress: (done, total) => deps.log(` ${done}/${total}`)
|
|
97
|
+
});
|
|
98
|
+
if (result.translated === 0 && result.fellBack === 0) {
|
|
99
|
+
deps.log(`${basename(outPath)} is already complete. Nothing to translate.`);
|
|
100
|
+
return 0;
|
|
101
|
+
}
|
|
102
|
+
writeFileSync(outPath, `${JSON.stringify(result.messages, null, 2)}\n`, "utf8");
|
|
103
|
+
deps.log(`Wrote ${outPath} (${result.translated} key(s) translated). Review the diff before committing.`);
|
|
104
|
+
if (result.fellBack > 0) {
|
|
105
|
+
deps.error(`${result.fellBack} key(s) kept the ${from} text because a batch failed or a {placeholder} was lost. They are not missing anymore, so a plain re-run skips them: re-run with --force, or edit them by hand.`);
|
|
106
|
+
return 1;
|
|
107
|
+
}
|
|
108
|
+
return 0;
|
|
109
|
+
}
|
|
110
|
+
/** Loads `.env.local` then `.env` from the working directory, never overriding a set variable. */
|
|
111
|
+
function loadEnvFiles(cwd) {
|
|
112
|
+
const load = process.loadEnvFile;
|
|
113
|
+
if (!load) return;
|
|
114
|
+
for (const name of [".env.local", ".env"]) {
|
|
115
|
+
const path = resolve(cwd, name);
|
|
116
|
+
if (!existsSync(path)) continue;
|
|
117
|
+
const before = { ...process.env };
|
|
118
|
+
try {
|
|
119
|
+
load(path);
|
|
120
|
+
} catch {
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
for (const [key, value] of Object.entries(before)) process.env[key] = value;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
//#endregion
|
|
127
|
+
//#region src/translate-messages-cli.ts
|
|
128
|
+
/**
|
|
129
|
+
* `bison-translate-messages`: fills a next-intl message file from the source
|
|
130
|
+
* locale's file with the Anthropic translator. Run it from the site, which
|
|
131
|
+
* installs `@anthropic-ai/sdk` (an optional peer of this package).
|
|
132
|
+
*
|
|
133
|
+
* bison-translate-messages --source src/messages/en.json --out src/messages/es.json
|
|
134
|
+
* bison-translate-messages --source src/messages/en.json --out src/messages/es.json --force
|
|
135
|
+
*
|
|
136
|
+
* Only keys missing or empty in --out are translated unless --force is
|
|
137
|
+
* passed. --site-name, --about, --voice and --keep (comma list) carry the
|
|
138
|
+
* site's prompt facts. The key is read from ANTHROPIC_API_KEY (the
|
|
139
|
+
* environment, `.env.local` or `.env`), never from arguments.
|
|
140
|
+
*/
|
|
141
|
+
loadEnvFiles(process.cwd());
|
|
142
|
+
const code = await runTranslateMessagesCli(process.argv.slice(2), {
|
|
143
|
+
env: process.env,
|
|
144
|
+
log: (line) => console.log(line),
|
|
145
|
+
error: (line) => console.error(line)
|
|
146
|
+
});
|
|
147
|
+
process.exit(code);
|
|
148
|
+
//#endregion
|
|
149
|
+
export {};
|
|
150
|
+
|
|
151
|
+
//# sourceMappingURL=translate-messages-cli.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"translate-messages-cli.mjs","names":[],"sources":["../src/translate/messages-cli.ts","../src/translate-messages-cli.ts"],"sourcesContent":["import { existsSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { basename, resolve } from \"node:path\";\nimport { parseArgs } from \"node:util\";\n\nimport { anthropicTranslator } from \"./anthropic\";\nimport { fillMessages, type MessagesJson } from \"./messages\";\nimport type { TranslateSite, Translator } from \"./types\";\n\nexport const TRANSLATE_MESSAGES_USAGE = `Usage: bison-translate-messages --source <en.json> --out <es.json> [--force]\n [--from <code>] [--to <code>] [--language <name>] [--site-name <name>] [--about <line>]\n [--voice <words>] [--keep <a,b,c>] [--context <hint>]\n\n--language names the --to locale in the prompt, as the plugin's\nsite.languages does (\"Latin American Spanish\"), so message files and CMS\ncopy get the same dialect.\n\nFills keys that are missing or empty in --out from --source. --force\nre-translates every key. Reads ANTHROPIC_API_KEY (and the optional\nANTHROPIC_TRANSLATE_MODEL) from the environment, .env.local or .env.`;\n\ninterface CliDeps {\n env: Record<string, string | undefined>;\n log: (line: string) => void;\n error: (line: string) => void;\n /** Overrides the Anthropic translator (tests). */\n translator?: Translator;\n}\n\n/** Locale code from a message file name: `src/messages/es.json` is `es`. */\nconst codeOf = (path: string) => basename(path).replace(/\\.json$/i, \"\");\n\n/**\n * The `bison-translate-messages` run, as a function: returns the exit code.\n * Writes the target only after every batch has run. A batch that fails (or a\n * translation that loses a `{placeholder}`) keeps the source text, so the\n * file stays complete, and the run exits 1 and says how to redo those keys.\n */\nexport async function runTranslateMessagesCli(argv: string[], deps: CliDeps): Promise<number> {\n let values: Record<string, string | boolean | undefined>;\n try {\n ({ values } = parseArgs({\n args: argv,\n options: {\n source: { type: \"string\" },\n out: { type: \"string\" },\n force: { type: \"boolean\", default: false },\n from: { type: \"string\" },\n to: { type: \"string\" },\n language: { type: \"string\" },\n \"site-name\": { type: \"string\" },\n about: { type: \"string\" },\n voice: { type: \"string\" },\n keep: { type: \"string\" },\n context: { type: \"string\" },\n help: { type: \"boolean\", default: false },\n },\n }));\n } catch (error) {\n deps.error(`${(error as Error).message}\\n\\n${TRANSLATE_MESSAGES_USAGE}`);\n return 2;\n }\n if (values.help) {\n deps.log(TRANSLATE_MESSAGES_USAGE);\n return 0;\n }\n const sourcePath = typeof values.source === \"string\" ? resolve(values.source) : undefined;\n const outPath = typeof values.out === \"string\" ? resolve(values.out) : undefined;\n if (!sourcePath || !outPath) {\n deps.error(TRANSLATE_MESSAGES_USAGE);\n return 2;\n }\n\n const siteName = typeof values[\"site-name\"] === \"string\" ? values[\"site-name\"] : undefined;\n const to = typeof values.to === \"string\" ? values.to : codeOf(outPath);\n const language = typeof values.language === \"string\" && values.language ? values.language : undefined;\n const site: TranslateSite | undefined =\n siteName || language\n ? {\n name: siteName ?? \"\",\n ...(language ? { languages: { [to]: language } } : {}),\n ...(typeof values.about === \"string\" ? { about: values.about } : {}),\n ...(typeof values.voice === \"string\" ? { voice: values.voice } : {}),\n ...(typeof values.keep === \"string\"\n ? {\n keepVerbatim: values.keep\n .split(\",\")\n .map((word) => word.trim())\n .filter(Boolean),\n }\n : {}),\n }\n : undefined;\n const translator =\n deps.translator ??\n anthropicTranslator({ apiKey: deps.env.ANTHROPIC_API_KEY, model: deps.env.ANTHROPIC_TRANSLATE_MODEL });\n if (translator.configured === false) {\n deps.error(\"ANTHROPIC_API_KEY is not set. Add it to .env.local and retry.\");\n return 1;\n }\n\n const source = JSON.parse(readFileSync(sourcePath, \"utf8\")) as MessagesJson;\n const target =\n !values.force && existsSync(outPath) ? (JSON.parse(readFileSync(outPath, \"utf8\")) as MessagesJson) : undefined;\n const from = typeof values.from === \"string\" ? values.from : codeOf(sourcePath);\n\n const result = await fillMessages({\n source,\n ...(target !== undefined ? { target } : {}),\n translator,\n from,\n to,\n force: values.force === true,\n ...(typeof values.context === \"string\" ? { context: values.context } : {}),\n ...(site ? { site } : {}),\n onProgress: (done, total) => deps.log(` ${done}/${total}`),\n });\n if (result.translated === 0 && result.fellBack === 0) {\n deps.log(`${basename(outPath)} is already complete. Nothing to translate.`);\n return 0;\n }\n writeFileSync(outPath, `${JSON.stringify(result.messages, null, 2)}\\n`, \"utf8\");\n deps.log(`Wrote ${outPath} (${result.translated} key(s) translated). Review the diff before committing.`);\n if (result.fellBack > 0) {\n // The keys are now filled (so a plain re-run will skip them); say exactly\n // how to redo them once the transient issue is resolved.\n deps.error(\n `${result.fellBack} key(s) kept the ${from} text because a batch failed or a {placeholder} was lost. ` +\n \"They are not missing anymore, so a plain re-run skips them: re-run with --force, or edit them by hand.\",\n );\n return 1;\n }\n return 0;\n}\n\n/** Loads `.env.local` then `.env` from the working directory, never overriding a set variable. */\nexport function loadEnvFiles(cwd: string): void {\n const load = (process as unknown as { loadEnvFile?: (path: string) => void }).loadEnvFile;\n if (!load) return;\n for (const name of [\".env.local\", \".env\"]) {\n const path = resolve(cwd, name);\n if (!existsSync(path)) continue;\n const before = { ...process.env };\n try {\n load(path);\n } catch {\n continue;\n }\n // loadEnvFile overrides; restore anything that was already set.\n for (const [key, value] of Object.entries(before)) process.env[key] = value;\n }\n}\n","#!/usr/bin/env node\n/**\n * `bison-translate-messages`: fills a next-intl message file from the source\n * locale's file with the Anthropic translator. Run it from the site, which\n * installs `@anthropic-ai/sdk` (an optional peer of this package).\n *\n * bison-translate-messages --source src/messages/en.json --out src/messages/es.json\n * bison-translate-messages --source src/messages/en.json --out src/messages/es.json --force\n *\n * Only keys missing or empty in --out are translated unless --force is\n * passed. --site-name, --about, --voice and --keep (comma list) carry the\n * site's prompt facts. The key is read from ANTHROPIC_API_KEY (the\n * environment, `.env.local` or `.env`), never from arguments.\n */\nimport { loadEnvFiles, runTranslateMessagesCli } from \"./translate/messages-cli\";\n\nloadEnvFiles(process.cwd());\nconst code = await runTranslateMessagesCli(process.argv.slice(2), {\n env: process.env,\n log: (line) => console.log(line),\n error: (line) => console.error(line),\n});\nprocess.exit(code);\n"],"mappings":";;;;;;AAQA,MAAa,2BAA2B;;;;;;;;;;;;AAqBxC,MAAM,UAAU,SAAiB,SAAS,KAAK,CAAC,QAAQ,YAAY,GAAG;;;;;;;AAQvE,eAAsB,wBAAwB,MAAgB,MAAgC;CAC5F,IAAI;AACJ,KAAI;AACF,GAAC,CAAE,UAAW,UAAU;GACtB,MAAM;GACN,SAAS;IACP,QAAQ,EAAE,MAAM,UAAU;IAC1B,KAAK,EAAE,MAAM,UAAU;IACvB,OAAO;KAAE,MAAM;KAAW,SAAS;KAAO;IAC1C,MAAM,EAAE,MAAM,UAAU;IACxB,IAAI,EAAE,MAAM,UAAU;IACtB,UAAU,EAAE,MAAM,UAAU;IAC5B,aAAa,EAAE,MAAM,UAAU;IAC/B,OAAO,EAAE,MAAM,UAAU;IACzB,OAAO,EAAE,MAAM,UAAU;IACzB,MAAM,EAAE,MAAM,UAAU;IACxB,SAAS,EAAE,MAAM,UAAU;IAC3B,MAAM;KAAE,MAAM;KAAW,SAAS;KAAO;IAC1C;GACF,CAAC;UACK,OAAO;AACd,OAAK,MAAM,GAAI,MAAgB,QAAQ,MAAM,2BAA2B;AACxE,SAAO;;AAET,KAAI,OAAO,MAAM;AACf,OAAK,IAAI,yBAAyB;AAClC,SAAO;;CAET,MAAM,aAAa,OAAO,OAAO,WAAW,WAAW,QAAQ,OAAO,OAAO,GAAG,KAAA;CAChF,MAAM,UAAU,OAAO,OAAO,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
|
|
1
|
+
import { Block, Config, Endpoint, Field, Payload, PayloadRequest, Plugin, SanitizedConfig } from "payload";
|
|
2
|
+
|
|
3
|
+
//#region src/translate/types.d.ts
|
|
4
|
+
/** A document as one locale's depth-0 read returns it. */
|
|
5
|
+
type Doc = Record<string, unknown>;
|
|
6
|
+
/**
|
|
7
|
+
* What a site tells the model about itself. The library owns the generic
|
|
8
|
+
* rules (JSON in and out, placeholders, tags, URLs, literal legal copy);
|
|
9
|
+
* these are the facts only a site has.
|
|
10
|
+
*/
|
|
11
|
+
interface TranslateSite {
|
|
12
|
+
/** The brand name, kept verbatim. */
|
|
13
|
+
name: string;
|
|
14
|
+
/** One line on what the site is, for word choice ("a gigabit fiber provider in Kansas"). */
|
|
15
|
+
about?: string;
|
|
16
|
+
/** The voice to keep ("confident, friendly, speed-first"). */
|
|
17
|
+
voice?: string;
|
|
18
|
+
/** Brand, product and place names the model must never translate. */
|
|
19
|
+
keepVerbatim?: string[];
|
|
20
|
+
/**
|
|
21
|
+
* How to name a locale's language in the prompt, per locale code
|
|
22
|
+
* (`{ es: "Latin American Spanish" }`). A code left out is named by
|
|
23
|
+
* `Intl.DisplayNames`.
|
|
24
|
+
*/
|
|
25
|
+
languages?: Record<string, string>;
|
|
26
|
+
}
|
|
27
|
+
/** One translation call: from which locale, into which, and where the strings appear. */
|
|
28
|
+
interface TranslateRequest {
|
|
29
|
+
from: string;
|
|
30
|
+
to: string;
|
|
31
|
+
/** A short hint about where the strings appear, to improve word choice. */
|
|
32
|
+
context?: string;
|
|
33
|
+
/** The plugin's `site`, when it was given one. */
|
|
34
|
+
site?: TranslateSite;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Translates a batch of strings, returning them in the same order and the
|
|
38
|
+
* same length, or `null` on any failure. It never throws on purpose; a
|
|
39
|
+
* throw is caught and read as `null`.
|
|
40
|
+
*
|
|
41
|
+
* `configured: false` says the translator cannot run (no API key): the
|
|
42
|
+
* plugin then logs a warning on publish instead of deferring work, and
|
|
43
|
+
* hides "Translate now". Leave it unset on a translator that always runs.
|
|
44
|
+
*/
|
|
45
|
+
type Translator = ((strings: string[], request: TranslateRequest) => Promise<string[] | null>) & {
|
|
46
|
+
configured?: boolean;
|
|
47
|
+
};
|
|
48
|
+
/** A collection or global the plugin translates, by slug or with a prompt hint. */
|
|
49
|
+
type TranslatedEntity = string | {
|
|
50
|
+
slug: string; /** The prompt hint for one document, from its source-locale read (`Internet plan: Fiber 1000`). */
|
|
51
|
+
context?: (doc: Doc) => string;
|
|
52
|
+
};
|
|
53
|
+
interface AutoTranslateOptions {
|
|
54
|
+
/**
|
|
55
|
+
* The per-site switch. `false` registers nothing at all: no hooks,
|
|
56
|
+
* endpoints, admin components or config, the same config as leaving the
|
|
57
|
+
* plugin out.
|
|
58
|
+
*/
|
|
59
|
+
enabled: boolean;
|
|
60
|
+
/** Collections whose source-locale publishes fill every other locale. */
|
|
61
|
+
collections?: TranslatedEntity[];
|
|
62
|
+
/** Globals whose source-locale publishes fill every other locale. */
|
|
63
|
+
globals?: TranslatedEntity[];
|
|
64
|
+
/** What turns strings into another language: `anthropicTranslator(...)` or your own. */
|
|
65
|
+
translator: Translator;
|
|
66
|
+
/**
|
|
67
|
+
* Keeps the deferred translation alive after the response is sent, e.g.
|
|
68
|
+
* `waitUntil` from `@vercel/functions`. Off a serverless host leave it
|
|
69
|
+
* out; the work runs in the same process either way.
|
|
70
|
+
*/
|
|
71
|
+
waitUntil?: (promise: Promise<unknown>) => void;
|
|
72
|
+
/** Site facts passed to the translator on every call (see `TranslateSite`). */
|
|
73
|
+
site?: TranslateSite;
|
|
74
|
+
}
|
|
75
|
+
/** What happened in one target locale. */
|
|
76
|
+
interface TranslateLocaleResult {
|
|
77
|
+
locale: string;
|
|
78
|
+
/** `nothing`: every string already in step; `failed`: the translator gave no usable result. */
|
|
79
|
+
status: "translated" | "nothing" | "failed";
|
|
80
|
+
/** Strings written. */
|
|
81
|
+
count: number;
|
|
82
|
+
}
|
|
83
|
+
//#endregion
|
|
84
|
+
//#region src/translate/plugin.d.ts
|
|
85
|
+
/**
|
|
86
|
+
* Fills every locale but the default from the default whenever copy is
|
|
87
|
+
* published, without overwriting a translation a person corrected. Each site
|
|
88
|
+
* switches it on or off in its own `payload.config.ts`:
|
|
89
|
+
*
|
|
90
|
+
* ```ts
|
|
91
|
+
* autoTranslate({
|
|
92
|
+
* enabled: true,
|
|
93
|
+
* collections: ["pages", { slug: "plans", context: (doc) => `Internet plan: ${doc.name}` }],
|
|
94
|
+
* translator: anthropicTranslator({ apiKey: process.env.ANTHROPIC_API_KEY, site: { name: "ZoomFiber" } }),
|
|
95
|
+
* waitUntil,
|
|
96
|
+
* })
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* `enabled: false` returns the config untouched. Without `localization` (or
|
|
100
|
+
* with one locale) it logs one warning and registers nothing. Otherwise each
|
|
101
|
+
* listed collection gets an `afterChange` hook, `POST /api/<slug>/:id/translate`
|
|
102
|
+
* and a "Translate now" item in the document menu (`TRANSLATE_NOW`, run
|
|
103
|
+
* `payload generate:importmap`). The item is registered whether or not the
|
|
104
|
+
* translator has a key, so the import map never depends on the environment;
|
|
105
|
+
* it renders nothing when there is no key. Each listed
|
|
106
|
+
* global gets the hook.
|
|
107
|
+
*/
|
|
108
|
+
declare function autoTranslate(options: AutoTranslateOptions): Plugin;
|
|
109
|
+
//#endregion
|
|
110
|
+
//#region src/translate/anthropic.d.ts
|
|
111
|
+
/**
|
|
112
|
+
* The library's default model. Translating marketing copy does not need the
|
|
113
|
+
* most capable model; a current Sonnet handles it well at a fraction of the
|
|
114
|
+
* price. A site overrides it per environment (`ANTHROPIC_TRANSLATE_MODEL`).
|
|
115
|
+
*/
|
|
116
|
+
declare const DEFAULT_TRANSLATE_MODEL = "claude-sonnet-5-5";
|
|
117
|
+
/** The slice of the Anthropic client this translator calls. */
|
|
118
|
+
interface AnthropicMessagesClient {
|
|
119
|
+
messages: {
|
|
120
|
+
create(body: Record<string, unknown>, options?: {
|
|
121
|
+
timeout?: number;
|
|
122
|
+
maxRetries?: number;
|
|
123
|
+
}): Promise<{
|
|
124
|
+
content: Array<{
|
|
125
|
+
type: string;
|
|
126
|
+
text?: string;
|
|
127
|
+
}>;
|
|
128
|
+
}>;
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
interface AnthropicTranslatorOptions {
|
|
132
|
+
/**
|
|
133
|
+
* The API key, usually `process.env.ANTHROPIC_API_KEY`. Without one the
|
|
134
|
+
* translator reports `configured: false`: publishes still work, a
|
|
135
|
+
* warning is logged, and "Translate now" is hidden.
|
|
136
|
+
*/
|
|
137
|
+
apiKey?: string | null;
|
|
138
|
+
/** The model id; omitted or blank uses `DEFAULT_TRANSLATE_MODEL`. */
|
|
139
|
+
model?: string | null;
|
|
140
|
+
/** The site facts the prompt carries. The plugin's own `site`, when given, wins. */
|
|
141
|
+
site?: TranslateSite;
|
|
142
|
+
/** A client to use instead of constructing one (tests, a proxied client). */
|
|
143
|
+
client?: AnthropicMessagesClient;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* The system prompt: the library's generic rules plus the site's own facts.
|
|
147
|
+
* Exported so a site can read exactly what the model is told.
|
|
148
|
+
*/
|
|
149
|
+
declare function translationSystemPrompt(request: Pick<TranslateRequest, "from" | "to" | "site">): string;
|
|
150
|
+
/** Tolerant JSON-array parse: strips accidental ``` fences and prose around the array, and checks the shape. */
|
|
151
|
+
declare function parseJsonArray(text: string): string[] | null;
|
|
152
|
+
/**
|
|
153
|
+
* The provided `Translator`: one Anthropic Messages call per batch, the
|
|
154
|
+
* strings as a JSON array in and out. `@anthropic-ai/sdk` is an optional
|
|
155
|
+
* peer, loaded on the first call, so a site with translation off installs
|
|
156
|
+
* nothing. Every failure (no key, the SDK missing, a network error, a
|
|
157
|
+
* refusal, a malformed or wrong-length reply) returns `null` and is logged
|
|
158
|
+
* by the caller; it never throws.
|
|
159
|
+
*/
|
|
160
|
+
declare function anthropicTranslator(options: AnthropicTranslatorOptions): Translator;
|
|
161
|
+
//#endregion
|
|
162
|
+
//#region src/translate/document.d.ts
|
|
163
|
+
type TranslateDocumentOptions = ({
|
|
164
|
+
collection: string;
|
|
165
|
+
id: number | string;
|
|
166
|
+
} | {
|
|
167
|
+
global: string;
|
|
168
|
+
}) & {
|
|
169
|
+
/** Target locale or locales. Omitted: every locale but the default. */locale?: string | readonly string[]; /** Re-translate every filled source string, replacing target copy a person may have corrected. */
|
|
170
|
+
overwrite?: boolean;
|
|
171
|
+
/**
|
|
172
|
+
* The source copy before the change being translated: the last published
|
|
173
|
+
* version, or the previous document on a collection without drafts. A
|
|
174
|
+
* source string that differs from it is re-translated. Omitted: there is
|
|
175
|
+
* nothing to compare with, so only blank target strings are filled.
|
|
176
|
+
*/
|
|
177
|
+
previous?: Doc | null; /** Defaults to the translator the enabled `autoTranslate` plugin registered. */
|
|
178
|
+
translator?: Translator; /** The prompt hint; defaults to the plugin's `context` for the slug, else the slug and the title. */
|
|
179
|
+
context?: string; /** Defaults to the plugin's `site`. */
|
|
180
|
+
site?: TranslateSite;
|
|
181
|
+
/**
|
|
182
|
+
* Pass only while the caller's transaction is still open (a seed inside its
|
|
183
|
+
* own transaction): the reads and writes then join it instead of opening a
|
|
184
|
+
* new one, which would deadlock against the outer operation's row locks.
|
|
185
|
+
* The plugin's hooks pass nothing, because they run past their commit.
|
|
186
|
+
*/
|
|
187
|
+
req?: PayloadRequest;
|
|
188
|
+
/**
|
|
189
|
+
* Write as this person: the target-locale write runs with
|
|
190
|
+
* `overrideAccess: false`, so the collection's `access.update` and every
|
|
191
|
+
* hook see them in `req.user`, and a refusal throws. Omitted: a server
|
|
192
|
+
* write, as the publish hook and scripts do. Opens its own transaction
|
|
193
|
+
* (it is not a `req`), so it is safe past a commit. On a collection with
|
|
194
|
+
* drafts, a document whose latest version is a draft is read and written
|
|
195
|
+
* as that draft (`draft: true`); one whose latest is published is
|
|
196
|
+
* written as a normal update. With or without a user, a target is never
|
|
197
|
+
* written live over a draft that is the latest version when the write
|
|
198
|
+
* happens: it goes into that draft instead (see the write).
|
|
199
|
+
*/
|
|
200
|
+
user?: PayloadRequest["user"];
|
|
201
|
+
};
|
|
202
|
+
/**
|
|
203
|
+
* Brings one document's target locales in step with its source locale (the
|
|
204
|
+
* config's default locale), string by string: blank target strings are
|
|
205
|
+
* filled, source strings that changed since `previous` are re-translated,
|
|
206
|
+
* and with `overwrite` every filled string is. Anything else keeps the
|
|
207
|
+
* target copy a person may have corrected.
|
|
208
|
+
*
|
|
209
|
+
* Reads the source at depth 0 (uploads as ids, array rows with their real
|
|
210
|
+
* ids) and each target with `fallbackLocale: false`, sends one batch per
|
|
211
|
+
* target locale, and writes the target with `context: { autoTranslate:
|
|
212
|
+
* false }` so the write cannot start another round.
|
|
213
|
+
*
|
|
214
|
+
* Returns one result per target locale. A translator failure is a `failed`
|
|
215
|
+
* result and leaves that locale as it was; it only throws on Payload errors
|
|
216
|
+
* or a misconfiguration (no localization, no translator, unknown slug).
|
|
217
|
+
*/
|
|
218
|
+
declare function translateDocument(payload: Payload, options: TranslateDocumentOptions): Promise<TranslateLocaleResult[]>;
|
|
219
|
+
//#endregion
|
|
220
|
+
//#region src/translate/strings.d.ts
|
|
221
|
+
/**
|
|
222
|
+
* Translates `strings` with `translator` in one batch, preserving order.
|
|
223
|
+
* Repeated strings are sent once. Returns `null` when the translator throws,
|
|
224
|
+
* returns `null`, or returns anything but one string per string sent, so a
|
|
225
|
+
* caller can leave the target as it was. Never throws.
|
|
226
|
+
*/
|
|
227
|
+
declare function translateStrings(strings: readonly string[], options: TranslateRequest & {
|
|
228
|
+
translator: Translator;
|
|
229
|
+
}): Promise<string[] | null>;
|
|
230
|
+
/** True unless the translator says it cannot run (an Anthropic translator with no API key). */
|
|
231
|
+
declare function isTranslatorConfigured(translator: Translator | undefined): translator is Translator;
|
|
232
|
+
//#endregion
|
|
233
|
+
//#region src/translate/fields.d.ts
|
|
234
|
+
/** Field configs plus the config-level blocks a `blockReferences` field points at. */
|
|
235
|
+
interface FieldSchema {
|
|
236
|
+
fields: readonly Field[];
|
|
237
|
+
/** `config.blocks`, for blocks fields that use `blockReferences`. */
|
|
238
|
+
blocks?: readonly Block[];
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* The row of `rows` that is the same row as `row`, where the two lists are
|
|
242
|
+
* one Payload array read twice (in two locales, or before and after a
|
|
243
|
+
* save): by id when the row has one, else by position.
|
|
244
|
+
*
|
|
245
|
+
* Payload gives every stored array row an id and keeps it across locales, so
|
|
246
|
+
* a row with an id is matched by it and by nothing else: a row that has an id
|
|
247
|
+
* and no partner has no partner, whatever sits at its position. Position is
|
|
248
|
+
* for rows that have no id yet (a row not saved, a fixture), and for rows of
|
|
249
|
+
* a localized array, whose ids differ per locale.
|
|
250
|
+
*/
|
|
251
|
+
declare function sameRow(rows: readonly Doc[] | undefined, row: Doc, index: number, byPosition?: boolean): Doc | undefined;
|
|
252
|
+
/**
|
|
253
|
+
* Every localized string in `doc`, each prefixed with its path, in config
|
|
254
|
+
* order. Two reads of a document compare equal exactly when no copy
|
|
255
|
+
* changed; the path keeps a string moved from one field to another from
|
|
256
|
+
* reading as unchanged.
|
|
257
|
+
*/
|
|
258
|
+
declare function collectLocalizedStrings(schema: FieldSchema, doc: unknown): string[];
|
|
259
|
+
/** The strings to translate for one target locale, and the write that lands them. */
|
|
260
|
+
interface TranslationJob {
|
|
261
|
+
/** Source-locale strings to translate, in order. May repeat; `translateStrings` dedupes. */
|
|
262
|
+
sources: string[];
|
|
263
|
+
/** The target-locale `data` for an update, given the translations of `sources` in the same order. */
|
|
264
|
+
patch: (translated: readonly string[]) => Doc;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* What to translate in one target locale, or `null` when the target is
|
|
268
|
+
* already in step with the source.
|
|
269
|
+
*
|
|
270
|
+
* - `source`: the document in the source locale, read at depth 0 so
|
|
271
|
+
* uploads are ids and array rows carry their real ids.
|
|
272
|
+
* - `target`: the document in the target locale, read with
|
|
273
|
+
* `fallbackLocale: false`. With fallback on, a blank target reads as the
|
|
274
|
+
* source, looks "already translated", and nothing is ever written.
|
|
275
|
+
* - `previous`: the source as it was before this change (the last
|
|
276
|
+
* published version, or the hook's `previousDoc` on a collection without
|
|
277
|
+
* drafts). `null` or omitted: there is none.
|
|
278
|
+
*
|
|
279
|
+
* The write carries only the top-level fields that hold a translated
|
|
280
|
+
* string. Inside them it is built from the source's structure (so
|
|
281
|
+
* non-localized values such as links and images are kept, and each array
|
|
282
|
+
* row keeps the source row's id, which is how Payload lands the target copy
|
|
283
|
+
* on the right row), with every localized string set to its translation or
|
|
284
|
+
* to the target copy it already had.
|
|
285
|
+
*/
|
|
286
|
+
declare function translationJob(input: {
|
|
287
|
+
schema: FieldSchema;
|
|
288
|
+
source: Doc;
|
|
289
|
+
target?: Doc | null;
|
|
290
|
+
previous?: Doc | null;
|
|
291
|
+
overwrite?: boolean;
|
|
292
|
+
}): TranslationJob | null;
|
|
293
|
+
//#endregion
|
|
294
|
+
//#region src/translate/guards.d.ts
|
|
295
|
+
/**
|
|
296
|
+
* Any document shape: the guard reads `_status` (absent on collections without
|
|
297
|
+
* a draft workflow) plus whatever localized fields the schema names, so it must
|
|
298
|
+
* not constrain the rest of the document.
|
|
299
|
+
*/
|
|
300
|
+
type MaybeDraft = (Doc & {
|
|
301
|
+
_status?: string | null;
|
|
302
|
+
}) | null | undefined;
|
|
303
|
+
/** True when no localized copy differs between the two documents. */
|
|
304
|
+
declare function localizedCopyUnchanged(previousDoc: unknown, doc: unknown, schema: FieldSchema): boolean;
|
|
305
|
+
/**
|
|
306
|
+
* The no-op decision for a collection or global WITHOUT a draft workflow:
|
|
307
|
+
* every write is live, so the incoming `previousDoc` is the right thing to
|
|
308
|
+
* diff. A create (no previous document) always translates.
|
|
309
|
+
*/
|
|
310
|
+
declare function shouldSkipUndraftedTranslation(opts: {
|
|
311
|
+
previousDoc?: MaybeDraft;
|
|
312
|
+
doc: MaybeDraft;
|
|
313
|
+
schema: FieldSchema;
|
|
314
|
+
}): boolean;
|
|
315
|
+
/** A drafted collection document, or a drafted global. */
|
|
316
|
+
type DraftedEntity = {
|
|
317
|
+
collection: string;
|
|
318
|
+
} | {
|
|
319
|
+
global: string;
|
|
320
|
+
};
|
|
321
|
+
interface DraftedDecision {
|
|
322
|
+
/** True for a draft save or a publish that changed no copy. */
|
|
323
|
+
skip: boolean;
|
|
324
|
+
/** The last published version's source copy, or `null` for a first publish. */
|
|
325
|
+
previous: Doc | null;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* The no-op decision for a drafts-enabled collection or global, which must
|
|
329
|
+
* NOT use the hook's `previousDoc`.
|
|
330
|
+
*
|
|
331
|
+
* Payload hands `afterChange` the most recently saved version, and with autosave
|
|
332
|
+
* on (375ms) that is the draft the editor was just typing into, carrying copy
|
|
333
|
+
* identical to what is being published. Diffing against it therefore reported
|
|
334
|
+
* "nothing changed" on essentially every admin publish, so auto-translation
|
|
335
|
+
* never ran from the admin at all. Diffing against the LAST PUBLISHED version
|
|
336
|
+
* is the question actually worth asking: did the published copy change?
|
|
337
|
+
*
|
|
338
|
+
* That also keeps the property the naive fix broke, namely that a publish which
|
|
339
|
+
* touches no localized copy (an image swap, a reordering) leaves a hand-edited
|
|
340
|
+
* target locale alone. A document with no earlier published version is a first
|
|
341
|
+
* publish and always translates, even one written entirely as a draft.
|
|
342
|
+
*
|
|
343
|
+
* Returns that previous published version too: it is the "before" the
|
|
344
|
+
* per-string rule compares each source string with.
|
|
345
|
+
*/
|
|
346
|
+
declare function draftedTranslationDecision(opts: {
|
|
347
|
+
req: PayloadRequest;
|
|
348
|
+
entity: DraftedEntity;
|
|
349
|
+
doc: MaybeDraft & {
|
|
350
|
+
id?: number | string;
|
|
351
|
+
};
|
|
352
|
+
schema: FieldSchema;
|
|
353
|
+
sourceLocale: string;
|
|
354
|
+
}): Promise<DraftedDecision>;
|
|
355
|
+
/** `draftedTranslationDecision`, answering only whether to skip. */
|
|
356
|
+
declare function shouldSkipDraftedTranslation(opts: Parameters<typeof draftedTranslationDecision>[0]): Promise<boolean>;
|
|
357
|
+
//#endregion
|
|
358
|
+
//#region src/translate/after-commit.d.ts
|
|
359
|
+
/**
|
|
360
|
+
* How long to wait for the transaction to end before abandoning the work. Far
|
|
361
|
+
* above any real publish, so reaching it means the request itself hung rather
|
|
362
|
+
* than that the work was slow.
|
|
363
|
+
*/
|
|
364
|
+
declare const TRANSACTION_TIMEOUT_MS = 30000;
|
|
365
|
+
/**
|
|
366
|
+
* Run `work` once the request's database transaction has ended, so an expensive
|
|
367
|
+
* or failure-prone tail never holds that transaction open.
|
|
368
|
+
*
|
|
369
|
+
* Payload runs `afterOperation` BEFORE commitTransaction (see updateByID.js), so
|
|
370
|
+
* no hook is late enough to do this on its own. Anything scheduled here must
|
|
371
|
+
* also open its own transaction rather than joining `req`'s: joining is
|
|
372
|
+
* mandatory only while the outer transaction is still live — once it has
|
|
373
|
+
* committed, its row locks are released and a fresh transaction is both safe
|
|
374
|
+
* and required. So `work` must pass no `req` to the Local API.
|
|
375
|
+
*
|
|
376
|
+
* The returned promise is handed to `waitUntil` (the site's, e.g. Vercel's),
|
|
377
|
+
* without which a serverless instance may be frozen the moment the response is
|
|
378
|
+
* flushed and the work would never finish. It is also returned to the caller,
|
|
379
|
+
* so tests and scripts can await the deferred work instead of sleeping.
|
|
380
|
+
*
|
|
381
|
+
* Deliberately not awaited by its callers: not awaiting is what removes the
|
|
382
|
+
* hold, and `waitUntil` is what keeps the work alive afterwards.
|
|
383
|
+
*
|
|
384
|
+
* Never rejects. The final catch is a backstop only — callers own their own
|
|
385
|
+
* error reporting — so that a defect in this helper cannot escape as an
|
|
386
|
+
* unhandled rejection and take the process down.
|
|
387
|
+
*/
|
|
388
|
+
declare function runAfterCommit(req: PayloadRequest, label: string, work: () => Promise<void>, waitUntil?: (promise: Promise<unknown>) => void): Promise<void>;
|
|
389
|
+
//#endregion
|
|
390
|
+
//#region src/translate/state.d.ts
|
|
391
|
+
interface AutoTranslateState {
|
|
392
|
+
translator: Translator;
|
|
393
|
+
site?: TranslateSite;
|
|
394
|
+
waitUntil?: (promise: Promise<unknown>) => void;
|
|
395
|
+
sourceLocale: string;
|
|
396
|
+
targetLocales: string[];
|
|
397
|
+
/** Per collection slug, its prompt hint builder. */
|
|
398
|
+
collections: Record<string, {
|
|
399
|
+
context?: (doc: Doc) => string;
|
|
400
|
+
}>;
|
|
401
|
+
/** Per global slug, its prompt hint builder. */
|
|
402
|
+
globals: Record<string, {
|
|
403
|
+
context?: (doc: Doc) => string;
|
|
404
|
+
}>;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* The test seam: the plugin's state (translator, site, locales, listed
|
|
408
|
+
* collections and globals) on a config that went through `autoTranslate`,
|
|
409
|
+
* or `undefined` when the plugin is off or was not applied. Read it instead
|
|
410
|
+
* of reaching into `config.custom` by key.
|
|
411
|
+
*/
|
|
412
|
+
declare function readTranslateState(config: Config | SanitizedConfig | Payload["config"]): Readonly<AutoTranslateState> | undefined;
|
|
413
|
+
/**
|
|
414
|
+
* The test seam: a copy of `config` whose plugin uses `translator` (a fake in
|
|
415
|
+
* a test) in place of the one it was registered with. The input is not
|
|
416
|
+
* changed. Throws when the config did not go through an enabled
|
|
417
|
+
* `autoTranslate`, so a test cannot silently translate nothing.
|
|
418
|
+
*/
|
|
419
|
+
declare function withTranslator<T extends Config | SanitizedConfig>(config: T, translator: Translator): T;
|
|
420
|
+
//#endregion
|
|
421
|
+
//#region src/translate/endpoint.d.ts
|
|
422
|
+
/** The admin component key for the "Translate now" document menu item. */
|
|
423
|
+
declare const TRANSLATE_NOW = "@bison-lab/payload-core/admin#TranslateNowMenuItem";
|
|
424
|
+
//#endregion
|
|
425
|
+
//#region src/translate/messages.d.ts
|
|
426
|
+
/** A next-intl message file: nested objects and arrays with string leaves. */
|
|
427
|
+
type MessagesJson = string | number | boolean | null | MessagesJson[] | {
|
|
428
|
+
[key: string]: MessagesJson;
|
|
429
|
+
};
|
|
430
|
+
interface FillMessagesResult {
|
|
431
|
+
/** The target file to write. */
|
|
432
|
+
messages: MessagesJson;
|
|
433
|
+
/** Keys translated. */
|
|
434
|
+
translated: number;
|
|
435
|
+
/**
|
|
436
|
+
* Keys whose batch failed, or whose translation lost a `{placeholder}`.
|
|
437
|
+
* They hold the source text, so the file stays
|
|
438
|
+
* complete (a hole renders as MISSING_MESSAGE); a plain re-run will not
|
|
439
|
+
* retry them, `--force` (or a hand edit) will.
|
|
440
|
+
*/
|
|
441
|
+
fellBack: number;
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Fills a target next-intl message file from the source file. The source is
|
|
445
|
+
* the truth; only keys that are missing or empty in the target are
|
|
446
|
+
* translated, so reviewed copy is never overwritten, unless `force`
|
|
447
|
+
* re-translates every key. Batches of 40, so one failure costs at most 40
|
|
448
|
+
* keys. `{placeholders}` survive because the translator's prompt keeps them.
|
|
449
|
+
*/
|
|
450
|
+
declare function fillMessages(options: {
|
|
451
|
+
source: MessagesJson;
|
|
452
|
+
target?: MessagesJson;
|
|
453
|
+
translator: Translator;
|
|
454
|
+
from: string;
|
|
455
|
+
to: string;
|
|
456
|
+
force?: boolean;
|
|
457
|
+
context?: string;
|
|
458
|
+
site?: TranslateSite;
|
|
459
|
+
onProgress?: (done: number, total: number) => void;
|
|
460
|
+
}): Promise<FillMessagesResult>;
|
|
461
|
+
//#endregion
|
|
462
|
+
export { type AnthropicMessagesClient, type AnthropicTranslatorOptions, type AutoTranslateOptions, type AutoTranslateState, DEFAULT_TRANSLATE_MODEL, type Doc, type FieldSchema, type MessagesJson, TRANSACTION_TIMEOUT_MS, TRANSLATE_NOW, type TranslateDocumentOptions, type TranslateLocaleResult, type TranslateRequest, type TranslateSite, type TranslatedEntity, type TranslationJob, type Translator, anthropicTranslator, autoTranslate, collectLocalizedStrings, draftedTranslationDecision, fillMessages, isTranslatorConfigured, localizedCopyUnchanged, parseJsonArray, readTranslateState, runAfterCommit, sameRow, shouldSkipDraftedTranslation, shouldSkipUndraftedTranslation, translateDocument, translateStrings, translationJob, translationSystemPrompt, withTranslator };
|
|
463
|
+
//# sourceMappingURL=translate.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"translate.d.mts","names":[],"sources":["../src/translate/types.ts","../src/translate/plugin.ts","../src/translate/anthropic.ts","../src/translate/document.ts","../src/translate/strings.ts","../src/translate/fields.ts","../src/translate/guards.ts","../src/translate/after-commit.ts","../src/translate/state.ts","../src/translate/endpoint.ts","../src/translate/messages.ts"],"mappings":";;;;KACY,GAAA,GAAM,MAAA;;AAAlB;;;;UAOiB,aAAA;EAAA;EAEf,IAAA;;EAEA,KAAA;EAFA;EAIA,KAAA;EAAA;EAEA,YAAA;EAMA;;;;AAIF;EAJE,SAAA,GAAY,MAAA;AAAA;;UAIG,gBAAA;EACf,IAAA;EACA,EAAA;EAIA;EAFA,OAAA;EAEoB;EAApB,IAAA,GAAO,aAAA;AAAA;;;;;;;;;;KAYG,UAAA,KAAe,OAAA,YAAmB,OAAA,EAAS,gBAAA,KAAqB,OAAA;EAC1E,UAAA;AAAA;;KAIU,gBAAA;EAGN,IAAA,UAEA;EAAA,OAAA,IAAW,GAAA,EAAK,GAAA;AAAA;AAAA,UAGL,oBAAA;EAHQ;AAGzB;;;;EAME,OAAA;EAMY;EAJZ,WAAA,GAAc,gBAAA;EAYP;EAVP,OAAA,GAAU,gBAAA;EAUU;EARpB,UAAA,EAAY,UAAA;EAJZ;;;;;EAUA,SAAA,IAAa,OAAA,EAAS,OAAA;EAAtB;EAEA,IAAA,GAAO,aAAA;AAAA;;UAOQ,qBAAA;EACf,MAAA;EAIA;EAFA,MAAA;EAEK;EAAL,KAAA;AAAA;;;;AAxFF;;;;;AAOA;;;;;;;;;;;;AAkBA;;;;;iBCkDgB,aAAA,CAAc,OAAA,EAAS,oBAAA,GAAuB,MAAA;;;;;AD3E9D;;;cEMa,uBAAA;;UAYI,uBAAA;EACf,QAAA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