getTranslation method

Future<Translation> getTranslation(
  1. String sourceText, {
  2. String from = autoLanguage,
  3. required String to,
})

Implementation

Future<Translation> getTranslation(
  String sourceText, {
  String from = autoLanguage,
  required String to,
}) async {
  final HttpResponseData httpResponseData =
      await _getData(sourceText, from, to: to, dataType: 't');

  final jsonData = httpResponseData.jsonData;
  final sb = StringBuffer();
  try {
    final indexedData = jsonData[_map[RequestType.translation]];
    for (final i in indexedData) {
      sb.write(i[0]);
    }
  } catch (e) {
    _throwBadDataException(
      RequestType.translation,
      httpResponseData,
      innerException: e,
    );
  }
  final translated = sb.toString();
  return _Translation(
    translated,
    source: sourceText,
    sourceLanguage: _languageList[from],
    targetLanguage: _languageList[to],
  );
}