php - How to prevent Google translate Api from converting "\n" , "\" and "/"
问题描述
I'm trying to translate strings from JSON throw PHP.
This strings contains \n
, \
, /
.
After translation this character disapears or extra spaces will be added, it depends on the position of this chars.
Example:
red\ncars
=> voitures \ nrouges
red\n cars
=> voitures rouges
red/cars
=> rouge / voitures
...
I need to keep this chars like this
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 ;
How can fix it ?
解决方案
同样的问题\n是翻译后的\n。有人建议在请求中添加 "format" : "text" ,但没有帮助。当我解码 JSON 响应时,它由于 \ 而崩溃。
在我解码响应之前,我做了: translateText = translateText.replaceAll("\ n", "\n"); // 飞镖
嗯....恐慌解决方案已经使用了一段时间了
推荐阅读
- android - 当 ConstraintLayout 膨胀时,您必须提供 layout_width 属性
- haskell - 为什么镜头包含用于 fromEnum/toEnum 的 Iso,但没有用于显示/读取的 Iso?
- css - Bootstrap 4 - align-items-* 类根本不起作用
- java - Extracting image src in HTML with jsoup
- r - 用带有对象的多行表达式注释 ggplot 图?
- reactjs - 使用 Graphql-dotnet 上传 Asp.net 核心文件
- django - “用户=验证(请求,用户名=用户名,密码=密码)”用户是无
- swift - How to create higher order functions in Swift?
- jquery - 如何在内联时让 2 个图像出现/消失
- tensorflow - Keras:矩阵大小不兼容:In[0]:[1,16384],In[1]:[1024,10]