From f7160c019b05aa0d8e3430187d6bc5a4ff09be59 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Thu, 10 Oct 2024 19:34:02 +0000 Subject: [PATCH] =?UTF-8?q?refactor:=20improve=20translation=20handling=20?= =?UTF-8?q?=F0=9F=8C=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- genaisrc/loc-strings.genai.mts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/genaisrc/loc-strings.genai.mts b/genaisrc/loc-strings.genai.mts index 92ae0f67e..eda21a9fd 100644 --- a/genaisrc/loc-strings.genai.mts +++ b/genaisrc/loc-strings.genai.mts @@ -32,6 +32,7 @@ for (const file of files) { await translateFile(file) } console.log(YAML.stringify(files.map(f => f.filename))) + async function translateFile(file: WorkspaceFile) { const { filename, content } = file console.log(`> ${filename}`) @@ -42,8 +43,8 @@ async function translateFile(file: WorkspaceFile) { // find the existing translation and remove existing translations const trfn = path.join(dir, langCode, path.basename(filename)) - const trsrc = await workspace.readText(trfn) - const translated = parsers.JSON5(trsrc) || {} + const translated: Record = + (await workspace.readJSON(trfn)) || {} for (const k of Object.keys(strings)) if (translated[k]) delete strings[k] // shortcut: all translation is done @@ -54,9 +55,6 @@ async function translateFile(file: WorkspaceFile) { console.log(`strings: ${Object.keys(strings).length} to translate`) - // serialize as ini - const contentToTranslate = INI.stringify(strings) - // the prompt engineering piece const { fences, text } = await runPrompt( ctx => { @@ -98,7 +96,7 @@ and should be translated following these rules: - The translations of "...|block" string should be short. ` - ctx.def("ORIGINAL", contentToTranslate, { language: "ini" }) + ctx.def("ORIGINAL", INI.stringify(strings), { language: "ini" }) }, { label: filename, cache: "translate" } ) @@ -111,7 +109,10 @@ and should be translated following these rules: delete news[key] } + // merge the translations Object.assign(translated, news) + + // write the translations back if modified const newContent = JSON.stringify(translated, null, 2) if (content !== newContent) await workspace.writeText(trfn, newContent)