# Change language within the conversation

The two solutions presented below go hand-in-hand in determining what's the user favorite language to interact in.

{% hint style="info" %}
A bot that doesn't have any information about the user's language will automatically use the [primary language](https://docs.chatlayer.ai/nlp/multilanguage-bots#primary-vs.-extra-languages). The active language inside the conversation can be changed inside the flow.
{% endhint %}

{% hint style="danger" %}
Please note that the bot can't figure out the language based on what the user is saying. What the bot can do instead is using the channel information. On Facebook, for example, the bot will use the value it receives from the FB user API. For the web widget, it'll use the SDK language.
{% endhint %}

## Toggle on Language detection

You can use  [**Language detection**](https://docs.chatlayer.ai/navigation/settings/settings#language-detection)  to check if the user's language is supported by your bot. If your bot doesn't support the user's language, redirect language detection to [a block that collects the user language](#collect-the-users-preferred-language).

## Collect the user's preferredLanguage

{% hint style="warning" %}
The variable that stores the user's preferred language is called `preferredLanguage`. You can collect and change this variable in multiple ways.
{% endhint %}

### With buttons

To build a buttons flow that collects the user's `preferredLanguage`:

1. Go to your canvas in [**Flows**](https://docs.chatlayer.ai/navigation/bot-builder/flows).
2. Create a [**Collect input** ](https://docs.chatlayer.ai/buildabot/flow-logic/dialog-state/user-input-bot-dialog)block.
3. Inside this block, ask the user in what language they'd like to continue, with buttons corresponding to your bot languages.
4. For each button, set the `preferredLanguage` **variable** with **value** corresponding to the chosen language code.

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FVMnTWiQl2M5BA53vcXwa%2FScreenshot%202024-09-03%20at%2010.47.43.png?alt=media&#x26;token=fdeec342-f14b-441a-a281-f36db0321fce" alt="" width="278"><figcaption></figcaption></figure>

5. Fill the lower part with these info:

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FUvX959YGLdKSXUbZXpN6%2FScreenshot%202024-09-03%20at%2010.49.53.png?alt=media&#x26;token=f7afc353-c3e7-4986-9009-cb6a2dec209f" alt="" width="276"><figcaption></figcaption></figure>

6. Click **Save**.&#x20;

### With an intent

{% stepper %}
{% step %}
Go to your [**Entities**](https://docs.chatlayer.ai/navigation/natural-language-processing-nlp/synonym-entities) tab.&#x20;
{% endstep %}

{% step %}
[Create a **Contextual entity**](https://docs.chatlayer.ai/natural-language-processing-nlp/detect-information-with-entities/contextual-entities#add-contextual-entities) for the intent that will trigger the flow to change language.

In the example below, we've created an entity called **@language** to identify the languages that are available in the bot and have set a value for them:

* `en` for English
* `pt` for Portuguese
* `es` for Spanish

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FyUIx3ShnVbFOamfNFSLL%2Fimage.png?alt=media&#x26;token=866f18f1-715d-44a4-8372-acc695dbec8f" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
[Create an **intent**](https://docs.chatlayer.ai/navigation/natural-language-processing-nlp/intents#add-a-new-intent) that will trigger the flow to change the language.

The intent of this example is called **change language** and is using the entity **@language**.

<div align="left"><figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2F4Rus52CDUx8Eb92GwSFr%2Fimage.png?alt=media&#x26;token=a731f87d-4370-4391-bf01-b839a7b138d1" alt=""><figcaption></figcaption></figure></div>
{% endstep %}

{% step %}
[**Train** your NLP](https://docs.chatlayer.ai/nlp/natural-language-processing-nlp/train-your-nlp).
{% endstep %}

{% step %}
Go back to your [**Flows**](https://docs.chatlayer.ai/navigation/bot-builder/flows) and add your **change language** intent to the canvas.
{% endstep %}

{% step %}
Make the change language intent go to a **Condition** block that we'll call **Go to language.**
{% endstep %}

{% step %}
In this **Condition** block, build conditions so that your `language` variable goes to the next block with the new language set as the value for the `preferredLanguage` variable, as in the example below:

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2Fu8zN8unhlzZcvRvNISrT%2Fimage.png?alt=media&#x26;token=4a2b7de2-8f60-4fc2-8646-6b827da59163" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
At the end, your flow should look like something like this:

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FWAygZCrizFFU0nIqfZPB%2Fimage.png?alt=media&#x26;token=9c773759-8b98-482c-b64a-0acf7e575c8d" alt=""><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}

## Trigger a language with the Web channel

In the web widget you can trigger a specific bot language based on the language of the page the user is looking at. Find more information on this [here](https://docs.chatlayer.ai/channels/all-channels/web/web-v2#set-to-a-specific-language)[.](https://docs.chatlayer.ai/channels/all-channels/web/web-v2/web-v2-methods-and-options#change-the-language)
