translateText method

Future<V2TimValueCallback<Map<String, String>>> translateText({
  1. required List<String> texts,
  2. required String targetLanguage,
  3. String? sourceLanguage,
})

Implementation

Future<V2TimValueCallback<Map<String, String>>> translateText({
  required List<String> texts,
  required String targetLanguage,
  String? sourceLanguage,
}) async {
  try {
    final res = await wrappedPromiseToFuture(timeweb!.translateText(mapToJSObj({
      "sourceTextList": texts,
      "sourceLanguage": sourceLanguage ?? "auto",
      "targetLanguage": targetLanguage,
    })));
    List<String> translatedtext = List<String>.from(jsToMap(res.data)["translatedTextList"] ?? []);
    var resMap = {};
    if (translatedtext.isNotEmpty) {
      for (var elem in translatedtext) {
        resMap[texts[translatedtext.indexOf(elem)]] = elem;
      }
    }
    if (res.code == 0) {
      return CommonUtils.returnSuccess<Map<String, String>>(resMap);
    } else {
      return CommonUtils.returnSuccess<Map<String, String>>(resMap);
    }
  } catch (e) {
    return CommonUtils.returnErrorForValueCb(e.toString());
  }
}