2

Let me start saying that I did some homework, but I did not find any answer to my question.

I'm trying to use the translate.google.com/translate_a url to translate single word from swedish to english. I'm doing this in objective-c.

here is an example:

word = @"hus"; // result should be "house"

urlPath = [NSString stringWithFormat:@"/translate_a/t?client=t&text=%@&langpair=sv|en", word];
url = [[NSURL alloc] initWithScheme:@"http" host:@"translate.google.com" path:urlPath];
NSLog(@"%@\n\n", url);
request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"GET"];
myData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
result = [[NSString alloc] initWithData:myData encoding: NSASCIIStringEncoding];
NSLog(@"%@\n\n", result);
self.viewController.textViewGood.text = result;

the NSLog gives me this url:
http://translate.google.com/translate_a/t?client=t&text=hus&langpair=sv%7Cen

the NSLog also shows the correct result:

2013-03-01 00:07:14.681 testGoogTrans[941:11303] [[["house","hus","",""]]

etc..

If I now call the url shown before in a browser, I get a file with the same result inside.

So this is working fine on both the app and the browser.

now, by changing only the word, that has a special char:

word = @"tåg"; // result should be "train"

and the rest remaining the same, I get a strange different result:

the printed url in the console:

http://translate.google.com/translate_a/t?client=t&text=t%C3%A5g&langpair=sv%7Cen which seems to be correctly escaped.

if I call this url in a browser, I get a file with the following content:

[[["trains","tåg","",""]],[["nom",["TRAIN","MARCH","ROPE","MARCHING","EXPEDITION","PROCESSION"]

etc... which is what I expect.

instead, in the app, the result shown in the console is:

[[["t\u00C3 \u00A5 g","t\u00C3\u00A5g","",""]],,"sv",,[["t\u00C3",[5],1,0,998,0,1,0]

etc...

i believe the url is correctly formatted, and I don't know why i get a different answer from within the app.

anyone knows something about this or can put me on the direction to solve that?

thanks in advance

kelly
  • 415
  • 2
  • 9
  • 24
pm200107
  • 303
  • 1
  • 11

1 Answers1

0

I think this line is the culprit:

result = [[NSString alloc] initWithData:myData encoding: NSASCIIStringEncoding];

and should be:

result = [[NSString alloc] initWithData:myData encoding: NSUTF8StringEncoding];

When I try your URL in Chrome under Linux, it downloads a file called t and:

$ file t
t: UTF-8 Unicode text, with very long lines, with no line terminators
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • thanks for your answer. both give the same result for the second call. when I try in any browser, the result is always good. it is a t.js file with the expected result (train). it is from within the app that the result is not "correct" or as expected. if you use NSUTF8StringEncoding for the translation of the word "hus", as the return is ASCII, the result gets (null) – pm200107 Mar 01 '13 at 13:04