1

我正在尝试使用我的 iOS 应用程序翻译中文。使用“谷歌翻译 API”会在翻译中引入一些错误,但对于相同的文本,谷歌翻译网页不会在翻译中引入错误。“谷歌翻译”API 响应还包括“"”和“'”字符。下面是与之相关的代码片段。

//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"];
        }
    }
4

0 回答 0