Browse SDKs · HarmonyOS
Platform
SDKsHarmonyOSEnterprise

Translate message

Translate a synchronized message and obtain an updated message object containing translated content.

Copy

Locates the target message using conversationID and clientMsgID.

Code example

import sdk, { TranslateMessageReq, TranslateMessageResp } from '@openimsdk/imsdk';

const request: TranslateMessageReq = {
  conversationID: conversationID,
  clientMsgID: message.clientMsgID,
  sourceLanguageCode: 'en',
  targetLanguageCode: 'zh-CN',
};
const response: TranslateMessageResp = await sdk.translateMessage(request);
replaceMessage(response.message.clientMsgID, response.message);

Parameters

ParameterTypeRequiredDescription
conversationIDstringYesTarget conversation identifier.
clientMsgIDstringYesClient message identifier of the message to translate.
sourceLanguageCodestringYesSource language code (for example, 'en').
targetLanguageCodestringYesTarget language code (for example, 'zh-CN').

Return value

Returns a Promise resolving to TranslateMessageResp:

PropertyTypeDescription
messageIMMessageUpdated message object containing translated text.

State changes after the call

The source message must already be synchronized to the local database. After the Promise resolves, update the local message item matching conversationID:clientMsgID, while preserving the original message text for toggle display. This operation does not broadcast translation results to other client devices. For standalone text translation, see Translate text.