user1771823
user1771823

Reputation: 139

google translate in Objective-C

I have seen some post which uses google translate web page.

NSString* englishString = [englishInputArray objectAtIndex:i];


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

    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(@"Text: %@",result);

I have two questions: 1)the json return from the web page look like this

[[["Bonjour","Hello","",""]],[["interjection",["bonjour","salut","all\u00f4","tiens"]]],"en",,[["Bonjour",[5],1,0,1000,0,1,0]],[["Hello",4,,,""],["Hello",5,[["Bonjour",1000,1,0]],[[0,5]],"Hello"]],,,[],1]

Other than doing string manipulation is there a way to get the the exact translation string alone ie in tis case "Bonjour" alone.

2: Does anybody know if this is this a free service ? Google apis seems to be a paid service. But if you use web page is that a free service.

Upvotes: 0

Views: 1451

Answers (1)

CStreel
CStreel

Reputation: 2642

  1. No. All API's I've used have always been either JSON or XML. There is no reason to use string manipulation when you can just parse the data into a readable structure

  2. If you are looking to use another service that isn't paid, keep in mind there are normally strict limitations. Try something like: SDL https://www.beglobal.com/developers/api-documentation/

Have you read Google's Translate API Documentation? https://developers.google.com/translate/

For example performing a GET request like so

GET https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&source=en&target=de&q=Hello%20world

Should return the following response:

{
    "data": {
        "translations": [
            {
                "translatedText": "Hallo Welt"
            }
        ]
    }
}

With this you can just parse the JSON and display the data

Upvotes: 2

Related Questions