Google Translate ajax API

Difference between version 0 and 1 - Previous - Next
I needed to check language and translate a lot of texts, so I wrote those 2 procs inspired from [Google AJAX search API] and getting all I needed from http://code.google.com/apis/ajaxlanguage/documentation/reference.html#_intro_fonje%|%official page%|% 

I'm testing it right now, I made +5000 requests for lang_detect and around 1500 translations in one day, and google let me do ... (500 ms between each lang_detect and 1s between each translation).
   `g_translate` { ''`string`'': } r Returns a string of text translated to english or `-1` in case of error.
   `lang_detect` { ''`string`'': } r Returns the detected language as a 2 chars string  and a floating number between 1 and 0 representing confidence. In case of error, language is "error" and confidence is `-1`.

----
 ======
package require http
 package require json
 proc g_translate {to_translate} {
     set api_key "{<your API key here>" }
     set q [::http::formatQuery sig $api_key q $to_translate v 1.0 ]
     set url "http://ajax.googleapis.com/ajax/services/language/translate?langpair=%7Cen"
     set t [http::geturl $url -query $q]
     set translated ""{}
     if { [http::ncode $t] == 200 } {
         set d [http::data $t]
         set idx 0
         while { $idx >= 0 } {
             set idx [string first {responseData} $d $idx]
             if { $idx >= 0 } {
                 set jd [json::json2dict [string range $d [expr {$idx-2}] end]]
                 if {[dict get $jd responseData] !=ne "{null"}} {
                     set translated [dict get $jd responseData translatedText]
                 }
                 incr idx 5
             }
         }
     } else {
       set translated -1
       puts "Error=[http::error $t]"
       puts "Status=[http::status $t]"
       puts "Code=[http::code $t]"
       puts "Ncode=[http::ncode $t]"
       puts "Data=[http::data $t]"
     }
     http::cleanup $t
     return $translated
 }
 proc lang_detect { to_translate } {
  set api_key "<your API key here>"
  set q [::http::formatQuery sig $api_key q $to_translate v 1.0 ]
  set url "http://ajax.googleapis.com/ajax/services/language/detect?"
  # works only with GET 
  append url $q
  set t [http::geturl $url]
  set translated ""
  if { [http::ncode $t] == 200 } {
    set d [http::data $t]
    set idx 0
    while { $idx >= 0 } {
        set idx [string first {responseData} $d $idx]
        if { $idx >= 0 } {
            set jd [json::json2dict [string range $d [expr {$idx-2}] end]]
            if {[dict get $jd responseData] != "null"} {
             set lang [dict get $jd responseData language]
             set conf [dict get $jd responseData confidence]
            }
            incr idx 5
        }
    }
  } else {
    set lang "error"
    set conf -1
    puts "Error=[http::error $t]"
    puts "Status=[http::status $t]"
    puts "Code=[http::code $t]"
    puts "Ncode=[http::ncode $t]"
  }
  http::cleanup $t
  return "$lang $conf"
 }
proc lang_detect to_translate {
    set api_key {<your API key here>}
    set q [::http::formatQuery sig $api_key q $to_translate v 1.0 ]
    set url "http://ajax.googleapis.com/ajax/services/language/detect?"
    # works only with GET 
    append url $q
    set t [http::geturl $url]
    set translated {} 
    if {[http::ncode $t] == 200} {
      set d [http::data $t]
      set idx 0
      while {$idx >= 0} {
          set idx [string first {responseData} $d $idx]
          if {$idx >= 0} {
              set jd [json::json2dict [string range $d [expr {$idx-2}] end]]
              if {[dict get $jd responseData] ne {null}} {
               set lang [dict get $jd responseData language]
               set conf [dict get $jd responseData confidence]
              }
              incr idx 5
          }
      }
    } else {
      set lang error
      set conf ---1
      puts Error=[http::error $t]
      puts Status=[http::status $t]
      puts Code=[http::code $t]
      puts Ncode=[http::ncode $t]
    }
    http::cleanup $t
    return "$lang $conf"
}
======