我正在尝试使用我的iOS应用程序翻译中文。使用" Google Translate API"在翻译中引入了一些错误,但是使用相同的文字,谷歌翻译网页不会在翻译中引入错误。 "谷歌翻译" API响应还包括"& quot"和"&#39"字符。以下是与其相关的代码段。
//Text recognized by tesseract.
text = [recText stringByAddingPercentEncodingWithAllowedCharacters:set];
NSString * urlText =[NSString stringWithFormat:@"https://www.googleapis.com/language/translate/v2?key=%@&source=%@&target=%@&q=%@",_key,_sourceLanguage,_targetLanguage,text];
NSURL *url = [NSURL URLWithString:urlText];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
NSError *error;
[request setURL:url];
[request setHTTPMethod:@"GET"];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
if(response != nil)
{
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:&error];
NSDictionary *data = dic[@"data"];
NSArray *translations = data[@"translations"];
for (NSDictionary *translation in translations)
{
translatedText = translation[@"translatedText"];
}
}