【问题标题】:How get translation from Google Translate API v2 php?如何从 Google Translate API v2 php 获取翻译?
【发布时间】:2016-02-22 21:00:03
【问题描述】:

我使用https://github.com/google/google-api-php-client 进行翻译。

当我尝试时

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

我明白了

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
        (
        )

)

但是当我尝试使用 getTranslations() 函数获取翻译时 - 我得到了空数组。请指教!

【问题讨论】:

    标签: php google-api-php-client google-translate


    【解决方案1】:

    图书馆好像坏了。

    您可以使用它来获取翻译!

    $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"]);
    

    会给你

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

    我提交了PR that fixes this,但尚未被接受。

    【讨论】:

      猜你喜欢
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-18
      • 2014-04-21
      • 1970-01-01
      相关资源
      最近更新 更多