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