Chat / Flutter
Current version: v3
Chat Flutter v3
Chat Flutter
Chat
Flutter
Home
/
Chat
/
Flutter
/
Message

Auto-translate messages

Sendbird's auto-translation feature lets you send text messages in different languages. When sending a text message, set a list of language codes to a UserMessageParams object and 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.

try {
    final params = UserMessageParams(message: MESSAGE)
        ..targetLanguages = ['es', 'ko'];   // Spanish and Korean

    final preMessage = await channel.sendUserMessage(params, onCompleted: (msg, error){
        // The message has been successfully sent and translation of the messages is accessible through the translations property.
    });
} catch (e) {
    // Handle error.
}

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

class MyClass with ChannelEventHandler {
    // Add this class via sendbird.addChannelEventHandler(UNIQUE_HANDLER_ID, this).
    // Or remove the handler with sendbird.removeChannelEventHandler(UNIQUE_HANDLER_ID) when it's no longer needed.
    @override
    void onMessageReceived(BaseChannel channel, BaseMessage message) {
        If (message is UserMessage) {
            final esTranslatedMessage = message.translations[‘es’];     // Spanish
            // Display translation in the UI.
        }
    }
}