Multi-language experience with real-time translated messages

Cognigy.AI Lab

You've entered the Cognigy.AI Lab. This content and example code is inspirational and not quality assured for production.

When it comes to conversational AI, one might think about use cases in multiple languages to offer the virtual agent support to the most possible amount of users around the world. However, the number of spoken languages and related efforts could stop these ambitions.



Translating every message of the user and virtual agent needs some kind of automated process to reduce the human effort. Therefore, one could think about using well-known translators, such as Microsoft, Google, or DeepL Translate. Furthermore, the actual virtual agent should only use one language, namely the companies one, to be developed. In summary, that means the incoming and outgoing message needs to be translated "on the fly":

  1. Translation tool (Google, Microsoft, DeepL)
  2. Middleware for translating the messages

How to

Everything required can be implemented in the Cognigy.AI platform. Therefore, this example will use a Transformer Function to develop the middleware and Microsoft to provide the actual translation of the current message.


For this example, the virtual agent will detect the language of the incoming user message and translate the outgoing text message into the users one. In the end, it will look like the conversations shown in the image above.

Translate Function

In order to translate messages, the Transformer must contain a related function:

async function microsoftTranslate(text, language, apikey) {

	try {
		const microsoftOptions = {
			method: 'POST',
			uri: `${language}`,
			headers: {
				'Ocp-Apim-Subscription-Key': apikey,
				'Content-type': 'application/json',
				'Accept': 'application/json',
				'X-ClientTraceId': uuid.v4().toString()
			body: [
			json: true

		const response = await httpRequest(microsoftOptions);

		return response;
	} catch (error) {
		throw new Error(error.message);


Handle Input

All incoming messages need to be translated into the primary language of the virtual agent. Furthermore, the detected user language has to be remembered:

handleInput: async ({ payload, endpoint }) => {

	// Translate the text
	let translation = await microsoftTranslate(payload.text, FLOW_LOCALE, MICROSOFT_TRANSLATOR_API_KEY);

	let translatedText = translation[0].translations[0].text;
	let detectedLanguage = translation[0].detectedLanguage.language;

	// Write detected language into the session storage
	const sessionStorage = await getSessionStorage(payload.userId, payload.sessionId);
	sessionStorage.detectedLanguage = detectedLanguage;

	return {
		userId: payload.userId,
		sessionId: payload.sessionId,
		text: translatedText,

Handle Output

With all the collected information in mind, the virtual agent can send the right output message and directly translate it:

handleOutput: async ({ processedOutput, output, endpoint, userId, sessionId }) => {

	// Get the detected language from the input user message
	const sessionStorage = await getSessionStorage(userId, sessionId);
	const detectedLanguage = sessionStorage.detectedLanguage;

	// Translate the outgoing message
	let translation = await microsoftTranslate(processedOutput.text, detectedLanguage, MICROSOFT_TRANSLATOR_API_KEY);
        processedOutput.text = translation[0].translations[0].text;

	return processedOutput;


Finally, the virtual agent should return a text message to answer the user's request. This can be added easily in a Cognigy Flow:




Please sign in to leave a comment.

Was this article helpful?
0 out of 0 found this helpful