使用example.php
<?php
header('Content-Type: text/html;charset=utf-8');
require_once 'gtranslate.php';
$gt=new gtranslate();
echo $gt->translate('Hello, I am Don', 'ta','en',true);
gtranslate.php
<?php
class gtranslate {
function __construct() {
$this->translate_url = 'https://translate.google.com/m?ie=UTF-8&prev=_m&hl=en&';
$this->urlReferer = 'https://translate.google.com/m';
$this->userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
}
public function translate($text, $to, $from = '',$cache=false) {
$url = $this->translate_url . 'sl=' . $from . '&tl=' . $to . '&q=' . urlencode(@$text);
if(file_exists('cache/'.$to.'/'.md5($url).'.cache') && $cache){
return file_get_contents('cache/'.$to.'/'.md5($url).'.cache');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
curl_setopt($ch, CURLOPT_REFERER, $this->urlReferer);
curl_setopt($ch, CURLOPT_URL, $url);
$resp = $this->parseResponse(curl_exec($ch));
if(!file_exists('cache')){
@mkdir('cache');
}
if(!file_exists('cache/'.$to)){
@mkdir('cache/'.$to);
}
@file_put_contents('cache/'.$to.'/'.md5($url).'.cache', $resp);
return $resp;
}
private function parseResponse($str = '') {
$result = strip_tags($str, '<div>');
$result = explode('<', substr($result, strpos($result, 'class="t0"') + 11, strpos($result, 'class="t0"')));
$result = $result[0];
return $result;
}
}
此代码将英语翻译成泰米尔语,但我想要的是用泰米尔语回应该单词的发音。
答案 0 :(得分:-1)
我认为你正在寻找音译。 https://developers.google.com/transliterate/v1/getting_started这是它的api。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="https://www.google.com/jsapi">
</script>
<script type="text/javascript">
// Load the Google Transliterate API
google.load("elements", "1", {
packages: "transliteration"
});
function onLoad() {
var options = {
sourceLanguage:
google.elements.transliteration.LanguageCode.ENGLISH,
destinationLanguage:
[google.elements.transliteration.LanguageCode.HINDI],
shortcutKey: 'ctrl+g',
transliterationEnabled: true
};
// Create an instance on TransliterationControl with the required
// options.
var control =
new google.elements.transliteration.TransliterationControl(options);
// Enable transliteration in the textbox with id
// 'transliterateTextarea'.
control.makeTransliteratable(['transliterateTextarea']);
}
google.setOnLoadCallback(onLoad);
</script>
</head>
<body>
Type in Hindi (Press Ctrl+g to toggle between English and Hindi)<br>
<textarea id="transliterateTextarea" style="width:600px;height:200px"></textarea>
</body>
</html>
https://google-developers.appspot.com/transliterate/v1/richedittransliteration就是它的真实例子。