Autotranslation of text


Quite often when the site's being developed, there is a need to provide it with a multi-language support option - the feature enabling translating the site content into multiple languages. The task is quite feasible within Drupal environment as prompted by such modules as Internationalization, Content translation, Locale.

But what  if, instead of multilingual localization, there's a need to enable autotranslation of the site content from one language into another? Google service, a handy translation tool, can well be utilized for the purpose, too, in which case, one of the advantages being that it gets easily integrated into Drupal. To perform autotranslate of the site's content one can make use of Google Translate or Google Translate Filter modules.

The former allows for translating the page into any language supported by Google via creating a block that chooses the selected language. A shortcoming of this module is that it  only  accomplishes visual translation of the page without logging the changes into the base.

The second module enables adding autotranslation of text filter in input formats and it gets activated while the content is being saved and then translates the text nto the selected language. Unfortunately, there is only dev version of this module available,  that's why there might be some issues with its operation. The module's main flaw, though, is it's inability to process texts that have more than 2000 symbols.

Having analyzed the existing options for autotranslation, I have to admit that all of them require editor's participation to a lesser or greater extent. So what can be done to secure the  autotranslation  option that does not require any editorship?

For example, in the case of news feed as being posted from the Enlish-language site  with rss with automatic translation into the selected language  and eventually  saved into DB. What can be done in this situation? This task can be performed through one of  Google's  interfaces, with its programming service - Google Translate API  applied.

Google Translate API is a tool that enables  automatic  translations of the text from one language into another. While using iе one should consider 3 main parameters:

  • source text (q) - text that has to be translated
  • source language (source) - the language in which source text is written;
  • target language (target) - the language into which the text is translated.

There are several ways to activate API:

  • using REST directly;
  • using REST from JavaScript (no server code is  required).

When sticking to these methods one should utilize another parameter - API key for project that can be found in Google APIs console. Let's take a closer look at the usage of the mentioned methods.

REST, or Representational State Transfer, in Google Translate API provides access to the service, not the resources. As a result, API provides the only URI that acts as a final element of the service. Some very detailed information about the service must be indicated in the request, such as parameters of query.

The format for Google Translate API URI is having the following look:


If necessary parameters have been inserted into the processing query, the processor will look the following way:


REST from JavaScript allows to activate Google translate API using the reverse parameter of the query and function of the reverse call. This allows for some rich applications being written to have the translated texts displayed without the server code to be written out.

In the example below you can see how this approach can be applied:

   <title>Google Translate API</title> 
   <div id="sourceText">Hello,world!</div> 
   <div id="translation"></div> 
     function translateText(response) { 
       document.getElementById("translation").innerHTML += "<br>" +[0].translatedText; 
     var newScript = document.createElement('script'); 
     newScript.type = 'text/javascript'; 
     var sourceText = escape(document.getElementById("sourceText").innerHTML);
     var source = '' + sourceText;
     newScript.src = source; 

There is another way to do the autotranslation - with use of Google Translate API with JSON. This option is possible through writing a code and doesn't require to insert API key of project. Request accepts 3 necessary parameters:

  • source text (q);
  • version of protocol (v);
  • source and target languages (langpair), devided by symbol "|".

Syntax for request has the following look:


Previous example of translation using this method will look the following way:,%20world!&langpair=en%7ru

Besides the necessary parameters, the current request can also accept secondary ones:

  • reverse argument (callback);
  • additional argument for reverse (context);
  • format of result (format);
  • key to identify site (key);
  • IP-address of the final user on the name of which query is done (userip).

After the query has been done we will get the following structure of the result:

* error? 
   If error happened in text translation: 
    * code 
       HTTP code of error . 
    * message 
       Error description. 
  *  translation 
       Translated text. 
  *  detectedSourceLanguage? 
       If source language was detected automatically, it will be returned here.

That's how it can be written in PHP:

* Translate text with Google API service 
function _google_translate($text, $lang_from='en', $lang_to='ru') {
  $url_prefix         = ''; 
  $text                = urlencode($text); 
  $lang_from        = urlencode($lang_from); 
  $lang_to            = urlencode($lang_to); 
  $langpair           = $lang_from . '|' . $lang_to; 
  $service_url        = $url_prefix . '&q=' . $text . '&langpair=' . $langpair;
  $translated_text  = @file_get_contents($service_url); 
  $json                 = json_decode($translated_text, true);
  if ($json['responseStatus'] != 200){ 
    return false; 
  return $json['responseData']['translatedText']; 

To my mind this option is easier and more practical because it offers an easier way to control and delete errors, have the text autotranslated without editor's participation, plus the site registation on Google API is not required. There is a also a set of additional query parameters that can provide security of its performance.

5 votes, Rating: 5

Read also


JavaScript code, just using php and DrupalForm API, based on the state of the second (or even on the basis of several elements), after any user's...


XML - RPC - simple remote procedure call protocol.  XML- RPC - is the father of one of  the most popular protocol SOAP....


The following article is giving a description of Titanium Appcelerator platform opportunities....


In theprevious article we shared about creating install profiles for Drupal 6. In this article it'll be described how to create...


In Drupal 7 API there is a chapter dedicated to site theming. All elements of site, without exclusion, should go through the theming process. But first of all theming itself should be set up.

Need a quote? Let's discuss the project

Are you looking for someone to help you with your Drupal Web Development needs? Let’s get in touch and discuss the requirements of your project. We would love to hear from you.

Join the people who have already subscribed!

Want to be aware of important and interesting things happening? We will inform you about new blog posts on Drupal development, design, QA testing and more, as well news about Drupal events.

No charge. Unsubscribe anytime