Skip to main content

AI Translation (TSAITranslateInterface)

AI Translation provides provider-neutral streaming text translation. Partial callbacks carry cumulative text; completion carries the final result or error.

Prerequisites​

TSAIContext *context = [TSAIKit sharedInstance].activeContext;
if (![context supportsAIFeatures:TSAIFeatureTextTranslation]) {
return;
}
id<TSAITranslateInterface> translate = context.translate;

Data models​

TSAITranslateConfig​

PropertyTypeDescription
sourceLanguageTSAILanguageSource language; TSAILanguageAuto enables detection
targetLanguageTSAILanguageConcrete target language; Auto and Unknown are invalid
TSAITranslateConfig *config =
[TSAITranslateConfig configWithSourceLanguage:TSAILanguageAuto
targetLanguage:TSAILanguageEnglishUS];

Results​

ModelPropertiesDescription
TSAITranslatePartialResulttaskId, text, detectedSourceLanguage, isFinalCumulative partial translation
TSAITranslateResulttaskId, sourceText, translatedText, detectedSourceLanguageFinal translation

Callback types​

CallbackDescription
TSAITranslatePartialBlockInvoked zero or more times; text only grows
TSAITranslateCompletionBlockInvoked once on success, failure, or cancellation

Translate text​

- (NSString *)translateText:(NSString *)text
config:(TSAITranslateConfig *)config
onPartialResult:(nullable TSAITranslatePartialBlock)onPartialResult
completion:(nullable TSAITranslateCompletionBlock)completion;
NSString *taskId = [translate translateText:@"今天天气很好"
config:config
onPartialResult:^(TSAITranslatePartialResult *partial) {
TSLog(@"Translation: %@", partial.text);
}
completion:^(TSAITranslateResult *result, NSError *error) {
if (error) {
TSLog(@"Translation failed: %@", error.localizedDescription);
return;
}
TSLog(@"Final translation: %@", result.translatedText);
}];

Cancel translation​

- (void)cancelTranslationWithTaskId:(NSString *)taskId;

Cancellation completes the original task with a cancellation error. Completed and unknown task IDs are no-ops.

[translate cancelTranslationWithTaskId:taskId];

Language-code mapping​

All AI modules share TSAILanguage. Convert to or from BCP-47 when integrating with application configuration or services:

NSString *code = [TSAILanguageMapper bcp47CodeForLanguage:TSAILanguageEnglishUS];
TSAILanguage language = [TSAILanguageMapper languageForBcp47Code:@"en-US"];

See Common Types and Errors for the language list and Auto rules.

Important notes​

  1. Completion is the authoritative terminal signal, including error and cancellation paths.
  2. The trailing partial may have isFinal = YES; still wait for completion.
  3. TSAILanguageAuto is valid only as a source language.
  4. Unsupported languages return TSAIErrorCodeNotSupported; the SDK does not silently substitute a default.