Skip to content

RWS/sdl-ios-sampleapp

Repository files navigation

SDL iOS SDK Sample App

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.

Adding the SDK to your app in 3 Easy Steps

STEP 1. Get Your SDL Language Cloud API Key

STEP 2. Install the SDK

  • The SDK is available as a cocoapod and can be installed as follows:
platform :ios, '7.0'
pod "SDL-iOS-SDK", "~> 0.1.0"

STEP 3. Import the SDK in your code

#import "SDL.h"

Using the SDL Translation API

Setup your API Key once

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   [[SDL languageCloud] setup:@"<your api key>"]
   ...
}

Performing a text translation

 [[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);
    }
];

Sample App Tutorial

Download and open the project

SDL

Setup your API key

  • If you run the project you should immediately get an error:

SDL

- Remove error line: ```ruby #error @"Setup your SDL Language Cloud API Key" ``` - And enter your valid API key: ```ruby [[SDL languageCloud] setup:@""]; ``` - Then run the project again - Everything should build perfectly and the simulator should launch

Perform a translation

  • 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:

SDL

- If you type something and press the Translate button, the app will perform the translation and popup the result:

SDL

- That's all there is to it

Under The Hood

  • 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

License

The SDL iOS SDK is made available under the MIT license. Pleace see the LICENSE file for more details.

Releases

No releases published

Packages

No packages published