Table of contents
1.
Introduction
2.
Cloud Translation Editions
2.1.
Cloud Translation - Basic (v2)
2.1.1.
Translating input strings
2.1.2.
Model Parameter
2.2.
Cloud Translation - Advanced (v3)
2.2.1.
Translating text
2.2.2.
AutoML Translation custom model to translate text (Node.js)
2.2.3.
Model Parameter
3.
Comparison- Basic and Advanced
3.1.
Cloud Translation - Basic (v2)
3.2.
Cloud Translation - Advanced (v3)
4.
Frequently Asked Questions
4.1.
What is the Cloud Translation API?
4.2.
Is Google Cloud translation free?
4.3.
Does AI power Google Translate?
5.
Conclusion
Last Updated: Mar 27, 2024

Cloud Translation

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Google's Cloud Translation service offers quick translations between the source language and more than 100 other languages. The add-on is extremely helpful for those that offer the store content in multiple languages. Since it drastically cuts down on time requirements. It is simple to obtain a literal translation with just one click. Simply add a new product, and an automatic translation into the target language or languages is generated.

In contrast to internet translators, Google Cloud Translation stores the translated text in a database. As a result, search queries can use the results.

For example, you may have a global support team that gets client cases in a variety of languages. To convert those customer cases into the native tongue of your support team members, you can add translation to your process.

Translation increases the overall efficiency of your support team because they no longer need to manually translate text or rely on others.

Cloud Translation Editions

Cloud Translation has two editions available:

  • Cloud Translation - Basic (v2)
     
  • Cloud Translation - Advanced (v3)
     

Cloud Translation - Basic (v2)

HTML or plain text can be used as the input text. Cloud Translation - Basic only translates text between HTML elements in the input. Due to variances between the source and target languages, the output keeps the original HTML tags while, when possible, translating the text inside the tags. Due to word order changes during translation, the output HTML tags may not appear in the same order as the input text.

Translating input strings

Follow Node.js setup instructions in the Translation quickstart using client libraries before attempting this sample.

Implementing in Node.js

// Imports the Google Cloud client library
const {Translate} = require('@google-cloud/translate').v2;

// Creates a client
const translate = new Translate();

/**
 * TODO for developer, uncomment the following lines before running the sample.
 */
// const text = 'The text to translate, e.g., Hello, world!';
// const target = 'The target language, e.g. ru';

async function translateText() {
  // Translate the text into the target language. "text" can be a string for
  // Translating a single string or an array of strings to translate multiple texts
  let [translations] = await translate.translate(text, target);
  translations = Array.isArray(translations) ? translations : [translations];
  console.log('Translations:');
  translations.forEach((translation, i) => {
    console.log(`${text[i]} => (${target}) ${translation}`);
  });
}

translateText();
You can also try this code with Online Javascript Compiler
Run Code

Model Parameter

When you submit a translation request to Cloud Translation - Basic, your text is translated using the Google Neural Machine Translation (NMT) model. No other model is permitted. Use Cloud Translation - Advanced to translate text using AutoML models.

Cloud Translation - Advanced (v3)

Cloud Translation - Advanced supports translating text using customized AutoML Translation models and generating glossaries to ensure that the Cloud Translation API accurately translates a customer's domain-specific terminology.
You can select the translation model to utilize when submitting a translation request to Cloud Translation - Advanced. The nmt model is applied if no model is specified.

 

By using its model ID, you can specify the translation model. The model ID for the NMT model is general/nmt. For details on AutoML Translation model IDs, see the section that follows.

Translating text

HTML or plain text can be used as the text input. Only text included within HTML tags is translated using the Cloud Translation API. To the maximum extent, the output retains the (untranslated) HTML tags with the translated content between the tags due to linguistic differences between the source and destination languages. The output HTML tags may not be in the same order as the input text due to word order changes during translation.

Implementing in Node.js

