How to Translate Google Docs to English with Google Apps Script

If you’ve ever faced the challenge of translating a Google Doc, you know it can be tedious to copy-paste content into a translation tool. Luckily, Google Apps Script offers a seamless way to integrate translation right into your Google Docs. In this post, we’ll explore how to create a custom Google Apps Script function that allows you to translate the content of your document to English with a single click. Let’s dive in!

What Does the Script Do?

The script we’re creating automates the translation of a Google Doc into English using Google Apps Script. It leverages the LanguageApp service to make the translation process efficient and straightforward. Here’s a summary of what the script does:

  1. Retrieves the content of the active Google Doc.
  2. Translates all the text into English.
  3. Replaces the existing content with the translated version.
  4. Adds a custom menu item, allowing users to trigger the translation easily.

The Code Explained

Here’s the full code that we’ll be using:

function translateDocToEnglish() {
  try {
    // Get the active document
    const doc = DocumentApp.getActiveDocument();
    const body = doc.getBody();
    
    // Get all the text from the document
    const originalText = body.getText();

    // Translate the text to English using Google Apps Script's LanguageApp service
    const translatedText = LanguageApp.translate(originalText, null, "en");

    // Clear the document's current content and replace it with the translated text
    body.setText(translatedText);

    // Log a success message to indicate completion
    Logger.log("Document successfully translated to English.");
  } catch (e) {
    // Log an error message if something goes wrong
    Logger.log("Error: " + e.message);
  }
}

function onOpen() {
  const ui = DocumentApp.getUi();
  ui.createMenu('Translate')
    .addItem('Translate to English', 'translateDocToEnglish')
    .addToUi();
}

// To automatically add the menu to the Google Doc when it is opened
function onInstall(e) {
  onOpen(e);
}

Step-by-Step Breakdown

1. Main Translation Function (translateDocToEnglish)

This function is responsible for the translation process. It retrieves the active Google Doc’s content, translates it to English, and replaces the original content. The function utilizes LanguageApp.translate(), a built-in Google Apps Script service, to perform the translation efficiently.

  • Get the active document: const doc = DocumentApp.getActiveDocument(); retrieves the Google Doc that is currently open.
  • Get all the text: body.getText(); extracts the full text of the document.
  • Translate to English: LanguageApp.translate(originalText, null, "en"); uses Google’s language translation API to translate the text. The null parameter indicates that the original language should be automatically detected.
  • Set the translated text: body.setText(translatedText); replaces the document’s content with the translated version.
  • Error handling: The try...catch block ensures that any errors are logged for troubleshooting.

2. Adding a Custom Menu (onOpen)

The onOpen function adds a custom menu to your Google Doc, making it easier to use the translation function whenever you need it.

  • This function uses DocumentApp.getUi() to access the document’s user interface.
  • It then creates a new menu item called Translate, with an option to Translate to English, which will call the translateDocToEnglish function when clicked.

3. Installing the Menu on Open (onInstall)

The onInstall function ensures that the menu is available whenever a user opens the document. It essentially calls the onOpen function to add the custom menu.

How to Use the Script

  1. Open Google Apps Script Editor: Open your Google Doc and navigate to Extensions > Apps Script to open the Google Apps Script editor.
  2. Paste the Code: Copy the above code and paste it into the script editor.
  3. Save and Run: Save your script and run the onOpen function once to authorize the script. After that, every time you open the document, the Translate menu will be available.
  4. Translate Your Document: Click on the Translate menu and select Translate to English to translate the entire document.

Benefits of Using This Script

  • Effortless Translation: No need to copy and paste text into external tools. Simply click and translate.
  • Automatic Language Detection: The script detects the document’s language automatically, making it versatile.
  • Customization: You can modify the script to add more language options if needed.

Potential Use Cases

  • Collaborative Documents: If you’re working on a shared document with colleagues who speak different languages, you can use this script to quickly translate content to English.
  • Research Translation: Easily translate academic papers, reports, or notes into English for better comprehension.
  • Learning and Practice: This is a great way to practice Google Apps Script and get familiar with Google Workspace automation.

Final Thoughts

This script provides a simple but powerful way to translate Google Docs into English directly, without needing to switch between applications. It highlights how Google Apps Script can enhance productivity and workflow automation. Feel free to customize it for different languages or add more functionalities to suit your needs.

If you’re interested in learning more about Google Apps Script, check out our other posts where we dive into automation, custom functions, and how you can extend Google Workspace to make your work even easier!