Chat / JavaScript
Chat JavaScript v4
Chat JavaScript
Chat
JavaScript
Home
/
Chat
/
JavaScript
/
Message
This is the new Docs for Chat SDK v4 for JavaScript. To see the previous Docs, click here.

Auto-translate messages

Using Sendbird's auto-translation feature, you can send text messages in different languages. Set an array of language codes to the UserMessageCreateParams object, then pass the object as an argument to a parameter in the sendUserMessage() method to request messages to be translated in the corresponding language.

Note: Sendbird's message auto-translation feature is powered by Google Cloud Translation API recognition engine. You can find the language codes supported by the engine in the translation engine page. You can also visit the language Support page in Google Cloud Translation.

const params: UserMessageCreateParams = {
    translationTargetLanguages = ['es', 'ko'],  // Spanish and Korean.
};
// ...
channel.sendUserMessage(params)
    .onSucceeded((message: UserMessage) => {
        // ...
    });

As shown in the code below, you can retrieve the translation of a text message by using the userMessage.translations property, which has a Map object containing a language code and the translated message.

Open channelGroup channel
const channelHandler = new OpenChannelHandler({
    onMessageReceived: (channel: BaseChannel, message: BaseMessage) => {
        if (message instanceof UserMessage) {
            const esTranslatedMessage = message.translations['es']; // Spanish.
            // ...
        }
    },
});

sb.openChannel.addChannelHandler(UNIQUE_HANDLER_ID, channelHandler);