如何防止Google翻译Api转换“ \ n”,“ \”和“ /”

问题描述 投票:0回答:1

[我正在尝试从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个回答
0
投票

翻译后相同的问题是\ n。有些人建议在请求中添加“ format”:“ text”,但这没有帮助。当我解码JSON响应时,由于\。

而崩溃

在我解码响应之前,我做了:translationText = translationText.replaceAll(“ \ n”,“ \ n”); // Dart

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

© www.soinside.com 2019 - 2024. All rights reserved.