【问题标题】:Google Cloud Translate: cryptic error 400 invalid value谷歌云翻译:神秘错误 400 无效值
【发布时间】:2019-12-24 20:41:46
【问题描述】:

我正在使用谷歌翻译(基本版)翻译一些字符串。几分钟前它工作正常,但现在它只返回错误 400。代码非常简单:

function translatePhrase($text, $target, $source = 'it') {

    $sourceLanguage = $source;
    $targetLanguage = $target; 

    $translate = new TranslateClient();
    $result = $translate->translate($text, [
        'source' => $sourceLanguage,
        'target' => $targetLanguage,
    ]);

    $output = $result['text'];

    return $output;
}

返回:

Uncaught Google\Cloud\Core\Exception\BadRequestException: {
"error": {
"code": 400,
"message": "Invalid Value",
"errors": [
{
"message": "Invalid Value",
"domain": "global",
"reason": "invalid"
}
]
}
}
in \vendor\google\cloud-core\src\RequestWrapper.php:362

Stack trace:
#0 \vendor\google\cloud-core\src\RequestWrapper.php(206): Google\Cloud\Core\RequestWrapper->convertToGoogleException(Object(GuzzleHttp\Exception\ClientException))
#1 \translate\vendor\google\cloud-core\src\RestTrait.php(95): Google\Cloud\Core\RequestWrapper->send(Object(GuzzleHttp\Psr7\Request), Array)
#2 \translate\vendor\google\cloud-translate\src\V2\Connection\Rest.php(83): Google\Cloud\Translate\V2\Connection\Rest->send('translations', 'translate', Array)
#3 \translate\vendor\google\cloud-translate\src\V2\TranslateClient.php(248): Google\Cloud\Translate\V2\Connection\Rest->listTra
in [\translate\vendor\google\cloud-core\src\RequestWrapper.php riga 362]

有什么想法吗?

【问题讨论】:

    标签: php google-cloud-platform google-translate


    【解决方案1】:

    正如人们所看到的,它完全出错了here ...所以选项数组有问题。它应该看起来像这样(因为没有单个示例通过 source 语言代码,而是返回自动检测到的 $result['source']):

    function translatePhrase($text, $target) {
        $translate = new TranslateClient();
        $result = $translate->translate($text, [
            'target' => $target
        ]);
        return $result['text'];
    }
    

    【讨论】:

      【解决方案2】:

      我知道原因了。

      基本上,我是在强制使用错误的源语言。当我指定正确的源语言时,它又可以工作了。

      【讨论】:

      • 一个人不必声明任何源语言,一开始就 - 可能只有几个术语存在于不同的语言中,具有不同的含义(只要它是一个句子,它应该使用自动检测没问题)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 2020-03-27
      • 2020-09-25
      相关资源
      最近更新 更多