使用谷歌translator API翻译一个PHP $string。

时间:2022-10-17 11:05:59

been google'ing for a while how is the best way to translate with google translator in PHP, found very different ways converting URLS, or using Js but i want to do it only with php (or with a very simple solution JS/JQUery)

谷歌'ing在PHP中使用谷歌翻译的最好方法是,找到了非常不同的方法来转换url,或者使用Js,但我只想用PHP(或者使用一个非常简单的解决方案Js /JQUery)

example:

例子:

//hopefully with $from_lan and $to_lan being like 'en','de', .. or similar
function translate($from_lan, $to_lan, $text){

// do

return $translated_text;

}

can you give me a clue? or maybe you already have this function..

你能给我一个提示吗?或者你已经有了这个函数

my intention it's to use it only for the languages i have not already defined (or keys i haven't defined), that's why i wan it so simple, will be only temporal..

我的意图是只使用那些我还没有定义的语言(或者我没有定义的键),这就是为什么我要这么简单,只是暂时的。

EDIT

编辑

thanks for your replies we are now trying this soulutions:

谢谢你的回复,我们现在正在尝试这种方法:

function auto_translate($from_lan, $to_lan, $text){
// do


$json = json_decode(file_get_contents('https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($text) . '&langpair=' . $from_lan . '|' . $to_lan));
$translated_text = $json->responseData->translatedText;


return $translated_text;

}

}

(there was a extra 'g' on variables for lang... anyway)

(朗的变量有一个额外的“g”。无论如何)

it returns: works now :)

它返回:现在工作:)

i don't really understand much the function, so any idea why is not acepting the object? (now i do)

我不太理解这个函数,所以知道为什么不接受这个对象吗?(现在我做)

OR:

或者:

    function auto_translate($from_lan, $to_lan, $text){
    // do

//    $json = json_decode(file_get_contents('https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($text) . '&langpair=' . $from_lan . '|' . $to_lan));
//    $translated_text = $json['responseData']['translatedText'];
    error_reporting(1);
    require_once('GTranslate.php');
    try{
       $gt = new Gtranslate();
       $translated_text = $gt->english_to_german($text);

     } catch (GTranslateException $ge)
     {
           $translated_text= $ge->getMessage();
     }


    return $translated_text;
}

And this one looks great but it doesn't even gives me an error, the page won't load (error_report(1) :S)

这个看起来很棒,但它甚至不会给我一个错误,页面不会加载(error_report(1):S)

thanks in advance!

提前谢谢!

1 个解决方案

#1


11  

I haven't tested this yet, but try:

我还没有测试过,但是试试:

function translate($from_lan, $to_lan, $text){
    $json = json_decode(file_get_contents('https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($text) . '&langpair=' . $from_lan . '|' . $to_lan));
    $translated_text = $json->responseData->translatedText;

    return $translated_text;
}

EDIT: Fixed, tested and works.

编辑:固定,测试和工作。

#1


11  

I haven't tested this yet, but try:

我还没有测试过,但是试试:

function translate($from_lan, $to_lan, $text){
    $json = json_decode(file_get_contents('https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($text) . '&langpair=' . $from_lan . '|' . $to_lan));
    $translated_text = $json->responseData->translatedText;

    return $translated_text;
}

EDIT: Fixed, tested and works.

编辑:固定,测试和工作。