translateText static method

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

Implementation

static Future<V2TimValueCallback<Map<String, String>>> translateText({
  required List<String> texts,
  required String targetLanguage,
  String? sourceLanguage,
}) async {
  String userData = Tools.generateUserData();
  Pointer<Uint8> user_data = Tools.string2PointerInt8(userData);

  Pointer<Uint8> texts_ptr = Tools.string2PointerInt8(json.encode(texts));
  Pointer<Uint8> targetLanguage_ptr = Tools.string2PointerInt8(targetLanguage);
  Pointer<Uint8> sourceLanguage_ptr = Tools.string2PointerInt8(sourceLanguage ?? "auto");
  int res = desktopSDK.D_TIMMsgTranslateText(
    texts_ptr,
    sourceLanguage_ptr,
    targetLanguage_ptr,
    user_data,
  );
  if (res != TIMResult.TIM_SUCC) {
    return V2TimValueCallback<Map<String, String>>.fromJson({
      "code": res,
      "desc": "",
    });
  } else {
    Map<String, dynamic>? data = await getAsyncData(apiKey: userData);
    Map<String, String> res = Map<String, String>.from({});
    List<Map<String, dynamic>> dataList = List<Map<String, dynamic>>.from(json.decode(data["json_param"].isEmpty ? json.encode([]) : data["json_param"]));
    for (var element in dataList) {
      res[element["msg_translate_text_source_text"]] = element["msg_translate_text_target_text"];
    }
    return V2TimValueCallback<Map<String, String>>.fromJson({
      "code": data["code"],
      "desc": data["desc"],
      "data": res,
    });
  }
}