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

Sendbird's auto-translation feature lets you send text messages in different languages. When sending a text message, 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 into the desired languages. With this, you can add a real-time translation feature to your app. To enable this feature, contact our sales team.

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) => {
        // ...
    });

To show the translated messages, use the userMessage.translations property, which has a Map object containing the language codes and translations as shown in the code below.

Open 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);

Group channel

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

sb.groupChannel.addGroupChannelHandler(UNIQUE_HANDLER_ID, channelHandler);