Skip to content

Commit 063983d

Browse files
fix: translate from any language, always overwrite existing vi files
1 parent e27f9ae commit 063983d

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

src/translate.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ const LLM_CONCURRENCY = 3;
2525
const MAX_RETRIES = 3;
2626
const RETRY_BASE_MS = 5_000;
2727

28-
const ZH_REPORTS = ["ai-cli", "ai-agents", "ai-web", "ai-trending", "ai-hn"];
29-
const ROLLUP_REPORTS = ["ai-weekly", "ai-monthly"];
30-
const ALL_REPORTS = [...ZH_REPORTS, ...ROLLUP_REPORTS];
28+
const ALL_REPORTS = ["ai-cli", "ai-agents", "ai-web", "ai-trending", "ai-hn", "ai-weekly", "ai-monthly"];
3129

3230
const client = new OpenAI({
3331
apiKey: process.env["OPENAI_API_KEY"],
@@ -64,7 +62,7 @@ async function callLlm(prompt: string): Promise<string> {
6462
{
6563
role: "system",
6664
content:
67-
"You are a Chinese-to-Vietnamese translator. You MUST output ONLY Vietnamese text. Never output Chinese.",
65+
"You are a professional translator to Vietnamese. You MUST output ONLY Vietnamese text. Never output Chinese or English (except proper nouns, URLs, code).",
6866
},
6967
{ role: "user", content: prompt },
7068
],
@@ -90,7 +88,8 @@ async function callLlm(prompt: string): Promise<string> {
9088
}
9189

9290
function buildPrompt(markdown: string): string {
93-
return `You are a professional translator. Translate the following Markdown document from Chinese to Vietnamese.
91+
return `Translate the following Markdown document to Vietnamese.
92+
The source may be in Chinese or English — detect automatically and translate to Vietnamese.
9493
9594
Rules:
9695
- Preserve ALL Markdown formatting exactly (headings, tables, links, bold, italic, code blocks, blockquotes, lists).
@@ -113,27 +112,21 @@ function todayCST(): string {
113112
}
114113

115114
async function translateFile(datePath: string, report: string): Promise<boolean> {
116-
const zhFile = path.join(datePath, `${report}.md`);
117-
const viFile = path.join(datePath, `${report}-vi.md`);
115+
const srcFile = path.join(datePath, `${report}.md`);
118116

119-
if (!fs.existsSync(zhFile)) {
117+
if (!fs.existsSync(srcFile)) {
120118
console.log(`[translate] Skip ${report} — source not found`);
121119
return false;
122120
}
123121

124-
if (fs.existsSync(viFile)) {
125-
console.log(`[translate] Skip ${report} — Vietnamese version exists`);
126-
return false;
127-
}
128-
129-
const zhContent = fs.readFileSync(zhFile, "utf-8");
130-
if (!zhContent.trim()) {
122+
const srcContent = fs.readFileSync(srcFile, "utf-8");
123+
if (!srcContent.trim()) {
131124
console.log(`[translate] Skip ${report} — source is empty`);
132125
return false;
133126
}
134127

135-
console.log(`[translate] Translating ${report} (${zhContent.length} chars)...`);
136-
const viContent = await callLlm(buildPrompt(zhContent));
128+
console.log(`[translate] Translating ${report} (${srcContent.length} chars)...`);
129+
const viContent = await callLlm(buildPrompt(srcContent));
137130
console.log(`[translate] Result preview: ${viContent.slice(0, 200).replace(/\n/g, " ")}`);
138131
const outPath = path.join(datePath, `${report}-vi.md`);
139132
fs.mkdirSync(path.dirname(outPath), { recursive: true });

0 commit comments

Comments
 (0)