{"id":403,"date":"2011-07-03T20:50:00","date_gmt":"2011-07-03T20:50:00","guid":{"rendered":"https:\/\/blog.alwawee.ru\/?p=403"},"modified":"2011-07-06T13:13:15","modified_gmt":"2011-07-06T13:13:15","slug":"objective-c-wrap-over-microsoft-bing-translate-api","status":"publish","type":"post","link":"https:\/\/blog.alwawee.ru\/?p=403","title":{"rendered":"Objective C wrap over Microsoft Translator V2 API"},"content":{"rendered":"<p>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. <\/p>\n<p>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&#8217;t use any JSON or XML parsing. <\/p>\n<p>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.<\/p>\n<p>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&#8217;s all. <\/p>\n<p>Please post any comments if this was useful.  <\/p>\n<p><code><br \/>\n#define URL_STRING @\"http:\/\/api.microsofttranslator.com\/v2\/Http.svc\/Translate?appId=here your own app id&text=\"<\/p>\n<p>- (IBAction) translateBing:(id)sender<br \/>\n{<br \/>\n\tSimpVocAppDelegate *appDelegate = (SimpVocAppDelegate *)[[UIApplication sharedApplication] delegate];<br \/>\n\t\/\/ \u0424\u043b\u0430\u0433 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f Translate<br \/>\n\tappDelegate.translateActive = YES;<\/p>\n<p>\t\/\/ \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f<br \/>\n\tif (appDelegate.internetActive == NO){<br \/>\n\t\tUIAlertView *alert = [[UIAlertView alloc]<br \/>\n\t\t\t\t\t\t\t  initWithTitle:@\"Internet Connection problem\"<br \/>\n\t\t\t\t\t\t\t  message:@\"There is no Internet Connection. Please, type translation yourself or leave blank\"<br \/>\n\t\t\t\t\t\t\t  delegate:self cancelButtonTitle:@\"Ok\"<br \/>\n\t\t\t\t\t\t\t  otherButtonTitles:nil];<br \/>\n\t\t[alert show];<br \/>\n\t\t\/\/ \u0424\u043b\u0430\u0433 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f Translate<br \/>\n\t\tappDelegate.translateActive = NO;<br \/>\n\t\treturn;<br \/>\n\t}<br \/>\n\t\/\/ \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0441\u0442\u0438 Bing Translate<br \/>\n\tif (appDelegate.hostActive == NO){<br \/>\n\t\tUIAlertView *alert = [[UIAlertView alloc]<br \/>\n\t\t\t\t\t\t\t  initWithTitle:@\"Translate Service problem\"<br \/>\n\t\t\t\t\t\t\t  message:@\"Translation Service is not available. Please, type translation yourself or leave blank\"<br \/>\n\t\t\t\t\t\t\t  delegate:self cancelButtonTitle:@\"Ok\"<br \/>\n\t\t\t\t\t\t\t  otherButtonTitles:nil];<br \/>\n\t\t[alert show];<br \/>\n\t\t\/\/ \u0424\u043b\u0430\u0433 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f Translate<br \/>\n\t\tappDelegate.translateActive = NO;<br \/>\n\t\treturn;<br \/>\n\t}<\/p>\n<p>\t[self performSelectorInBackground:@selector(performLongTaskInBackground) withObject:nil];<br \/>\n}<\/p>\n<p>- (void) performLongTaskInBackgroundBing<br \/>\n{<br \/>\n\t\/\/ Set up a pool for the background task.<br \/>\n    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];<\/p>\n<p>\tSimpVocAppDelegate *appDelegate = (SimpVocAppDelegate *)[[UIApplication sharedApplication] delegate];<br \/>\n\tNSString *tText = wordTextField.text;<\/p>\n<p>\tif ([tText isEqualToString:@\"\"]==NO){<br \/>\n\t\tNSString *translation;<br \/>\n\t\tNSString *fromL;<br \/>\n\t\tNSString *toL;<\/p>\n<p>\t\tfromL = [appDelegate.lang objectForKey:appDelegate.languageFrom];<br \/>\n\t\ttoL = [appDelegate.lang objectForKey:appDelegate.languageTo];<\/p>\n<p>\t\tNSMutableString* urlString = [NSMutableString string];<br \/>\n\t\t[urlString appendString: URL_STRING];<br \/>\n\t\t[urlString appendString: self.wordTextField.text];<br \/>\n\t\t[urlString appendString:@\"&from=\"];<br \/>\n\t\t[urlString appendString: fromL];<br \/>\n\t\t[urlString appendString:@\"&to=\"];<br \/>\n\t\t[urlString appendString: toL];<\/p>\n<p>\t\tNSURL* url = [NSURL URLWithString: urlString];<br \/>\n\t\tNSURLRequest* request = [NSURLRequest requestWithURL: url cachePolicy: NSURLRequestReloadIgnoringCacheData timeoutInterval: 60.0];<br \/>\n\t\tNSURLResponse* response; NSError* error;<br \/>\n\t\tNSData* data = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];<br \/>\n\t\tif (data == nil) {<br \/>\n\t\t\t\/\/NSLog(@\"Could not connect to the server: %@ %@\", urlString, [error description]);<br \/>\n\t\t\treturn nil;<br \/>\n\t\t} else {<br \/>\n\t\t\tNSString* contents = [[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease];<br \/>\n\t\t\t\/\/NSLog(@\"# %@ %@ %@\", fromL, toL, contents);<br \/>\n\t\t\t\/\/return [self translateCharacters: [[[contents JSONValue] objectForKey: @\"responseData\"] objectForKey: @\"translatedText\"]];<\/p>\n<p>\t\t\tNSRange match;<br \/>\n\t\t\tmatch = [contents rangeOfString: @\">\"];<br \/>\n\t\t\tcontents = [contents substringFromIndex: match.location+1];<\/p>\n<p>\t\t\tmatch = [contents rangeOfString: @\"<\"];\n\t\t\ttranslation = [contents substringWithRange: NSMakeRange (0, match.location)];\n\t\t\t\/\/NSLog(@\"%@\", contents); \n\t\t\ttranslationTextField.text = translation;\n\t\t\t\/\/ \u0424\u043b\u0430\u0433 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f Translate\n\t\t\tappDelegate.translateActive = NO;\n\t\t}\n\t    [self performSelectorOnMainThread:@selector(completeLongRunningTask) withObject:nil waitUntilDone:YES];\n\t\t[pool release];\t\n\t}\n}\n<\/code><\/p>\n<p>Here you can get some information about Bing Translate API : <\/p>\n<p><a href=\"http:\/\/www.microsofttranslator.com\/dev\/\">http:\/\/www.microsofttranslator.com\/dev\/<\/a><br \/>\n<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ff512423.aspx\">http:\/\/msdn.microsoft.com\/en-us\/library\/ff512423.aspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-403","post","type-post","status-publish","format-standard","hentry","category-android"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Objective C wrap over Microsoft Translator V2 API - Denis Kutlubaev<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.alwawee.ru\/?p=403\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Objective C wrap over Microsoft Translator V2 API - Denis Kutlubaev\" \/>\n<meta property=\"og:description\" content=\"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 [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.alwawee.ru\/?p=403\" \/>\n<meta property=\"og:site_name\" content=\"Denis Kutlubaev\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/alwaweemobile\" \/>\n<meta property=\"article:published_time\" content=\"2011-07-03T20:50:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2011-07-06T13:13:15+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/blog.alwawee.com\/?attachment_id=1982\" \/>\n<meta name=\"author\" content=\"Denis Kutlubaev\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Denis Kutlubaev\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=403#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=403\"},\"author\":{\"name\":\"Denis Kutlubaev\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"headline\":\"Objective C wrap over Microsoft Translator V2 API\",\"datePublished\":\"2011-07-03T20:50:00+00:00\",\"dateModified\":\"2011-07-06T13:13:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=403\"},\"wordCount\":199,\"commentCount\":0,\"articleSection\":[\"Android\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=403#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=403\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=403\",\"name\":\"Objective C wrap over Microsoft Translator V2 API - Denis Kutlubaev\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\"},\"datePublished\":\"2011-07-03T20:50:00+00:00\",\"dateModified\":\"2011-07-06T13:13:15+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=403#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=403\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=403#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.alwawee.ru\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Objective C wrap over Microsoft Translator V2 API\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/\",\"name\":\"Denis Kutlubaev\",\"description\":\"iOS Development\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blog.alwawee.ru\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\",\"name\":\"Denis Kutlubaev\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"caption\":\"Denis Kutlubaev\"},\"description\":\"iOS Developer, creator of Tornado Browser and many other popular apps\",\"sameAs\":[\"http:\\\/\\\/dennis.alwawee.com\"],\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Objective C wrap over Microsoft Translator V2 API - Denis Kutlubaev","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.alwawee.ru\/?p=403","og_locale":"en_US","og_type":"article","og_title":"Objective C wrap over Microsoft Translator V2 API - Denis Kutlubaev","og_description":"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 [&hellip;]","og_url":"https:\/\/blog.alwawee.ru\/?p=403","og_site_name":"Denis Kutlubaev","article_publisher":"https:\/\/www.facebook.com\/alwaweemobile","article_published_time":"2011-07-03T20:50:00+00:00","article_modified_time":"2011-07-06T13:13:15+00:00","og_image":[{"url":"http:\/\/blog.alwawee.com\/?attachment_id=1982","type":"","width":"","height":""}],"author":"Denis Kutlubaev","twitter_misc":{"Written by":"Denis Kutlubaev","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.alwawee.ru\/?p=403#article","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/?p=403"},"author":{"name":"Denis Kutlubaev","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"headline":"Objective C wrap over Microsoft Translator V2 API","datePublished":"2011-07-03T20:50:00+00:00","dateModified":"2011-07-06T13:13:15+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.alwawee.ru\/?p=403"},"wordCount":199,"commentCount":0,"articleSection":["Android"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.alwawee.ru\/?p=403#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.alwawee.ru\/?p=403","url":"https:\/\/blog.alwawee.ru\/?p=403","name":"Objective C wrap over Microsoft Translator V2 API - Denis Kutlubaev","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/#website"},"datePublished":"2011-07-03T20:50:00+00:00","dateModified":"2011-07-06T13:13:15+00:00","author":{"@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"breadcrumb":{"@id":"https:\/\/blog.alwawee.ru\/?p=403#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.alwawee.ru\/?p=403"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.alwawee.ru\/?p=403#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.alwawee.ru\/"},{"@type":"ListItem","position":2,"name":"Objective C wrap over Microsoft Translator V2 API"}]},{"@type":"WebSite","@id":"https:\/\/blog.alwawee.ru\/#website","url":"https:\/\/blog.alwawee.ru\/","name":"Denis Kutlubaev","description":"iOS Development","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.alwawee.ru\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7","name":"Denis Kutlubaev","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","caption":"Denis Kutlubaev"},"description":"iOS Developer, creator of Tornado Browser and many other popular apps","sameAs":["http:\/\/dennis.alwawee.com"],"url":"https:\/\/blog.alwawee.ru\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/403","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=403"}],"version-history":[{"count":0,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/403\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}