Skip to content

Commit

Permalink
refactor: improve translation handling 🌐
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Oct 10, 2024
1 parent bc4c940 commit f7160c0
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions genaisrc/loc-strings.genai.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand All @@ -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<string, string> =
(await workspace.readJSON(trfn)) || {}
for (const k of Object.keys(strings)) if (translated[k]) delete strings[k]

// shortcut: all translation is done
Expand All @@ -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 => {
Expand Down Expand Up @@ -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" }
)
Expand All @@ -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)

Expand Down

0 comments on commit f7160c0

Please sign in to comment.