luigimarmo / google-api-translate-java

Automatically exported from code.google.com/p/google-api-translate-java
0 stars 0 forks source link

support retrieve multiple translations in one request #65

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The google translate API supports making a single request to translate a
line into multiple languages. The tool currently only supports a single
language per request. 

I have added a public method

  Map<String, String> translate(String text, String from, String[] to)

Which allows you to pass in an array of Languages. This method returns a
Map in the format of <Language, translation> for example:

        final String from = Language.ENGLISH;
        final String[] to = {
                Language.FRENCH,
                Language.ITALIAN,
                Language.GERMAN,
                Language.SPANISH
        };

        final Map<String,String> translations = Translate.translate("hello
world", from, to);

        System.out.println(translations);

outputs

{de=Hallo Welt, it=ciao a tutti, fr=Bonjour tout le monde, es=hola mundo}

so we can call translations.get(Language.SPANISH) and have "hola mundo"

In addition to to the new method, I broke off 

private static final String LANG_VAR = "&langpair=";

from URL_STRING

I have attached the patch.

Original issue reported on code.google.com by mikenere...@gmail.com on 8 Sep 2009 at 7:03

Attachments:

GoogleCodeExporter commented 9 years ago
On that patch, also update the javadoc for the to param

   @param to[]   The language codes to translate to.

Original comment by mikenere...@gmail.com on 8 Sep 2009 at 7:04

GoogleCodeExporter commented 9 years ago
That patch was for an older version of Translate. Here is the patch for the new
version, as well as the patch for the TranslateTest.

The several languages method was changed from Map<String,String> to 
Map<Language,String>

This patch also includes the method to translate many lines into one language.

For example, similar to whats in the test...

        final Language from = Language.ENGLISH;
        final Language to = Language.SPANISH;

        final String[] lines = {
                "No one forgives with more grace and love than a child.",
                "The best way to escape from a problem is to solve it.",
                "It is not giving children more that spoils them; it is giving them
more to avoid confrontation.",
                "Morality, like art, means drawing a line someplace."
        };

        final String[] translations = Translate.execute(lines, from, to);

        for(int i = 0, length = translations.length; i<length; i++)
        {
            System.out.println(lines[i]);
            System.out.print("  =  ");
            System.out.println(translations[i]);
        }

Outputs

No one forgives with more grace and love than a child.
  =  No se perdona con más gracia y el amor de un niño.
The best way to escape from a problem is to solve it.
  =  La mejor manera de escapar de un problema es resolverlo.
It is not giving children more that spoils them; it is giving them more to avoid
confrontation.
  =  No se trata de dar a los niños más que los mima, es dar más a evitar la
confrontación.
Morality, like art, means drawing a line someplace.
  =  La moral, como el arte, significa trazar una línea en algún lugar.

Original comment by mikenere...@gmail.com on 8 Sep 2009 at 8:51

Attachments:

GoogleCodeExporter commented 9 years ago
Found a bug when a single line or single language is passed where a JSONObject 
is
returned (a single entry) rather than the expected JSONArray (many entries).

New tests and updated methods.

Original comment by mikenere...@gmail.com on 9 Sep 2009 at 2:20

Attachments:

GoogleCodeExporter commented 9 years ago
One last note on this issue. Using the batch API options a method could be 
written
that translates several lines into several languages.

For example, this URL translates the words One, Two, Three, Four into french,
italian, spenish, and german.

http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=One&langpair
=en|it&q=One&langpair=en|fr&q=One&langpair=en|de&q=One&langpair=en|es&q=Two&lang
pair=en|it&q=Two&langpair=en|fr&q=Two&langpair=en|de&q=Two&langpair=en|es&q=Thre
e&langpair=en|it&q=Three&langpair=en|fr&q=Three&langpair=en|de&q=Three&langpair=
en|es&q=Four&langpair=en|it&q=Four&langpair=en|fr&q=Four&langpair=en|de&q=Four&l
angpair=en|es

This would probably require either a POJO or complicated tree map to manage the
response and turn it into a usable model.

Here is the response...

{
   "responseData":[
      {
         "responseData":{
            "translatedText":"Uno"
         },
         "responseDetails":null,
         "responseStatus":200
      },
      {
         "responseData":{
            "translatedText":"Un"
         },
         "responseDetails":null,
         "responseStatus":200
      },
      {
         "responseData":{
            "translatedText":"Eins"
         },
         "responseDetails":null,
         "responseStatus":200
      },
      {
         "responseData":{
            "translatedText":"Uno"
         },
         "responseDetails":null,
         "responseStatus":200
      },
      {
         "responseData":{
            "translatedText":"Due"
         },
         "responseDetails":null,
         "responseStatus":200
      },
      {
         "responseData":{
            "translatedText":"Deux"
         },
         "responseDetails":null,
         "responseStatus":200
      },
      {
         "responseData":{
            "translatedText":"Zwei"
         },
         "responseDetails":null,
         "responseStatus":200
      },
      {
         "responseData":{
            "translatedText":"Dos"
         },
         "responseDetails":null,
         "responseStatus":200
      },
      {
         "responseData":{
            "translatedText":"Tre"
         },
         "responseDetails":null,
         "responseStatus":200
      },
      {
         "responseData":{
            "translatedText":"Trois"
         },
         "responseDetails":null,
         "responseStatus":200
      },
      {
         "responseData":{
            "translatedText":"Drei"
         },
         "responseDetails":null,
         "responseStatus":200
      },
      {
         "responseData":{
            "translatedText":"Tres"
         },
         "responseDetails":null,
         "responseStatus":200
      },
      {
         "responseData":{
            "translatedText":"Quattro"
         },
         "responseDetails":null,
         "responseStatus":200
      },
      {
         "responseData":{
            "translatedText":"Quatre"
         },
         "responseDetails":null,
         "responseStatus":200
      },
      {
         "responseData":{
            "translatedText":"Vier"
         },
         "responseDetails":null,
         "responseStatus":200
      },
      {
         "responseData":{
            "translatedText":"Cuatro"
         },
         "responseDetails":null,
         "responseStatus":200
      }
   ],
   "responseDetails":null,
   "responseStatus":200
}

Original comment by mikenere...@gmail.com on 9 Sep 2009 at 6:48

GoogleCodeExporter commented 9 years ago
Thanks for this Mike. I've made a few changes as a result of your suggestions, 
hopefully covering most use cases.

The changes wil be in the next release.

Original comment by rich.mid...@gmail.com on 20 Sep 2009 at 5:35

GoogleCodeExporter commented 9 years ago
I looked at your commit. I have not run it but from scanning the code it looks 
like
translating "hello world" from one language to 4 languages will result in 4 
requests
to ajax.googleapis.com where as my patch utilizes the API provided by Google to
perform the 4 translations in a single request. 

There is a big difference between the two strategies.

Original comment by mikenere...@gmail.com on 20 Sep 2009 at 5:44

GoogleCodeExporter commented 9 years ago
Indeed. I intend to change that method to employ the other new method which 
allows for 
translating multiple texts to multiple languages before the release, which will 
then 
use a single server request. It was a shortcut to ensure the unit tests work 
whilst 
ensuring server access is limited to as few places as possible.

Original comment by rich.mid...@gmail.com on 20 Sep 2009 at 5:49