I’ve got a Google Doc that I’m constantly updating and I need a translated version that stays current automatically. Right now I’m using the built-in translate feature under Tools menu, but this creates a one-time translation. Every time I edit the original document, I have to manually translate it again which is really annoying.
I’m wondering if there’s a way to set up automatic synchronization so when I change something in the source document, the translated version updates itself. In Google Sheets there’s that handy GOOGLETRANSLATE formula that works great.
I’ve been experimenting with Apps Script and found the LanguageApp.translate method. Here’s what I tried so far:
function autoTranslate() {
var sourceDoc = DocumentApp.openById('source-document-id-here');
var translatedText = LanguageApp.translate(sourceDoc, 'es', 'fr');
Logger.log(translatedText);
return translatedText;
}
The problem is I can’t figure out how to extract the actual text content from the document object, and I’m not sure how to write the translated content back to the target document. Also, I only see time-based triggers available - should I be running this script from the original document instead of the destination one?
Try getBody().getText() to pull the text from your source doc. For writing it back, open the target with openById(), then getBody().clear() and appendParagraph(). You might want to use an installable trigger instead of time-based - maybe try onChange?
Had this exact problem last year with multilingual docs. Your Apps Script idea works, but you’ll need DocumentApp.openById('your-id').getBody().getText() to actually pull the content. Fair warning though - getText() kills all your formatting and paragraph breaks. For syncing, skip time-based triggers and use installable ones instead. Set up an onEdit trigger on your source doc, but heads up - it gets weird with collaborative editing. I ended up doing a hybrid thing where the script runs every few minutes but checks a version stamp so it’s not translating the same stuff over and over. One thing that bit me: Google Translate API has daily quotas even on the free tier. You’ll hit limits fast if you’re updating longer docs frequently.
Your problem is that LanguageApp.translate() wants text, not a document object. You’re passing it the wrong thing. First, extract the text with sourceDoc.getBody().getText(), then translate that. To write it back, use targetDoc.getBody().clear() then targetDoc.getBody().setText(translatedText). Just a heads up - this method removes all formatting. If you need to keep formatting, you’ll have to loop through each paragraph and element separately, which can be complicated. For triggers, avoid time-based ones and use onEdit directly on your source document. It activates when content changes but may have issues with the mobile app and collaboration. Additionally, keep in mind that Google’s translation quota is limited, so frequent updates on large documents can quickly reach rate limits.
This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.