translate method
Future<String>
translate(
- String source,
- LanguageCode sourceLanguage,
- LanguageCode target
override
Translates given text to specified language
source
- text which should be translated
sourceLanguage
- the language in which source
was given
target
- language to which source
should be translated
Implementation
@override
Future<String> translate(
String source,
LanguageCode sourceLanguage,
LanguageCode target,
) async {
logger.info('Translate "$source" from $sourceLanguage to $target');
final apiResult = await _httpClient.post<_GoogleCloudTranslation>(
path: 'v3/projects/$projectId:translateText',
decoder: (response) => _decodeTranslationJson(
_htmlUnescape.convert(utf8.decode(response.bodyBytes)),
),
body: {
'sourceLanguageCode': sourceLanguage,
'targetLanguageCode': target,
'contents': [
source,
]
},
);
if (!apiResult.succeeded) {
logger.warning('Translation failed');
return source;
}
return apiResult.valueUnsafe.translations.first.translatedText;
}