这是我用来从英语翻译成越南语的代码。
NSString *urlPath = [NSString stringWithFormat:@"/translate_a/t?client=t&text=%@&langpair=en|vi",txt_view.text];
NSURL *url = [[NSURL alloc] initWithScheme:@"http"
host:@"translate.google.com"
path:urlPath];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"GET"];
NSURLResponse *response;
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if(error == nil)
{
NSString *result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"Result : %@", result);
NSArray *arr = [api tocanizeTheString:result tocan:@","];
result = [arr objectAtIndex:0];
result = [api replaceStringwithString:result strTobeReplaced:@"[" stringReplaceWith:@""];
result = [api replaceStringwithString:result strTobeReplaced:@"\"" stringReplaceWith:@""];
txt_view.text = result; // here i get only translated data...
}
else
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
有时它会给出准确的结果,有时它不会。请告诉我有什么问题。
“我的名字是 Taimur Mushtaq”我在我的应用程序中输入这个,它显示正确的结果是“Tên tôi là Taimur Mushtaq”。
“我是个好孩子”的结果应该是“Tôi là một chàng trai tốt”。相反,它是“tôi là m\u1ED9t c\u1EADu bé t\u1ED1t”