import { v2 } from "@google-cloud/translate";
const translate = new v2.Translate({
key: import.meta.env.CLOUD_TRANSLATE_API_KEY,
const headingsPath = new URL("./headings.json", import.meta.url);
const headings = JSON.parse(fs.readFileSync(headingsPath, "utf8"));
// const languages = ["bn", "gu", "hi", "ja", "kn", "ml", "ne", "or", "pa", "ta", "te"];
const languages = ["ja"];
const delay = (ms) => new Promise((r) => setTimeout(r, ms));
fs.writeFileSync(headingsPath, JSON.stringify(headings, null, 2) + "\n");
for (const lang of languages) {
console.log(`Translating to ${lang}...`);
for (const book of headings) {
for (const [, chapterHeadings] of Object.entries(book.chapters)) {
for (const h of chapterHeadings) {
if (!h.heading?.en) continue;
const [translation] = await translate.translate(h.heading.en, lang);
h.heading[lang] = translation;
console.error(` Error translating "${h.heading.en}" to ${lang}:`, err.message);
const [translation] = await translate.translate(h.heading.en, lang);
h.heading[lang] = translation;
console.error(` Failed retry for "${h.heading.en}" to ${lang}:`, err2.message);
if (translated % 50 === 0) {
console.log(` ${lang}: ${translated} translated, ${skipped} skipped`);
console.log(` ${lang} done. ${translated} translated, ${skipped} skipped`);
console.log(`Done! ${translated} translated, ${skipped} skipped`);