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.
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.
}
}
}