SDKsHarmonyOSEnterprise
Translate text
Translate arbitrary text into a specified target language.
Translates an arbitrary input text snippet without creating or modifying chat message records.
Code example
import sdk, { TranslateTextReq, TranslateTextResp } from '@openimsdk/imsdk';
const request: TranslateTextReq = {
text: 'Hello, OpenIM',
sourceLanguage: 'en',
targetLanguage: 'zh-CN',
};
const response: TranslateTextResp = await sdk.translateText(request);
showTranslation(response.translatedText, response.detectedLanguage);Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
text | string | Yes | Raw text string to translate. |
sourceLanguage | string | Yes | Source language code (for example, 'en'). |
targetLanguage | string | Yes | Target language code (for example, 'zh-CN'). |
Return value
Returns a Promise resolving to TranslateTextResp:
| Property | Type | Description |
|---|---|---|
translatedText | string | Translated text output. |
detectedLanguage | string | Language detected by the translation service engine. |
State changes after the call
Language codes must adhere to formats supported by your configured translation service provider. detectedLanguage reflects the language detected by the engine and may differ from the requested source language. Render translation output alongside the original text without overwriting it. To translate an existing message and update local message state, see Translate message.
Was this page helpful?