【问题标题】:How to prevent Google translate Api from converting "\n" , "\" and "/"如何防止谷歌翻译 Api 转换 "\n" 、 "\" 和 "/"
【发布时间】:2019-05-28 09:37:48
【问题描述】:

我正在尝试从 JSON throw PHP 转换字符串。 此字符串包含 \n\/。 翻译后这个字符消失或添加额外的空格,这取决于这个字符的位置。

例子:

red\ncars => voitures \ nrouges

red\n cars => voitures rouges

red/cars => rouge / voitures

...

我需要像这样保留这些字符

red\ncars => voiture\nrouge

red\n cars => voiture\n rouge

red/cars => rouge/voitures

...

$apiKey = 'xxx';
$source = 'en';
$target = 'fr'; 
$original_text = "voiture\nrouges";                 
$url = 'https://www.googleapis.com/language/translate/v2?key=' . $apiKey . '&q=' . rawurlencode($original_text) . '&source='.$source.'&target='.$target;
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($handle);                 
$responseDecoded = json_decode($response, true);
curl_close($handle);            
$translated_text = $responseDecoded['data']['translations'][0]['translatedText'];   
echo $translated_text ;

如何解决?

【问题讨论】:

    标签: php google-translate


    【解决方案1】:

    同样的问题\n是翻译后的\n。 有人建议在请求中添加 "format" : "text" ,但没有帮助。 当我解码 JSON 响应时,它因 \ 而崩溃。

    在我解码响应之前,我做了: translateText = translateText.replaceAll("\n", "\n"); // 飞镖

    嗯.... 已经使用了一段时间的紧急解决方案

    【讨论】:

      猜你喜欢
      • 2020-10-02
      • 2011-03-28
      • 1970-01-01
      • 2016-11-18
      • 2015-11-10
      • 2011-12-26
      • 2010-10-10
      相关资源
      最近更新 更多