translateBulkToSingleTarget method
Future<List<String> >
translateBulkToSingleTarget(
- List<
String> sources, - LanguageCode sourceLanguage,
- LanguageCode target
override
Translates given texts to specified language
sources - list of text which should be translated
sourceLanguage - the language in which sources were given
target - language to which sources should be translated
Implementation
@override
Future<List<String>> translateBulkToSingleTarget(
List<String> sources,
LanguageCode sourceLanguage,
LanguageCode target,
) async {
logger.info(
'Translate bulk "$sources" from $sourceLanguage to single $target');
final prompt = '''
Translate the following texts from $sourceLanguage to $target.
Return only the translations, in the same order as the texts listed, one per line.
Do not include the original texts, language names, colons, or any extra text—just the translations.
Texts:
${sources.join('\n')}
''';
final apiResult = await _queryCompletions(prompt, 2000);
if (!apiResult.succeeded) {
logger.warning('Translation failed');
return sources;
}
if (apiResult.valueUnsafe.choices.isEmpty) {
logger.warning('Choices list is empty for prompt: $prompt');
return sources;
}
final translatedText = apiResult.valueUnsafe.choices.first.message.content;
final lines = _splitResponseIntoLines(translatedText);
if (lines.length != sources.length) {
logger.warning(
'Expected ${sources.length} translations, got ${lines.length}');
}
return List.generate(
sources.length,
(i) => i < lines.length ? lines[i] : sources[i],
);
}