> For the complete documentation index, see [llms.txt](https://docs.chatlayer.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.chatlayer.ai/nlp/languages/change-language-within-the-conversation.md). Agents can also search and read this documentation through its [MCP server](https://docs.chatlayer.ai/~gitbook/mcp).

# Change language within the conversation

If you have built your bot to support multiple languages, you want it to talk to the user in their own language.

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/pages/-LLTwJA_ouoSwx78oYQG#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.md#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.md).
2. Create a [**Collect input** ](https://docs.chatlayer.ai/buildabot/flow-logic/dialog-state/user-input-bot-dialog.md)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&amp;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&amp;token=f7afc353-c3e7-4986-9009-cb6a2dec209f" alt="" width="276"><figcaption></figcaption></figure>

6. Click **Save**.

### With an intent

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

{% step %}
[Create a **Contextual entity**](https://docs.chatlayer.ai/nlp/natural-language-processing-nlp/detect-information-with-entities/contextual-entities.md#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&amp;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.md#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&amp;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.md).
{% endstep %}

{% step %}
Go back to your [**Flows**](https://docs.chatlayer.ai/navigation/bot-builder/flows.md) 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&amp;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&amp;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.md#set-to-a-specific-language)[.](https://docs.chatlayer.ai/channels/all-channels/web/web-v2/web-v2-methods-and-options.md#change-the-language)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.chatlayer.ai/nlp/languages/change-language-within-the-conversation.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
