Question

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.

Was it helpful?

Solution

"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.

OTHER TIPS

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top