azure_translation 0.1.0 azure_translation: ^0.1.0 copied to clipboard
A wrapper for interacting with Azure's translation API.
A wrapper for interacting with Azure's translation API. #
Currently supports:
- Listing languages
- Translation
Coming soon:
- Transliteration
- Language detection
Listing Languages #
Listing languages is simple:
import 'package:azure_translation/azure_translation.dart' as at;
// ...
final langs = (await at.languages()).unwrap();
print(langs.translation?.take(4).join('\n'));
// Language(af, Afrikaans, Afrikaans, ltr)
// Language(am, Amharic, አማርኛ, ltr)
// Language(ar, Arabic, العربية, rtl)
// Language(as, Assamese, অসমীয়া, ltr)
The language list contains lists of translation
, transliteration
and dictionary
languages.
It is possible to only request one or two of these scopes by passing the optional scopes
parameter:
listLanguages(scopes: [LanguageScope.translation]);
There is also an optional baseLanguage
parameter, which sets the Accept-Language
header.
final langs = (await at.languages(baseLanguage: 'fr')).unwrap();
print(langs.translationLanguage('en'));
// Language(en, Anglais, English, ltr)
Translation #
Docs coming soon but this is self explanatory for now:
import 'package:azure_translation/azure_translation.dart' as at;
final res = await at.translate(
['hello world', 'good morning'],
baseLanguage: 'en', // optional
languages: ['fr', 'vi', 'ar'],
key: 'YOUR_AZURE_KEY',
region: 'YOUR_AZURE_REGION',
);
print(res.object!.join('\n'));
// TranslationResult(hello world, [fr: Salut tout le monde, vi: Chào thế giới, ar: مرحبا بالعالم])
// TranslationResult(good morning, [fr: Bonjour, vi: Xin chào, ar: صباح الخير])
Error handling #
Error handling in this package is all done using the result class pattern. There are no exceptions unless something goes wrong with HTTP (e.g. you have no connection). Specifically, it uses the result class from the elegant package.
Like so:
final langs = await languages();
final Result<LanguageList, AzureTranslationError> res = await languages();
if (res.ok) {
final LanguageList languageList = res.object!;
print('Success! Language list: $languageList');
} else {
final AzureTranslationError error = res.error!;
print('Error! $error');
}