Skip to content

Instantly share code, notes, and snippets.

@harumaxy
Last active November 29, 2022 14:25
Show Gist options
  • Save harumaxy/60d78a10e043f072ed1c0034ec745fbb to your computer and use it in GitHub Desktop.
Save harumaxy/60d78a10e043f072ed1c0034ec745fbb to your computer and use it in GitHub Desktop.
import { GTR } from "https://deno.land/x/[email protected]/mod.ts";
function zip<T, U>(a: T[], b: U[]): Array<[T, U]> {
return a.map((k, i) => [k, b[i]]);
}
const fileArg = Deno.args[0];
if (!fileArg) {
console.error("Please provide a file path");
Deno.exit(1);
}
const lines = (await Deno.readTextFile(fileArg)).split("\n");
const alreadyTranslated = lines.filter((line) => line.includes("=>"));
const toTranslate = lines.filter((line) => !line.includes("=>"));
const gtr = new GTR();
const { trans } = await gtr.translate(toTranslate.join("\n"), {
targetLang: "ja",
});
const result = zip(
toTranslate,
trans.split("\n").map((ja) => ja.trim())
).map(([en, ja]) => `${en} => ${ja}`);
const outLines = alreadyTranslated.concat(result);
await Deno.writeTextFile(fileArg, outLines.join("\n"));
// input.txt
// 努力
// 未来
// A
// bueatiful
// star
// deno run --unstable -A translate-lines.ts input.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment