As the world becomes more connected, communicating with people in different parts of the world is increasingly important. Google Translate can help you understand and be understood in many languages, including Vietnamese. In this article, we will show you how to utilize Google Translate API with Google Apps Script to create a script that can translate English to Vietnamese automatically.
Step 1: Create a Google Cloud Project and Enable Google Translate API
The first thing you need to do is to create a Google Cloud Project and enable Google Translate API. Follow these simple steps:
- Go to the Google Cloud Console (https://console.cloud.google.com/).
- In the project drop-down, select Create Project.
- Enter a Project name and click Create.
- After the project is created, select the project from the project drop-down.
- Click on the hamburger menu on the left and navigate to APIs & Services > Dashboard.
- Click on the Enable APIs and Services button and search for “Google Cloud Translation API”.
- Click on the Google Cloud Translation API and then click on the Enable button to enable this API.
Step 2: Get the Google Cloud API Key
To be able to use Google Cloud Translation API, you will need an API key. Follow these steps to get an API key:
- From the same Google Cloud Console window, navigate to APIs & Services > Credentials.
- Click the Create Credentials button and select API key.
- Copy the API key generated and save it for later use.
Step 3: Create a New Google Apps Script
Now we are ready to create a new Google Apps Script.
- Go to the Google Apps Script page: https://www.google.com/script/start/.
- Click on the New Script button.
- Give your script a name (e.g. “English to Vietnamese Translation”).
- After you create the script, copy and paste the following code into it.
function translateText() { var apiKey = 'YOUR_API_KEY'; // Replace YOUR_API_KEY with your API key var sourceText = 'Hello, how are you?'; // Replace this with the text to be translated var url = 'https://translation.googleapis.com/language/translate/v2?key=' + apiKey; var payload = { q: sourceText, source: 'en', target: 'vi' }; var options = { method: 'post', payload: payload }; var response = UrlFetchApp.fetch(url, options); var result = JSON.parse(response.getContentText()); Logger.log(result.data.translations[0].translatedText); }
- Replace YOUR_API_KEY with the API key that you copied earlier.
- Replace ‘Hello, how are you?’ with the text that you want to translate.
- Click on the Run button to run the script.
The code sends a POST request to the Translate API with the text to be translated and the source/target languages as parameters. The API then returns a JSON object with the translated text, which is logged to the console.
Step 4: Test the Translation
To test the script:
- Click on the Run button again.
- Open the Logs window by clicking on the View > Logs menu.
- Check the logs to verify that the translated text was returned.
Congratulations! You have created a successful English to Vietnamese translation with Google Translate using Google Apps Script.
Step 5: Advanced Usage
To use this script to translate a large amount of text, you can modify the code to read text from a spreadsheet or document, and then output the translated text to another sheet or document.
Here’s an example of a code modification for translating text from a column in a Google Sheet:
function translateSheet() {
var apiKey = 'YOUR_API_KEY'; // Replace YOUR_API_KEY with your API key
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange(1, 1, sheet.getLastRow(), 1); // Assumes text is in column A
var values = range.getValues();
for (var i = 0; i < values.length; i++) {
var sourceText = values[i][0];
var url = 'https://translation.googleapis.com/language/translate/v2?key=' + apiKey;
var payload = {
q: sourceText,
source: 'en',
target: 'vi'
};
var options = {
method: 'post',
payload: payload
};
var response = UrlFetchApp.fetch(url, options);
var result = JSON.parse(response.getContentText());
sheet.getRange(i + 1, 2).setValue(result.data.translations[0].translatedText); // Outputs translated text to column B
}
}
This code reads text from column A in the active sheet, translates it using the API, and outputs the translated text to column B.
Happy translating!