使用 Google 翻译 API 翻译 iOS 中的多个字符串
Translate more than one string in iOS with Google Translate API
我正在尝试用 Google Translate API 翻译一些字符串,但我不能用 API 的最新版本一次翻译多个字符串。我正在使用 Alamofire 发出请求,但是当我尝试使用与文档中相同的键创建参数字典时,它当然会失败。参数应该是这种格式。
{ 'q': 'Hello world',
'q': 'My name is Jeff',
'target': 'de' }
有人知道如何使用相同的参数键发送此请求吗?
我让它工作的唯一方法是更改请求的 httpBody。
var requestBody = "{"
for (_, value) in allText {
requestBody.append("\'q\':\'\(value)\',")
}
requestBody.append("\'source\':\'\(Language.english.rawValue)\',")
requestBody.append("\'target\':\'\(language.rawValue)\'")
requestBody.append("}")
var request = URLRequest(url: url!)
request.httpMethod = HTTPMethod.post.rawValue
request.setValue("application/json; charset=UTF-8", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData
用 Alamofire 执行请求对我有用。
我真的不知道为什么无法提供解决方案的人对问题投反对票,但无论如何,希望能帮助可能 运行 完成类似任务的人。
我正在尝试用 Google Translate API 翻译一些字符串,但我不能用 API 的最新版本一次翻译多个字符串。我正在使用 Alamofire 发出请求,但是当我尝试使用与文档中相同的键创建参数字典时,它当然会失败。参数应该是这种格式。
{ 'q': 'Hello world', 'q': 'My name is Jeff', 'target': 'de' }
有人知道如何使用相同的参数键发送此请求吗?
我让它工作的唯一方法是更改请求的 httpBody。
var requestBody = "{"
for (_, value) in allText {
requestBody.append("\'q\':\'\(value)\',")
}
requestBody.append("\'source\':\'\(Language.english.rawValue)\',")
requestBody.append("\'target\':\'\(language.rawValue)\'")
requestBody.append("}")
var request = URLRequest(url: url!)
request.httpMethod = HTTPMethod.post.rawValue
request.setValue("application/json; charset=UTF-8", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData
用 Alamofire 执行请求对我有用。
我真的不知道为什么无法提供解决方案的人对问题投反对票,但无论如何,希望能帮助可能 运行 完成类似任务的人。