Translation
一个简单的为 Google 翻译 API 定制的 iOS 库。
快速入门 (Google)
Objective-C
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);
}];
Swift
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)
}
演示
- 转到演示目录。
- 打开
.xcworkspace
(非.xcodeproj
!)文件。 - 安装 Cocoapod
- 运行应用。
将翻译添加到您的项目中
CocoaPods
CocoaPods 是 Objective-C 的依赖管理工具,它自动化并简化了在项目中使用第三方库(如 Translation)的过程。
platform :ios, '8.0'
pod "Translation"
源文件
或者,您可以将 Translation 文件夹直接添加到您的项目中。Translation 使用 AFNetworking - 如果您以这种方式包含它,则您的项目需要这个库才能工作。CocoaPods 安装会为您管理这个依赖项。
使用
导入到您的项目中
Objective-C
#import <Translation/Translation.h>
Swift
以下步骤创建此文件。
请将导入到 Swift 桥接头文件,请按照#import <Translation/Translation.h>
使用 Google 进行初始化...
Objective-C
ICTTranslation *translation = [[ICTTranslation alloc] initWithGoogleAPIKey:<GOOGLE_API_KEY>];
Swift
let translation = Translation(googleAPIKey: <GOOGLE_API_KEY>)
翻译
Objective-C
[translation translateText:@"Bonjour" completion:^(NSError * _Nullable error, NSString * _Nullable translated, NSString * _Nullable sourceLanguage) {
NSLog(@"error -> %@", error);
NSLog(@"translated -> %@", translated);
NSLog(@"sourceLanguage -> %@", sourceLanguage);
}];
Swift
translation.translate(text: "Bonjour") { (error, translated, sourceLanguage) in
print(error.debugDescription as Any)
print(translated as Any)
print(sourceLanguage as Any)
}
请注意,翻译是一次性操作。您需要为每次翻译实例化一个新的
Translation
对象。
检测语言
检测语言并返回其ISO语言代码作为detectedSource
参数。
如果初始化为Google,完成处理程序还会返回一个介于0和1之间的浮点数,表示匹配的置信度,1表示最高的置信度。这不支持Bing翻译,始终返回ICTTranslationUnknownConfidence
。
Objective-C
[detectLanguage detectLanguage:@"問題" completion:^(NSError * _Nullable error, NSString * _Nullable detectedSource, float confidence) {
NSLog(@"error -> %@", error);
NSLog(@"translated -> %@", detectedSource);
NSLog(@"sourceLanguage -> %f", confidence);
}];
Swift
detectLanguage.detectLanguage(text: "問題") { (error, detectLanguage, val) in
print(error.debugDescription as Any)
print(detectLanguage as Any)
print(val as Any)
}
获取支持的列表
Google支持不同的语言。您可以使用以下函数获取支持的语言的ISO语言代码列表。
Objective-C
[langs supportedLanguages:^(NSError * _Nonnull error, NSArray * _Nonnull languageCodes) {
NSLog(@"translated -> %@", languageCodes);
}];
Swift
langs.supportedLanguages { (error, langs) in
print(langs as Any)
}
花哨功能
指定源语言或目标语言
基本翻译功能会猜测源语言,并根据用户的手机设置指定目标语言
Objective-C
- (void)translateText:(NSString *)text
target:(nullable NSString *)target
completion:(ICTTranslationCompletionHandler)completion;
Swift
func translate(text: String, _ completion: (Error?, String?, String?)-> Void) -> Void
如果需要,可以指定源语言和/或目标语言
Objective-C
- (void)translateText:(NSString *)text
withSource:(nullable NSString *)source
target:(nullable NSString *)target
completion:(ICTTranslationCompletionHandler)completion;
Swift
func translate(text: String, source: String, target: String, _ completion: (Error?, String?, String?)-> Void) -> Void
禁用智能猜测
通常情况下,您不知道要将内容翻译成源语言是什么。按照用户的iPhone地区设置或键盘语言设置来选择似乎是一个明显的解决方案,但这并不可靠:没有限制您可以不使用英语键盘输入《Hola amigo!》。这在国际用户中很常见。
因此,如果翻译确定了可以做出良好的猜测,它将忽略上述函数中传入的source
参数。通常,这意味着text
参数足够复杂且足够长,以便引擎可以可靠地确定语言。通常情况下,简短的字符串片段会尊重传入的source
参数(如果有)。
要强制翻译始终尊重source
参数,请使用以下属性
Objective-C
translation.preferSourceGuess = false;
Swift
translation.preferSourceGuess = false
注意:除非您确实知道源语言,否则建议启用智能猜测功能,并如果有的话传递源参数,作为对语言检测器的提示。
用户限制(仅限Google)
对于Google Translate,您可以通过在Translation
实例中设置特定的用户标识属性来按用户/设备限制使用量。有关更多信息,请参阅具体的Google文档。
Objective-C
@property (nonatomic, nullable) NSString *quotaUser;
Swift
var quotaUser: String?
- (void)cancel;
func cancel() -> Void
对翻译有疑问或问题?创建问题!
翻译使用了以下项目
- PINCache
- AFNetworking
- 我也借鉴了README的一部分。说明CocoaPods的用法好棒呀!
翻译遵循MIT许可证。有关更多信息,请参阅LICENSE文件。
灵感
灵感来源于 FGTranslator