/**
 * TODO for developers, uncomment these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'global';
// const text = 'text to translate';


// Imports the Google Cloud Translation library
const {TranslationServiceClient} = require('@google-cloud/translate');


// Instantiates a client
const translationClient = new TranslationServiceClient();


async function translateText() {
  // Construct request
  const request = {
    parent: `projects/${projectId}/locations/${location}`,
    contents: [text],
    mimeType: 'text/plain', // mime types: text/plain, text/html
    sourceLanguageCode: 'en',
    targetLanguageCode: 'sr-Latn',
  };


  // Run request
  const [response] = await translationClient.translateText(request);


  for (const translation of response.translations) {
    console.log(`Translation: ${translation.translatedText}`);
  }
}


translateText();
You can also try this code with Online Javascript Compiler
Run Code


AutoML Translation custom model to translate text (Node.js)

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const modelId = 'YOUR_MODEL_ID';
// const text = 'text to translate';

// Imports the Google Cloud Translation library
const {TranslationServiceClient} = require('@google-cloud/translate');

// Instantiates a client
const translationClient = new TranslationServiceClient();
async function translateTextWithModel() {
  // Construct request
  const request = {
    parent: `projects/${projectId}/locations/${location}`,
    contents: [text],
    mimeType: 'text/plain', // mime types: text/plain, text/html
    sourceLanguageCode: 'en',
    targetLanguageCode: 'ja',
    model: `projects/${projectId}/locations/${location}/models/${modelId}`,
  };

  // Run request
  const [response] = await translationClient.translateText(request);

  for (const translation of response.translations) {
    console.log(`Translated Content: ${translation.translatedText}`);
  }
}

translateTextWithModel();
You can also try this code with Online Javascript Compiler
Run Code


Model Parameter

Rest and CMD line

The model query parameter allows you to specify which model to use for translation. To use the NMT model, for example, enter nmt. The instance below demonstrates how to use the model parameter in a translate request. Make the following substitutions before using any of the request data:

  • PROJECT_NUMBER_OR_ID: your Google Cloud project number or ID
     

HTTP method and URL:

POST https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/global:translateText


Request JSON body:

{
  "model": "projects/PROJECT_NUMBER_OR_ID/locations/global/models/general/nmt",
  "sourceLanguageCode": "en",
  "targetLanguageCode": "de",
  "contents": ["Come here!"]
}


Using Powershell to send your request:

Save the request body to a file called request.json and run the command:

$cred = gcloud auth application-default print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }


Invoke-WebRequest `
 -Method POST `
 -Headers $headers `
 -ContentType: "application/json; charset=utf-8" `
 -InFile request.json `
 -Uri "https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/global:translateText
" | Select-Object -Expand Content

 

You should get a JSON answer that looks similar to this:

{
  "translations": [
    {
      "translatedText": "Komm her!",
      "model": "projects/project-number/locations/global/models/general/nmt"
    }
  ]
}

Comparison- Basic and Advanced

There are two Cloud Translation editions available: Basic and Advanced. The edition you use is determined by the version of the client library or service endpoint you are using. The following descriptions highlight the key distinctions between the two.

If you are starting a new project, use Cloud Translation - Advanced to take advantage of new capabilities and service enhancements. Cloud Translation - Basic is still available, however it no longer supports additional capabilities such as using glossaries, requesting batch translations, or using AutoML models.

Cloud Translation - Basic (v2)

Cloud Translation - Basic allows you to use Google's pre-trained Neural Machine Translation (NMT) model to dynamically translate across languages. Basic is designed for simplicity and scalability. It's ideal for apps that deal largely with casual user-generated material, such as chat, social networking, or comments.

Cloud Translation - Advanced (v3)

Cloud Translation - Advanced includes all of the features of Cloud Translation - Basic plus more. Cloud Translation - Advanced is designed for customization and long-form content use cases. Glossaries and custom model selection are examples of customization options.

Frequently Asked Questions

What is the Cloud Translation API?

Google's neural machine translation technology is used via the Cloud Translation API to instantaneously translate texts into over a hundred languages. In the lab, you will use the API to translate text and determine its written language.

Is Google Cloud translation free?

You are just charged for the text you supplied; there is no additional payment for detection in addition to the translation.

Does AI power Google Translate?

The NMT system used by Google Translate is a massive artificial neural network capable of deep learning. GNMT enhances translation quality by leveraging millions of samples and deducing the most relevant translation from a broader context.

Conclusion

We had explored the Cloud Translation in this blog, as well as details about the two editions available and a comparison of them.
If you think the blog has helped you with an overview of Cloud Translation API, and if you like to learn more, check out our articles Cloud Computing, Cloud Computing Technologies, Cloud Computing Infrastructure, and Overview of a log-based metric.

Refer to our Coding Ninjas Studio Guided Path to learn Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and even more! You can also check out the mock test series and participate in the contests hosted by Coding Ninjas Studio! But say you're just starting and want to learn about questions posed by tech titans like Amazon, Microsoft, Uber, and so on. In such a case, for placement preparations, you can also look at the problemsinterview experiences, and interview bundle.

Do upvote our blogs if you find them helpful and engaging!

Happy Coding!

Live masterclass