Skip to content

Ai Ai Text Translation

Mr.P edited this page Aug 25, 2026 · 2 revisions

AI Text Translation

Translate a text value from a source language to a target language and receive the translated text incrementally.

Prerequisites

  • AIBudsAISDK is 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-CN and en-US; an empty source language enables automatic detection.

API Reference

Framework

AIBudsAI.xcframework

Import

Swift

import AIBudsAI

Objective-C

#import <AIBudsAI/AIBudsAI-Swift.h>

Declaration

Swift

/// 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.
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.
static func cancelTextTranslation()

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: `YES` when translation is complete.
///     - transcript: The current incremental translated text.
///     - error: The translation error, or `nil` on success.
+ (void)translateText:(NSString *)text
        from:(NSString *)sourceLanguage
        to:(NSString *)targetLanguage
        streamResultHandler:(void (^ _Nullable)(BOOL isFinal, NSString * _Nullable transcript, NSError * _Nullable error))streamResultHandler;

/// Cancels the current text translation operation.
+ (void)cancelTextTranslation;

See translateText and cancelTextTranslation.

Usage Examples

Swift

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
    }
}

Objective-C

[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.

AIBuds SDK iOS Wiki

Clone this wiki locally