views:

1140

answers:

3

I am using google's translate API to translate some text in the iPhone SDK. The URL I use is http://ajax.googleapis.com/ajax/services/language/translate?v=1.0

Everything works, except in some languages, Polish for example, the returned string sometimes have weird things like \u0026 as part of the string. It would show as "hello how ar\u0026e you". Why is this? The way I fetch for the result is as follows:

    NSData *returnData = [NSURLConnection sendSynchronousRequest: req returningResponse: response error: error];
 NSString *new = [[NSString alloc] initWithData:responseData encoding: NSUTF8StringEncoding];

Is the encoding incorrect? Is that why the new string appears to be incorrect? I've tried the same text int he google translate webpage and it gets translated correctly.


UPDATE

If you simply go to this URL, you will see the special characters. How do I correctly represent those characters in my app? Using the two lines of code above will not encode it correctly.

A: 

The funny thing is that Unicode char-code 0026 happens to be the ampersand character which also is the parameter separator character on URL requests. This makes me wonder if there isn't something going on with the "req" string instead of the encoding.

May want to take a look at the responses to this SO question Iphone SDk : Problem with ampersand in the URL string and see if it applies to your situation.

Ramin
+3  A: 

the example looks correct:

'\u0026quot;liberation\u0026quot; and \u0026quot;exit application\u0026quot;'

i guess is jsonEncode(htmlEncode("liberation" and "exit application"))

if you have a proper json library then translatedText should be: '..."liberation" and "exit application"...' and then you just need to pass that through a html decoder.

\uXXXX escapes are just unicode hex escapes and should be handled by the json parser

drscroogemcduck
A: 

The response is a JSON string (read about JSON here). Try using a JSON parser on the resulted string, and then retrieve the "responseData" element from it. Using this framework, for example, your code would look like:

// Your original code
NSData *returnData = [NSURLConnection sendSynchronousRequest: req returningResponse: response error: error];
NSString *jsonResponse = [[NSString alloc] initWithData:responseData encoding: NSUTF8StringEncoding];
// JSON parsing
SBJsonParser *parser = [SBJsonParser new];
id jsonObject = [parser objectWithString:jsonResponse];
// the result supports key-value coding
NSString* translatedValue = [jsonObject valueForKey:@"responseData"];
Aviad Ben Dov