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

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-

let metadataDic : [String : Any]?
let languageArray : NSMutableArray = NSMutableArray();

languageArray.add("en")
languageArray.add("hi")
languageArray.add("ru")
metadataDic.setValue(languageArray, forKey: "message_translation_languages")

let message = TextMessage(receiverUid: receiverID, text: text, receiverType: .user)
message.setMetadata(metadataDic);

CometChat.sendTextMessage(message: message, onSuccess: { (message) in

  print("TextMessage sent successfully. " + message.stringValue())

}) { (error) in

  print("TextMessage sending failed with error: " + error.errorDescription);
}

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.

var metadata : [String : Any]? = message.metaData
if metadata != nil {
            
  var injectedObject : [String : Any]? = (metadata?["@injected"] as? [String : Any])!
            
    if injectedObject != nil && (injectedObject!["extensions"] != nil){
                
      var extensionsObject : [String : Any]? = injectedObject?["extensions"] as? [String : Any]

      if extensionsObject != nil && extensionsObject?["message-translation"] != nil {
                    
        var messageTranslationObject = extensionsObject?["message-translation"] as! [String :  Any]

        let translations : NSMutableArray = messageTranslationObject["translations"] as! NSMutableArray
        let translationDic : NSMutableDictionary = NSMutableDictionary()

        for data in translations {
                        
          let translation : NSDictionary = data as! [String:Any] as NSDictionary
          let translatedText = translation["message_translated"]
          let translatedLanguage = translation["language_translated"]

          translationDic.setValue(translatedText, forKey: translatedLanguage as! String)
         }
      }
   }
}

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

Message Translation


Suggested Edits are limited on API Reference Pages

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