Web&小程序&uni-app

最近更新时间:2024-05-28 14:52:02

我的收藏

功能描述

消息翻译目前仅支持手动调接口翻译文本消息内容,图片、视频、文件、语音、自定义消息等非文本内容均不支持翻译。
说明:
1. 文本翻译是增值付费功能,处于内测阶段,您可通过即时通信 IM 文本消息翻译插件交流群联系我们为您开通体验完整功能。

效果展示



translateText

翻译文本的接口。
接口
chat.translateText(options)
参数
参数 options 为 Object 类型,包含的属性值如下:
名称
类型
描述
sourceTextList
Array
待翻译文本数组
sourceLanguage
String
源语言。可以设置为特定语言或 "auto"。"auto" 表示自动识别源语言。
targetLanguage
String
目标语言。支持的目标语言有多种,例如:英语-"en",简体中文-"zh",法语-"fr",德语-"de"等。
返回值
Promise
示例
// 将阿拉伯文翻译成英文
let promise = chat.translateText({
sourceTextList: ['مرحبًا'],
sourceLanguage: 'auto',
targetLanguage: 'en'
});
promise.then(function(imResponse) {
// 翻译成功 translatedTextList 和 sourceTextList 的元素一一对应
const { translatedTextList } = imResponse.data;
}).catch(function(imError) {
// 翻译失败,错误码2117
console.warn('translateText error:', imError);
});
// 将英文翻译成中文
let promise = chat.translateText({
sourceTextList: ['Hello Tencent', 'Build in-app chat with Tencent Cloud Chat'],
sourceLanguage: 'auto',
targetLanguage: 'zh'
});
promise.then(function(imResponse) {
// 翻译成功 translatedTextList 和 sourceTextList 的元素一一对应
const { translatedTextList } = imResponse.data;
}).catch(function(imError) {
// 翻译失败,错误码2117
console.warn('translateText error:', imError);
});