# Get the user's browser language

**Cognigy Support**  
July 7, 2022

Providing a multi-lingual experience in conversational AI projects is a very common use case nowadays. However, there are various approaches of how one could implement this:

1. In the webchat, for instance, the Realtime Translation Transformer could be used for translating all messages automatically.  
2. On the other hand, the Localization features could be used in order to provide different language versions of the virtual agent. In this case, the quality of the translation would be the highest since native speakers would provide the translated texts in, for example, Say Nodes.

In both scenarios, Cognigy.AI needs to be aware of the **user's preferred language**. Where the virtual agent could provide a first welcome message with _language select quick replies_, such as displayed below, this information could be sent to the virtual agent directly:

## Send the preferred language to Cognigy.AI

Such as described in the [**Webchat Embedding documentation**](https://github.com/Cognigy/WebchatWidget/blob/master/docs/embedding.md), the _initWebchat()_ function provides a callback method. This callback consists of the so-called **webchat object** which can be used in order to send a message to the virtual agent. This means a so-called [DATA_ONLY message can be sent](https://github.com/Cognigy/WebchatWidget/blob/master/docs/webchat-api.md#sending-data-only-messages) which is not displayed in the chat window. Finally, this invisible message could notify the virtual agent about the preferred user language in the current web browser and [switch the locale internally](https://docs.cognigy.com/docs/switch-locale):

_English: "Hello! I see that you would like to speak Spanish. Fine."

The only code that needs to be added to the _initWebchat()_ callback method is the following:

```
 initWebchat(
 "https://endpoint-trial...").then((webchat) => {
   // Get browser language and send to Cognigy.AI
   const browserLanguage = navigator.language || navigator.userLanguage;

webchat.sendMessage("", {
     language: browserLanguage
   });
});
```

As soon as the webchat is loaded, the user's browser language is stored as a constant:

```
const browserLanguage = navigator.language || navigator.userLanguage;
```

Afterward, this information is sent to Cognigy.AI via the _webchat.sendMessage()_ method:

```
webchat.sendMessage("", {
  language: browserLanguage
});
```

In Cognigy.AI, this data information can be checked by using CognigyScript in order to switch the locale:

The language information is stored in

```
input.data.language
```

and will look such as

- de-DE  
- en-US  
- es-ES  
- nl-NL  
- and more ...

### Webchat

- [Customize your Webchat with the MutationObserver API](https://support.cognigy.com/hc/en-us/articles/20142438205468-Customize-your-Webchat-with-the-MutationObserver-API)  
- [3 Ways to send Data from a Website to a Cognigy Flow from Cognigy Webchat V3](https://support.cognigy.com/hc/en-us/articles/16846703249692-3-Ways-to-send-Data-from-a-Website-to-a-Cognigy-Flow-from-Cognigy-Webchat-V3)  
- [Webchat v2: Disable Quick Reply buttons after click](https://support.cognigy.com/hc/en-us/articles/5398841450396-Webchat-v2-Disable-Quick-Reply-buttons-after-click)  
- [Webchat Overview](https://support.cognigy.com/hc/en-us/articles/360019867919-Webchat-Overview)  
- [The Demo Webchat](https://support.cognigy.com/hc/en-us/articles/360019831920-The-Demo-Webchat)  
- [Display teaser messages (Preview, Engagement)](https://support.cognigy.com/hc/en-us/articles/360020128379-Display-teaser-messages-Preview-Engagement)  
- [Get the user's browser language](https://support.cognigy.com/hc/en-us/articles/360022044759-Get-the-user-s-browser-language)  
- [Helpful (CSS) Style Examples](https://support.cognigy.com/hc/en-us/articles/360022110539-Helpful-CSS-Style-Examples)  
- [Display a welcome message above the Start Button](https://support.cognigy.com/hc/en-us/articles/4408792943378-Display-a-welcome-message-above-the-Start-Button)  
- [Expire sessions after n hours in Webchats with persistent history](https://support.cognigy.com/hc/en-us/articles/4421763771804-Expire-sessions-after-n-hours-in-Webchats-with-persistent-history)

#### Table of contents

Send the preferred language to Cognigy.AI

- [Send the preferred language to Cognigy.AI](https://support.cognigy.com/hc/en-us/articles/360022044759-Get-the-user-s-browser-language#send-the-preferred-language-to-cognigy-ai-0-0)

### Comments

0 comments

Article is closed for comments.

Was this article helpful?

YesNo

1 out of 1 found this helpful
