Skip to main content

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.

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.
View official skill

API Reference

Framework

AIBudsAI.xcframework

Import

Swift
import AIBudsAI

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.
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()

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

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.