Skip to main content

Message Translation

The Message Translation extension helps you translate messages into multiple languages.

Extension settings

  1. Login to CometChat and select your app.
  2. Go to the Extensions section and enable the Message Translation extension.

How does it work?

The messages translation extension allows the receivers of the message to translate it in the language of their choice.

In a group of multi-lingual people, a message sent by one of the participants can be in English. However, it can be translated by other members of the group to their languages.

We support translations in the following languages:

LanguageLanguage code
Afrikaansaf
Albaniansq
Amharicam
Arabicar
Armenianhy
Azerbaijaniaz
Bengalibn
Bosnianbs
Bulgarianbg
Catalanca
Chinese (Simplified)zh
Chinese (Traditional)zh-TW
Croatianhr
Czechcs
Danishda
Darifa-AF
Dutchnl
Englishen
Estonianet
Farsi (Persian)fa
Filipino, Tagalogtl
Finnishfi
Frenchfr
French (Canada)fr-CA
Georgianka
Germande
Greekel
Gujaratigu
Haitian Creoleht
Hausaha
Hebrewhe
Hindihi
Hungarianhu
Icelandicis
Indonesianid
Irishga
Italianit
Japaneseja
Kannadakn
Kazakhkk
Koreanko
Latvianlv
Lithuanianlt
Macedonianmk
Malayms
Malayalamml
Maltesemt
Marathimr
Mongolianmn
Norwegian (Bokmål)no
Pashtops
Polishpl
Portuguese (Brazil)pt
Portuguese (Portugal)pt-PT
Punjabipa
Romanianro
Russianru
Serbiansr
Sinhalasi
Slovaksk
Sloveniansl
Somaliso
Spanishes
Spanish (Mexico)es-MX
Swahilisw
Swedishsv
Tamilta
Telugute
Thaith
Turkishtr
Ukrainianuk
Urduur
Uzbekuz
Vietnamesevi
Welshcy

The language is identified using identifiers from RFC 5646 — if there is a 2-letter ISO 639-1 identifier, with a regional subtag if necessary, it uses that. Otherwise, it uses the ISO 639-2 3-letter code.

Implementation

The extension uses the callExtension() method provided by CometChat SDKs.

The messages will be translated according to the languages specified by the receiver. You can specify the languages for translation as an array. If you pass in an empty array, the language will default to English.

CometChat.callExtension('message-translation', 'POST', 'v2/translate', {
"msgId": 12,
"text": "Hey there! How are you?",
"languages": [ "ru", "hi", "mr" ]
}).then( result => {
// Result of translations
})
.catch(error => {
// Some error occured
});

The result will be received in the success callback of the callExtension() method. It will have the following JSON structure:

{
"msgId": 12,
"translations": [
{
"message_translated": "Эй там! Как ты?",
"language_translated": "ru",
"error": null,
"error_description": null
},
{
"message_translated": "अरे वहाँ! आप कैसे हैं?",
"language_translated": "hi",
"error": null,
"error_description": null
},
{
"message_translated": "",
"language_translated": "mr",
"error": "UnsupportedLanguagePairException",
"error_description": "Unsupported language pair: en to mr. Target language 'mr' is not supported"
}
],
"language_original": "en"
}

If the source language is not supported for translation, then you will get the following error:

{
"code": "ERR_NOT_SUPPORTED",
"message": "Autodetected source language '<language_code>' is not supported"
}

Translating at the sender's end

If the languages of the end-users/receivers are known before hand, this method of implementation can be used.

That is, before your trigger CometChat.sendMessage method, make a call to the message translation extension as mentioned above. Since the message has not been sent yet, the msgId won't be available and can be skipped in that call.

Once you get the response, make an entry in your message's metadata as shown below:

const messageText = "Hey there! How are you?";

const metadata = {
"message-translation": {
"ru": "Эй там! Как ты?",
"hi": "अरे वहाँ! आप कैसे हैं?"
},
};

const textMessage = new CometChat.TextMessage(receiverID, messageText, receiverType);
textMessage.setMetadata(metadata);

CometChat.sendMessage(textMessage).then(
message => {
console.log("Message sent successfully:", message);
}, error => {
console.log("Message sending failed with error:", error);
}
);

Then, at the receiver's end, based on the device/browser language, you can show the translated text message in their chats from the metadata.

Translating at the receiver's end

If the languages of the end-users/receivers are not known before hand, this method of implementation can be used.

Simply show a translate button above every message bubble in the UI. Make a call to the extension as mentioned above. The translation can then be shown in the same message bubble.

However, unlike the translations that are saved in the metadata when translating at the sender's end, these translations are ephemeral and will be lost once the page is reloaded, the user fetches older messages and scrolls back to the latest translated messages or the user switces conversations and comes back to the conversation with translated messages.