We all know, that Google has stopped Google Translate Service for applications and made Google Translate API depricated. So I decided to use Microsoft Bing Translate API for my purposes of online word translation.
I managed to make an objective C wrap over Microsoft Bing Translate API for translation of one word. It is analogous to the wrap for Google Translate API, but it is much simpler because it doesn’t use any JSON or XML parsing.
I just extract a translated word from a string that I get by HTTP GET Method in Obj. C. In the first function I check weather I have an Internet connection setup properly and I start running a background function for translation. I make it in background thread not to freeze user interface.
Then I produce a url and make a request. When I get a contents string, I cut it a little bit to get a translation of word. That’s all.
Please post any comments if this was useful.
#define URL_STRING @"http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=here your own app id&text="
- (IBAction) translateBing:(id)sender
{
SimpVocAppDelegate *appDelegate = (SimpVocAppDelegate *)[[UIApplication sharedApplication] delegate];
// Флаг состояния Translate
appDelegate.translateActive = YES;
// Проверка соединения
if (appDelegate.internetActive == NO){
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Internet Connection problem"
message:@"There is no Internet Connection. Please, type translation yourself or leave blank"
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
// Флаг состояния Translate
appDelegate.translateActive = NO;
return;
}
// Проверка доступности Bing Translate
if (appDelegate.hostActive == NO){
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Translate Service problem"
message:@"Translation Service is not available. Please, type translation yourself or leave blank"
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
// Флаг состояния Translate
appDelegate.translateActive = NO;
return;
}
[self performSelectorInBackground:@selector(performLongTaskInBackground) withObject:nil];
}
- (void) performLongTaskInBackgroundBing
{
// Set up a pool for the background task.
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
SimpVocAppDelegate *appDelegate = (SimpVocAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *tText = wordTextField.text;
if ([tText isEqualToString:@""]==NO){
NSString *translation;
NSString *fromL;
NSString *toL;
fromL = [appDelegate.lang objectForKey:appDelegate.languageFrom];
toL = [appDelegate.lang objectForKey:appDelegate.languageTo];
NSMutableString* urlString = [NSMutableString string];
[urlString appendString: URL_STRING];
[urlString appendString: self.wordTextField.text];
[urlString appendString:@"&from="];
[urlString appendString: fromL];
[urlString appendString:@"&to="];
[urlString appendString: toL];
NSURL* url = [NSURL URLWithString: urlString];
NSURLRequest* request = [NSURLRequest requestWithURL: url cachePolicy: NSURLRequestReloadIgnoringCacheData timeoutInterval: 60.0];
NSURLResponse* response; NSError* error;
NSData* data = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];
if (data == nil) {
//NSLog(@"Could not connect to the server: %@ %@", urlString, [error description]);
return nil;
} else {
NSString* contents = [[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease];
//NSLog(@"# %@ %@ %@", fromL, toL, contents);
//return [self translateCharacters: [[[contents JSONValue] objectForKey: @"responseData"] objectForKey: @"translatedText"]];
NSRange match;
match = [contents rangeOfString: @">"];
contents = [contents substringFromIndex: match.location+1];
match = [contents rangeOfString: @"<"];
translation = [contents substringWithRange: NSMakeRange (0, match.location)];
//NSLog(@"%@", contents);
translationTextField.text = translation;
// Флаг состояния Translate
appDelegate.translateActive = NO;
}
[self performSelectorOnMainThread:@selector(completeLongRunningTask) withObject:nil waitUntilDone:YES];
[pool release];
}
}
Here you can get some information about Bing Translate API :
http://www.microsofttranslator.com/dev/
http://msdn.microsoft.com/en-us/library/ff512423.aspx