如何在iPhone应用程序中使用Google翻译API V2?

时间:2013-11-26 10:53:35

标签: ios iphone google-translate

您好我正在开发一个iPhone应用程序,我正在计划实现一些语言翻译功能。我已经购买了谷歌转换器API并获得了API密钥。 现在根据Google提供的教程,我刚刚使用ASIFormDataRequest对此URL“https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&source=en&target=de&q=Hello%20world”进行了HTTP调用。不幸的是,它最终出现了一个名为Access not configured的错误。我错过了什么步骤?请帮帮我?

    ASIFormDataRequest *translateRequest = [ASIFormDataRequest requestWithURL:[ValidateClass getURL:@"Conversions.php"]];
    [translateRequest setDelegate:self];
    [translateRequest  setRequestMethod:@"GET"];
    [translateRequest setDidFinishSelector:@selector(didFinishRequest:)];
    [translateRequest setDidFailSelector:@selector(didFailRequest:)];
    [translateRequest setShouldAttemptPersistentConnection:NO];
    [translateRequest setTimeOutSeconds:120];
    [translateRequest startAsynchronous];

1 个答案:

答案 0 :(得分:1)

只需获取您的Google密钥并将其插入FGTranslator即可。之后,它是一个单行翻译函数调用。

FGTranslator *translator = [[FGTranslator alloc] initWithGoogleAPIKey:@"your_google_key"];

[translator translateText:@"Bonjour!" 
               completion:^(NSError *error, NSString *translated, NSString *sourceLanguage)
{
    if (error)
        NSLog(@"translation failed with error: %@", error);
    else
        NSLog(@"translated from %@: %@", sourceLanguage, translated);
}];