doukefu1361 2016-02-22 21:00
浏览 244
已采纳

如何从Google Translate API v2 php获得翻译?

I using https://github.com/google/google-api-php-client for translate.

When I try

print_r($translation->listTranslations( "John go home", "es" ));

I got

Google_Service_Translate_TranslationsListResponse Object
(
    [collection_key:protected] => translations
    [internal_gapi_mappings:protected] => Array
        (
        )
    [translationsType:protected] => Google_Service_Translate_TranslationsResource
    [translationsDataType:protected] => array
    [modelData:protected] => Array
        (
            [data] => Array
                (
                    [translations] => Array
                        (
                            [0] => Array
                                (
                                    [translatedText] => John ir a casa
                                    [detectedSourceLanguage] => en
                                )

                        )

                )

        )

    [processed:protected] => Array
        (
        )

)

But when I try to get translations using getTranslations() function - i got empty array. Please advice!

  • 写回答

1条回答 默认 最新

  • doukang1962 2016-02-22 22:27
    关注

    It looks like the library is broken.

    You can use this to get the translations out though!

    $client = new Google_Client();
    
    $client->setDeveloperKey('xxxx-your-dev-key-xxxx');
    
    
    $translate = new Google_Service_Translate($client);
    $translations = $translate->translations->listTranslations('Hello world!', 'fr');
    
    
    var_dump($translations->data);
    var_dump($translations->data['translations'][0]["translatedText"]);
    

    Will give you

    array(1) {
      ["translations"]=>
      array(1) {
        [0]=>
        array(2) {
          ["translatedText"]=>
          string(17) "Bonjour le monde!"
          ["detectedSourceLanguage"]=>
          string(2) "en"
        }
      }
    }
    string(17) "Bonjour le monde!"
    

    I submitted a PR that fixes this, but it hasn't been accepted yet.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C语言设计一个简单的自动换档程序
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。