Categories

(83)
(69)
(8)
(34)
(74)
(149)

Autotranslation of text

15.03.2011
Autotranslation of text
Author:

Quite often during the website development, 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 into 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 its 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 it, 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:

PHP

https://www.googleapis.com/language/translate/v2?parameters

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

php
GET https://www.googleapis.com/language/translate/v2?key=API-KEY&q=Hello,%20world!&source=en&target=ru

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:

php
<html>   
 <head> 
   <title>Google Translate API</title> 
 </head> 
 <body> 
   <div id="sourceText">Hello,world!</div> 
   <div id="translation"></div> 
   <script> 
     function translateText(response) { 
       document.getElementById("translation").innerHTML += "<br>" + response.data.translations[0].translatedText; 
     } 
   </script> 
   <script> 
     var newScript = document.createElement('script'); 
     newScript.type = 'text/javascript'; 
     var sourceText = escape(document.getElementById("sourceText").innerHTML);
     var source = 'https://www.googleapis.com/language/translate/v2?key=API-KEY&source=en&target=ru&callback=translateText&q=' + sourceText;
     newScript.src = source; 
      document.getElementsByTagName('head')[0].appendChild(newScript); 
   </script> 
 </body> 
</html>

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:

   php
https://ajax.googleapis.com/ajax/services/language/translate?parameters

Previous example of translation using this method will look the following way:

https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=Hello,%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:

php
* 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         = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0'; 
  $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 registration on Google API is not required. There is also a set of additional query parameters that can provide security of its performance.

6 votes, Rating: 5

Read also

1

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...

3

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

4

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

5

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

Subscribe to our blog updates