无法翻译与谷歌翻译

时间:2014-05-26 07:45:24

标签: javascript jquery ajax html5 google-translate

我尝试使用Google翻译翻译文字:

 $.ajax({
        type : "GET",
        url : "https://www.googleapis.com/language/translate/v2",
        dataType : 'jsonp',
        cache : false,
        contentType : "application/x-www-form-urlencoded; charset=UTF-8",
        data : "key="+translateKey+"&source=en&target=de&q=Hello",
        success : function(res) {
            console.log('res', res.data.translations[0].translatedText);
        },
        error : function(xhr, ajaxOptions, thrownError) {
        }
    });

    $.get("https://www.googleapis.com/language/translate/v2", {
        key : translateKey,
        source : "en",
        target : "fr",
        q : "hello"
    }, function(res) {
        console.log(res.data.translations[0].translatedText);
    }, "json").fail(function(jqXHR, textStatus, errorThrown) {
        alert("error :" + errorThrown);
    });

我正在使用API​​':

  • Google+ API
  • Google+域名API
  • 翻译API

我创建了一个Google项目并插入了我的本地主机和我的真实网站地址。 我收回了错误403禁止。

我尝试将其复制到这样的网址中: 的 https://www.googleapis.com/language/translate/v2?key=AIzaSyCClt5fo3FDMAym59FQw-R5D39eaTNfWIg&source=en&target=de&q=Hello

我得到了错误JSON。 我哪里错了?

我用AJAX和Javascript写作。

在这里答案!!!

你必须支付..但如果有人想使用它们,那么它们是php的黑客攻击:

    <?php
function curl($url,$params = array(),$is_coockie_set = false){
if(!$is_coockie_set){
/* STEP 1. let’s create a cookie file */
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
/* STEP 2. visit the homepage to set the cookie properly */
$ch = curl_init ($url);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
}
$str = ''; $str_arr= array();
foreach($params as $key => $value){
$str_arr[] = urlencode($key)."=".urlencode($value);
}
if(!empty($str_arr))
$str = '?'.implode('&',$str_arr);
/* STEP 3. visit cookiepage.php */
$Url = $url.$str;
$ch = curl_init ($Url);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
return $output;
}
function Translate($phrase, $from , $to){
$ret = "";
if(strlen($phrase)>1600){ //split and translate each part individually to get around 2048 size limit:
$phrases = str_split($phrase,1600);
foreach($phrases as $p) $ret .= translate($p, $from, $to);
return($ret);
}else{
$url = "http://translate.google.com/translate_a/t?client=p&q=".urlencode($phrase)."&hl={$to}&sl={$from}&tl={$to}&ie=UTF-8&oe=UTF-8&multires=0" ; // client = p !
$out = json_decode(curl($url), 1);
foreach($out['sentences'] as $sentence){
$ret .= $sentence['trans'].' ';
}
return stripslashes(trim($ret));
}}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body dir="rtl">
<?php
$text = "my name is name" ;
echo  Translate($text,'en','iw');
?>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

最好是找一位经验丰富的专业网站翻译。我不相信任何翻译工具的翻译工具,人工翻译是这类工作的最佳选择。

答案 1 :(得分:0)

转到Google API Console,点击“启用API”,然后搜索“翻译API”并将其启用(http://pastemehere.com/ai2xhwxm)。

相关问题