July 21, 2008

Google Translate API

Nikolai Kordulla

0 comments >>

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)
require_once('../pb/parser/pb_parser.php');
// just create the protoclasses
$parser = new PBParser();
$parser->parse('./translate.proto');
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
require_once('../message/pb_message.php');

// include the generated file
require_once('./pb_proto_translate.php');

// set up the translationrequest
$t = new TranslateRequest();
$t->set_sl('en');
$t->set_tl('zh-CN');
$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
Translation:夜是黑暗的,和天是光明的

Comments

 

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.