AI 텍스트 번역
텍스트를 원문 언어에서 대상 언어로 번역하고, 번역 결과를 순차적으로 받습니다.
사전 요구 사항
AIBudsAISDK가 초기화되어 있고 등록된 서비스 제공자를 선택해야 합니다.- 서비스 제공자가
AITextTranslationServiceAPI를 지원해야 합니다. - 입력 텍스트와 대상 언어가 비어 있지 않아야 합니다.
- 언어 식별자는
zh-CN,en-US처럼 하이픈으로 연결된 형식을 사용합니다. 원문 언어를 빈 문자열로 지정하면 자동 감지를 사용합니다.
AI를 활용해 구현
AI로 구현
AI로 이 워크플로 구현
공식 “AIBuds 텍스트 번역 구현” 스킬을 사용해 앱에 맞게 구현하세요.
https://docs-aibuds.github.io/ko/skills/implement-aibuds-text-translation을 읽고 지침을 따르세요. 이 스킬로 “AIBuds 텍스트 번역 구현”을 이 iOS 프로젝트에 구현하고 검증하세요.API Reference
프레임워크
AIBudsAI.xcframework
가져오기
- Swift
- Objective-C
import AIBudsAI#import <AIBudsAI/AIBudsAI-Swift.h>선언
- 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;translateText 및 cancelTextTranslation을 참고하세요.
사용 예제
- 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;
});
}];참고
- 원문 언어와 대상 언어를 같은 값으로 지정하지 마세요. 자동 감지가 필요하면 원문 언어에 빈 문자열을 사용합니다.
- transcript는 순차적으로 갱신되므로 이전 표시 결과를 교체할 수 있습니다.
- 이 API는 텍스트만 번역합니다. 연속 음성 번역에는 동시통역 기능을 사용하세요.
cancelTextTranslation()는 완료 콜백을 제공하지 않습니다. 취소를 요청할 때 로컬 상태를 함께 갱신하세요.