0

I have an issue with my iPhone application or rather Xcode. I am currently trying to convert a string that i get from Google Translate to a string format that supports cyrillic letters. i tried many String Encodings so far, but none of them seem to work properly.

Here is my code so far:

NSString *word = @"hello";
NSString *urlPath = [NSString stringWithFormat:@"/translate_a/t?client=t&text=%@&langpair=en|ru",word];

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;
data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"%@", result);

And this is the result on the console:

[
    [
        [
            "ÐÒÉ×ÅÔ",
            "hello",
            "privet",
            ""
        ]
    ],
    [
        [
            "verb",
            [
                "ÚÄÏÒÏ×ÁÔØÓÑ",
                "Ú×ÁÔØ",
                "ÏËÌÉËÁÔØ"
            ],
            [
                [
                    "ÚÄÏÒÏ×ÁÔØÓÑ",
                    [
                        "greet",
                        "hello",
                        "salute",
                        "hullo",
                        "hallo",
                        "halloa"
                    ],
                    ,
                    0.0050860695
                ],
                [
                    "Ú×ÁÔØ",
                    [
                        "call",
                        "invite",
                        "shout",
                        "hail",
                        "hallo",
                        "hello"
                    ],
                    ,
                    2.7109536e-05
                ],
                [
                    "ÏËÌÉËÁÔØ",
                    [
                        "hail",
                        "holler",
                        "call",
                        "challenge",
                        "speak",
                        "hello"
                    ],
                    ,
                    2.7109536e-05
                ]
            ],
            "hello",
            2
        ],
        [
            "noun",
            [
                "ÐÒÉ×ÅÔÓÔ×ÉÅ",
                "ÐÒÉ×ÅÔÓÔ×ÅÎÎÙÊ ×ÏÚÇÌÁÓ",
                "×ÏÚÇÌÁÓ ÕÄÉ×ÌÅÎÉÑ"
            ],
            [
                [
                    "ÐÒÉ×ÅÔÓÔ×ÉÅ",
                    [
                        "greeting",
                        "welcome",
                        "salute",
                        "hail",
                        "salutation",
                        "hello"
                    ],
                    ,
                    0.0014801305
                ],
                [
                    "ÐÒÉ×ÅÔÓÔ×ÅÎÎÙÊ ×ÏÚÇÌÁÓ",
                    [
                        "hallo",
                        "halloa",
                        "viva",
                        "hello"
                    ],
                    ,
                    2.7536449e-05
                ],
                [
                    "×ÏÚÇÌÁÓ ÕÄÉ×ÌÅÎÉÑ",
                    [
                        "hallo",
                        "halloa",
                        "hello"
                    ],
                    ,
                    2.7109536e-05
                ]
            ],
            "hello",
            1
        ]
    ],
    "en",
    ,
    [
        [
            "ÐÒÉ×ÅÔ",
            [
                1
            ],
            true,
            false,
            1000,
            0,
            1,
            0
        ]
    ],
    [
        [
            "hello",
            1,
            [
                [
                    "ÐÒÉ×ÅÔ",
                    1000,
                    true,
                    false
                ],
                [
                    "úÄÒÁ×ÓÔ×ÕÊÔÅ",
                    0,
                    true,
                    false
                ],
                [
                    "Hello",
                    0,
                    true,
                    false
                ],
                [
                    "úÄÒÁ×ÓÔ×ÕÊ",
                    0,
                    true,
                    false
                ],
                [
                    "ÈÅÌÌÏ",
                    0,
                    true,
                    false
                ]
            ],
            [
                [
                    0,
                    5
                ]
            ],
            "hello"
        ]
    ],
    ,
    [
        ,
        "Hello!",
        [
            10
        ],
        ,
        true
    ],
    [

    ],
    2
]

So i hope you get my problem. I want the string to be "привет" and not "ÐÒÉ×ÅÔ". When i tried to encode the data to other formats i usually get a "(null)" result to my string, like for NSUTF8StringEncoding for example.

Mani
  • 17,549
  • 13
  • 79
  • 100
Gustl007
  • 245
  • 1
  • 2
  • 12

2 Answers2

2

"translate.google.com" returns UTF-8 with the following parameters:

NSString *urlPath = [NSString stringWithFormat:@"/translate_a/t?client=t&text=%@&langpair=en|ru&ie=UTF-8&oe=UTF-8",word];

Then

NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

should work.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thanks that worked just fine! But now i have another problem. I want to split the string up into an array with the "componentsSeparatedByString:" but then it converts the letters again into weird signs. "привет" is then "\U043f\U0440\U0438\U0432\U0435\U0442" for example. What should i do now? – Gustl007 Jan 18 '14 at 10:32
  • @Gustl007: That is only how the NSLog of an array prints non-ASCII characters, you need not worry about that. - But the response looks like JSON, so the proper way would be to use NSJSONSerialization (instead of componentsSeparatedByString). But the JSON seems to be invalid, so that does not work. – Martin R Jan 18 '14 at 10:38
  • That works! Thank you so much! And i don't really know much about JSON, but if you say it is invalid anyway i will not confuse myself with it right now. – Gustl007 Jan 18 '14 at 10:43
0

Call this method on the data you're getting instead of initWithData:encoding::

- (NSString*)decodeKOI8R:(NSData*)dataToDecode {
    CFStringRef aCFString = CFStringCreateWithCString(NULL, [dataToDecode bytes], kCFStringEncodingKOI8_R);
    NSString *decodedString = (__bridge NSString*)aCFString;
    return decodedString;
}
Stavash
  • 14,244
  • 5
  • 52
  • 80