July 21, 2008

Google Translate API

Nikolai Kordulla


Today I implemented a google translate api for protocol buffers. To use this api in php just download google protocol for php, then create the following proto file:
message TranslateRequest
  optional string sl = 1 [default = 'en'];
  optional string tl = 2 [default = 'de'];
  optional string text = 3;

message TranslateResponse
  optional string text = 1;
throw it in the parser (it will automatically create the file pb_proto_translate.php)
// just create the protoclasses
$parser = new PBParser();
and write the following code to translate the phrase "the night is dark, and the day is bright" to simplified chinese.
// charset utf-8
header('Content-Type: text/html; charset=UTF-8');

// first include pb_message

// include the generated file

// set up the translationrequest
$t = new TranslateRequest();
$t->set_text('the night is dark, and the day is bright');

$tr = new TranslateResponse();
$t->Send('http://coderpeek.com/api/translate', $tr);

echo 'Translation:';
echo $tr->text();
the following chars will then appear on your screen



Leave a Reply

Don't enter anything here:
Name (required) Invalid name
Mail (will not be published) (required) Invalid mail
Website Invalid url

Content couldnt be empty.