CometChat Pro Documentation

You'll find comprehensive guides and documentation to help you start working with CometChat as quickly as possible. Let's jump right in!

Message Translation

Learn how to translate messages on-the-fly.

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

To translate a message to multiple languages, while sending the message, you need to specify the languages you would like to translate the message to.

We support translation between the following 25 languages: Arabic, Chinese (Simplified), Chinese (Traditional), Czech, Danish, Dutch, English, Finnish, French, German, Hebrew, Hindi, Indonesian, Italian, Japanese, Korean, Malay, Norwegian, Persian, Polish, Portuguese, Russian, Spanish, Swedish, and Turkish.

We identify the language 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.

{
	"message_translation_languages": [ "en", "hi", "..." ]
}

You can programmatically set this as follows-

JSONObject metadataObject = new JSONObject();
JSONArray languageArray = new JSONArray();
languageArray.put("en");
languageArray.put("hi");
languageArray.put("ru");
metadataObject.put("message_translation_languages", languageArray);
message.setMetadata(metadataObject);
CometChat.sendMessage(message, new CometChat.CallbackListener<TextMessage>() {
  @Override
    public void onSuccess(TextMessage textMessage) {
                        
  }

  @Override
    public void onError(CometChatException e) {

  }
});

At the recipients' end, the message will automatically include the translated languages. Here is a sample response:

"message-translation":
{
    "translations": [
      {
        "message_translated": "हैलो दुनिया",
        "language_translated": "hi"
      },
      {
        "message_translated": "привет мир",
        "language_translated": "ru"
      }
    ],
    "language_original": "en"
 }

At the recipients' end, from the message object, you can fetch the metadata by calling the getMetadata() method. Using this metadata, you can fetch translated messages.

JSONObject metadata = message.getMetadata();
if (metadata != null) {
  JSONObject injectedObject = metadata.getJSONObject("@injected");
  if (injectedObject != null && injectedObject.has("extensions")) {
    JSONObject extensionsObject = injectedObject.getJSONObject("extensions");
    if (extensionsObject != null && extensionsObject.has("message-translation"))
        {
          JSONObject messageTranslationObject = extensionsObject.getJSONObject("message-translation");
          JSONArray translations = messageTranslationObject.getJSONArray("translations");
          HashMap<String,String> translationsMap = new HashMap<String,String>();
          for(int i=0; i< translations.length();i++){
            JSONObject translation = translations.getJSONObject(i);
            String translatedText = translation.getString("message_translated");
            String translatedLanguage = translation.getString("language_translated");
            translationsMap.put(translatedLanguage,translatedText);

        }
    }
}
 if (metadata != null) {
  if (metadata.has("@injected")) {
    val injectedJSONObject = metadata.getJSONObject("@injected")
    if (injectedJSONObject != null && injectedJSONObject.has("extensions")) {
    val extensionsObject = injectedJSONObject.getJSONObject("extensions")
    if (extensionsObject.has("message-translation")) {

   val messageTranslationObject=extensionsObject.getJSONObject("message-translation")
   val translationMap: MutableMap<String, String> = mutableMapOf()
   val translations = messageTranslationObject.getJSONArray("translations")
      if (translations.length() > 0) {
       for (i in 0 until translations.length()) {
       val translation = translations.getJSONObject(i)
       val translatedText = translation.getString("message_translated")
       val translatedLanguage = translation.getString("language_translated")
       translationMap.put("message_translated", translatedText)
       translationMap.put("language_translated", translatedLanguage)

           }
         }
       }
     }
   }
 }

The translationsMap contains all the translated messages as values and the language codes as the keys.

Message Translation


Learn how to translate messages on-the-fly.

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.