This is a guide that helps you get started with the SDL iOS SDK so that you can add Translation capabilities to your iOS app. This guide includes a short tutorial of a sample app that makes use of the SDK.
- [Sign Up Here] (https://languagecloud.sdl.com/translation-api/sign-up) to get a Free API Key
- The SDK is available as a cocoapod and can be installed as follows:
platform :ios, '7.0'
pod "SDL-iOS-SDK", "~> 0.1.0"
#import "SDL.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[SDL languageCloud] setup:@"<your api key>"]
...
}
[[SDL languageCloud] translateText:@"Good Morning" from:[NSLocale localeWithLocaleIdentifier:@"en"] to:[NSLocale localeWithLocaleIdentifier:@"fr"] success:^(NSString* translation)
{
NSLog(@"Successful Translation: %@", translation);
}
failure:^(NSString* errorMessage)
{
NSLog(@"Error: %@", errorMessage);
}
];
- [Download the project] (https://github.com/dancali/sdl-ios-sampleapp/archive/master.zip) as a ZIP file
- Open the SDL-iOS-SampleApp.xcworkspace XCode Workspace file
- You should now see the project loaded in XCode:
- If you run the project you should immediately get an error:
- The app is pretty simple
- All it does is ask the user for input in English and it will translate the input the French
- The first thing you will see when running the app is a popup that will ask you for input:
- The translation call happens in the performTranslation method in SDLSampleApp.com
- If you look closely, it's one single asynchronous call:
- (void) performTranslation: (NSString*) text
{
//Show some progress
[_spinner startAnimating];
[[SDL languageCloud] translateText:text from:[NSLocale localeWithLocaleIdentifier:@"en"] to:[NSLocale localeWithLocaleIdentifier:@"fr"] success:^(NSString* translation)
{
//Stop the progress first
[_spinner stopAnimating];
//Great, we got something, let's show it
[self showTranslation:translation];
}
failure:^(NSString* errorMessage)
{
//Stop the progress first
[_spinner stopAnimating];
//Something went wrong, let's show the user what happened
[self showTranslation:[NSString stringWithFormat:@"Error: %@", errorMessage]];
}];
}
- Aside from the progress handling, the call is simple and straight-forward
- It passes in the text input we collected from the user and it specifies the language pair
- The from/to languages are specified as native NSLocale's
- And finally, the success block returns the translation and the failure block comes back with an error if something went wrong
The SDL iOS SDK is made available under the MIT license. Pleace see the LICENSE file for more details.