A simple iOS library for Google translation APIs.
ICTTranslation *translation = [[ICTTranslation alloc] initWithGoogleAPIKey:<GOOGLE_API_KEY>];
[translation translateText:@"Bonjour" completion:^(NSError * _Nullable error, NSString * _Nullable translated, NSString * _Nullable sourceLanguage) {
NSLog(@"error -> %@", error);
NSLog(@"translated -> %@", translated);
NSLog(@"sourceLanguage -> %@", sourceLanguage);
}];
let translation = Translation(googleAPIKey: <GOOGLE_API_KEY>)
translation.translate(text: "Bonjour") { (error, translated, sourceLanguage) in
print(error.debugDescription as Any)
print(translated as Any)
print(sourceLanguage as Any)
}
- Go to the Demo directory.
- Open the
.xcworkspace
(not the.xcodeproj
!) file. - Install Cocoapod
- Run the app.
CocoaPods is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries like Translation in your projects.
platform :ios, '8.0'
pod "Translation"
Alternatively you can directly add the Translation folder to your project. Translation uses AFNetworking - your project needs this for it to work if you include it this way. CocoaPods install manages this dependency for you.
#import <Translation/Translation.h>
Please import to the Swift Bridging Header file. Folow some steps to create this file.
#import <Translation/Translation.h>
ICTTranslation *translation = [[ICTTranslation alloc] initWithGoogleAPIKey:<GOOGLE_API_KEY>];
let translation = Translation(googleAPIKey: <GOOGLE_API_KEY>)
[translation translateText:@"Bonjour" completion:^(NSError * _Nullable error, NSString * _Nullable translated, NSString * _Nullable sourceLanguage) {
NSLog(@"error -> %@", error);
NSLog(@"translated -> %@", translated);
NSLog(@"sourceLanguage -> %@", sourceLanguage);
}];
translation.translate(text: "Bonjour") { (error, translated, sourceLanguage) in
print(error.debugDescription as Any)
print(translated as Any)
print(sourceLanguage as Any)
}
Note that translations are one-shot operations. You need to instantiate a new
Translation
object for each translation.
Detects the language and returns its ISO language code as the detectedSource
parameter.
If initialized with Google, the completion handler also returns a float between 0 and 1 indicating the confidence of the match, with 1 being the highest confidence. This is not supported with Bing translate and will always returns ICTTranslationUnknownConfidence
.
[detectLanguage detectLanguage:@"問題" completion:^(NSError * _Nullable error, NSString * _Nullable detectedSource, float confidence) {
NSLog(@"error -> %@", error);
NSLog(@"translated -> %@", detectedSource);
NSLog(@"sourceLanguage -> %f", confidence);
}];
detectLanguage.detectLanguage(text: "問題") { (error, detectLanguage, val) in
print(error.debugDescription as Any)
print(detectLanguage as Any)
print(val as Any)
}
Google supports different languages. You can get a list of supported ISO language codes with the following function:
[langs supportedLanguages:^(NSError * _Nonnull error, NSArray * _Nonnull languageCodes) {
NSLog(@"translated -> %@", languageCodes);
}];
langs.supportedLanguages { (error, langs) in
print(langs as Any)
}
The basic translation function makes a guess at the source language and specifies the target language based on the user's phone settings:
- (void)translateText:(NSString *)text
target:(nullable NSString *)target
completion:(ICTTranslationCompletionHandler)completion;
func translate(text: String, _ completion: (Error?, String?, String?)-> Void) -> Void
You can specify the source and/or the target languages if desired:
- (void)translateText:(NSString *)text
withSource:(nullable NSString *)source
target:(nullable NSString *)target
completion:(ICTTranslationCompletionHandler)completion;
func translate(text: String, source: String, target: String, _ completion: (Error?, String?, String?)-> Void) -> Void
Usually you don't know the source language to translate from. Going by user's iPhone locale or keyboard language settings seems like the obvious answer, but it is unreliable: there's nothing stopping you from typing Hola amigo! with an English keyboard. This is common, especially with international users.
For this reason Translation will ignore the passed-in source
parameter in the above function, if it determines a good guess can be made. Typically this means that the text
parameter is complex and long enough for the engine to reliably determine the language. Short string snippets will typically respect the passed-in source
parameter, if any.
To force Translation to always respect the source
parameter, use the following property:
translation.preferSourceGuess = false;
translation.preferSourceGuess = false
Note: Unless you definitely know the source language, I recommend leaving smart guessing on AND passing the source parameter if available as a hint to the language detector.
For Google Translate, you can throttle usage on a per-user/device basis by setting a specific user identifier property in the Translation
instance. See the specific Google documentation for more information.
@property (nonatomic, nullable) NSString *quotaUser;
var quotaUser: String?
- (void)cancel;
func cancel() -> Void
Have a question or an issue about Translation? Create an issue!
Translation uses the following projects:
- PINCache
- AFNetworking
- I also cribbed some parts of the README. Great job explaining CocoaPods usage guys!
Translation is available under the MIT license. See the LICENSE file for more info.
Inspired by FGTranslator 👈 💯