|
| 1 | +import { readFile, writeFile } from "node:fs/promises" |
| 2 | +import { openai } from "@ai-sdk/openai" |
| 3 | +import { tasks } from "@clack/prompts" |
| 4 | +import { generateObject } from "ai" |
| 5 | +import dotenv from "dotenv" |
| 6 | +import { execa } from "execa" |
| 7 | +import { po } from "gettext-parser" |
| 8 | +import { objectKeys } from "ts-extras" |
| 9 | +import { z } from "zod" |
| 10 | +import { LanguageIdDefault, Languages } from "#constants/language.ts" |
| 11 | +import type { LanguageId } from "#constants/language.ts" |
| 12 | + |
| 13 | +process.chdir(import.meta.dirname) |
| 14 | +dotenv.config() |
| 15 | + |
| 16 | +const $ = execa({ |
| 17 | + stdout: ["inherit", "pipe"], |
| 18 | + verbose: "short", |
| 19 | + preferLocal: true, |
| 20 | +}) |
| 21 | + |
| 22 | +await $`lingui extract` |
| 23 | +await translateLanguages() |
| 24 | +// Handled by Vite |
| 25 | +// await $`lingui compile` |
| 26 | + |
| 27 | +async function translateLanguages() { |
| 28 | + await tasks( |
| 29 | + objectKeys(Languages).map(languageId => ({ |
| 30 | + title: `Translating ${languageId}`, |
| 31 | + task: () => translateLanguage(languageId), |
| 32 | + })), |
| 33 | + ) |
| 34 | +} |
| 35 | + |
| 36 | +// TODO: extract empty messages and translate using JSON/JSONSchema |
| 37 | +async function translateLanguage(languageId: LanguageId) { |
| 38 | + if (languageId === LanguageIdDefault) return |
| 39 | + |
| 40 | + const path = `locales/${languageId}/messages.po` |
| 41 | + const source = await readFile(path) |
| 42 | + |
| 43 | + const pofile = po.parse(source) |
| 44 | + const translations = pofile.translations[""] |
| 45 | + |
| 46 | + if (!translations) { |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + const missingTranslations = Object.values(translations).filter( |
| 51 | + message => message.msgstr && !message.msgstr.filter(Boolean).length, |
| 52 | + ) |
| 53 | + |
| 54 | + if (!missingTranslations.length) { |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + const result = await generateObject({ |
| 59 | + model: openai("gpt-4.1"), |
| 60 | + schema: z.object({ |
| 61 | + translations: z.array( |
| 62 | + z.object({ |
| 63 | + msgid: z.string(), |
| 64 | + msgid_plural: z.string().optional(), |
| 65 | + msgstr: z.array(z.string()), |
| 66 | + }), |
| 67 | + ), |
| 68 | + }), |
| 69 | + prompt: ` |
| 70 | + You are a professional translator. Here is untranslated PO (gettext) translations. |
| 71 | + Translate them from **${LanguageIdDefault}** to **${languageId}**. |
| 72 | +
|
| 73 | + - Keep all msgid values unchanged |
| 74 | + - Preserve all placeholders like {variable}, %s, {{name}}, etc. |
| 75 | + - Maintain the exact JSON format |
| 76 | +
|
| 77 | + PO (gettext) translations: |
| 78 | + ${JSON.stringify(missingTranslations, null, 2)} |
| 79 | + `, |
| 80 | + }) |
| 81 | + |
| 82 | + // Update added translations |
| 83 | + for (const translatedMessage of result.object.translations) { |
| 84 | + const missingMessage = translations[translatedMessage.msgid] |
| 85 | + if (missingMessage) { |
| 86 | + missingMessage.msgstr = translatedMessage.msgstr |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Delete removed translations |
| 91 | + pofile.obsolete = {} |
| 92 | + |
| 93 | + const target = po.compile(pofile, { foldLength: Number.POSITIVE_INFINITY }) |
| 94 | + await writeFile(path, target) |
| 95 | +} |
0 commit comments