AI Text Translation
Translate a text value from a source language to a target language and receive the translated text incrementally.
Prerequisites
AIBudsAISDKis initialized and a registered provider is selected.- The provider supports
AITextTranslationServiceAPI. - The input text and target language are non-empty.
- Language identifiers use a hyphenated form such as
zh-CNanden-US; an empty source language enables automatic detection.
Implement with AI Assistance
Build with AI
Implement this workflow with AI
Use the official Implement AIBuds Text Translation skill to adapt this workflow to your app.
Read and follow https://docs-aibuds.github.io/skills/implement-aibuds-text-translation. Use it to implement Implement AIBuds Text Translation in this iOS project and verify the result.API Reference
Framework
AIBudsAI.xcframework
Import
- Swift
- Objective-C
import AIBudsAI#import <AIBudsAI/AIBudsAI-Swift.h>Declaration
- Swift
- Objective-C
/// Translates text from one language to another.
/// - Parameters:
/// - text: The text to translate.
/// - sourceLanguage: Source language code. An empty string enables auto-detect.
/// - targetLanguage: Target language code.
/// - streamResultHandler: Called repeatedly with translation results.
/// - isFinal: `true` when translation is complete.
/// - transcript: The current incremental translated text.
/// - error: The translation error, or `nil` on success.
public static func translateText(_ text: String,
from sourceLanguage: String,
to targetLanguage: String,
streamResultHandler: ((_ isFinal: Bool, _ transcript: String?, _ error: Error?) -> Void)? = nil)
/// Cancels the current text translation operation.
public static func cancelTextTranslation()/// Translates text from one language to another.
/// - Parameters:
/// - text: The text to translate.
/// - sourceLanguage: Source language code. An empty string enables auto-detect.
/// - targetLanguage: Target language code.
/// - streamResultHandler: Called repeatedly with translation results.
/// - isFinal: `YES` when translation is complete.
/// - transcript: The current incremental translated text.
/// - error: The translation error, or `nil` on success.
+ (void)translateText:(NSString * _Nonnull)text
from:(NSString * _Nonnull)sourceLanguage
to:(NSString * _Nonnull)targetLanguage
streamResultHandler:(void (^ _Nullable)(BOOL, NSString * _Nullable, NSError * _Nullable))streamResultHandler;
/// Cancels the current text translation operation.
+ (void)cancelTextTranslation;See translateText and cancelTextTranslation.
Usage Examples
- Swift
- Objective-C
AIBudsAISDK.translateText(
"你好,欢迎使用 AIBuds SDK。",
from: "zh-CN",
to: "en-US"
) { isFinal, transcript, error in
DispatchQueue.main.async {
if let error {
translationLabel.text = error.localizedDescription
return
}
if let transcript {
translationLabel.text = transcript
}
translateButton.isEnabled = isFinal
}
}[AIBudsAISDK translateText:@"你好,欢迎使用 AIBuds SDK。"
from:@"zh-CN"
to:@"en-US"
streamResultHandler:^(BOOL isFinal, NSString *transcript, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error != nil) {
self.translationLabel.text = error.localizedDescription;
return;
}
if (transcript != nil) {
self.translationLabel.text = transcript;
}
self.translateButton.enabled = isFinal;
});
}];Notes
- Do not use the same explicit source and target language. Use an empty source string when auto-detection is required.
- The transcript is incremental and can replace the previous displayed result.
- This API translates text only. For continuous spoken translation, use Simultaneous Interpretation.
cancelTextTranslation()has no completion callback; update local state when cancellation is requested.