A wrapper for interacting with Azure's translation API.

Currently supports:

  • Listing languages
  • Translation
  • Language detection
  • Breaking sentences

Coming soon:

  • Transliteration

Listing Languages

Azure Reference

Listing languages is simple:

import 'package:azure_translation/azure_translation.dart' as at;
// ...
final langs = (await at.languages()).unwrap();
// 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();
// Language(en, Anglais, English, ltr)


Azure Reference

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',
// 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: صباح الخير])


Azure Reference

final res = await transliterate(
    ['konnichiwa', 'arigato', 'sayounara'],
    key: key,
    region: region,
    language: 'ja',
    fromScript: 'Latn',
    toScript: 'Jpan',
// TransliterationResult(ja, Latn -> Jpan, {konnichiwa: こんにちわ, arigato: ありがと, sayounara: さようなら})TransliterationResult(ja, Latn -> Jpan, {konnichiwa: こんにちわ, arigato: ありがと, sayounara: さようなら})

Language Detection

Azure Reference

final res = await detect(
    ['bonjour', 'hola', 'здравейте'],
    key: key,
    region: region,
// DetectionResult(bonjour, fr, 1.0, true, false)
// DetectionResult(hola, es, 1.0, true, false)
// DetectionResult(здравейте, bg, 1.0, true, true)
// {fr: 1.0}

Breaking Sentences

Azure Reference

final res = await breakSentence(
        'How are you? I am fine. What did you do today?',
        '¿hola, cómo estás? ¿Donde está la biblioteca?',
    key: key,
    region: region,
// BrokenSentence([How are you? , I am fine. , What did you do today?])
// BrokenSentence([¿hola, cómo estás? , ¿Donde está la biblioteca?])

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');