Skip to main content

Built-in translation

The Nexconn Chat SDK provides built-in text translation, allowing users to translate message content into different languages without leaving your app.

How it works

The translation service processes text messages on the server and returns the translated content. Access translation capabilities through NCEngine.translateModule.

Translate messages

TypeScript
import { NCEngine, TextMessageContent, TranslateMessageParam } from '@nexconn/chat';

// Create a translation request for an existing message
const param = new TranslateMessageParam(
message.messageId, // messageUId: stable message identifier
'auto', // sourceLanguage: 'auto' to detect automatically
'en', // targetLanguage: override user default (optional)
(message.content as TextMessageContent).text, // text: original text on Web
);

const { code } = await NCEngine.translateModule.translateMessages({
list: [param],
force: false, // Re-translate even if already translated
});
if (code === 0) {
console.log('Translation request submitted');
} else {
console.log('Translation failed. Code:', code);
}
info

The Promise returned by translateMessages only indicates whether the request was accepted. The actual translated content is delivered asynchronously via TranslateHandler onTranslationCompleted.

Parameters

ParameterTypeRequiredDescription
listTranslateMessageParam[]YesArray of translation request descriptors (max 10 items)
forcebooleanNoRe-translate messages that already have a translation. Default: false.
modeTranslateModeNoTranslation mode. Default: TranslateMode.MECHANICAL.

Set translation language

TypeScript
const { code } = await NCEngine.translateModule.setTranslationLanguage('en');
if (code === 0) {
console.log('Translation language set to English');
}

Enable auto-translate

TypeScript
const { code } = await NCEngine.translateModule.setAutoTranslateEnabled(true);
if (code === 0) {
console.log('Auto-translate enabled');
}

Listen for translation events

TypeScript
import { NCEngine, TranslateHandler } from '@nexconn/chat';

NCEngine.addTranslateHandler('trans-handler', new TranslateHandler({
onTranslationCompleted({ results }) {
console.log('Translation finished:', results);
},
onAutoTranslateStateChanged({ isEnabled }) {
console.log('Auto-translate state changed:', isEnabled);
},
onTranslationLanguageChanged({ language }) {
console.log('Translation language changed to:', language);
},
}));

See Supported languages and codes for the full list of supported language codes.