---
url: https://talkjs.com/docs/Guides/Web_Components/Real-time_Translation/
title: "Set up real-time message translation | Web Components"
---

For AI agents: Find a documentation index at https://talkjs.com/llms.txt (full content at https://talkjs.com/llms-full.txt). Get a markdown version of any page by appending .md to its URL path.

# Set up real-time message translation

Translate chat messages in real time with the Google Cloud Translation API.

Real-time message translation is available on the [Growth plan](https://talkjs.com/pricing/) and up.

Real-time message translation allows users speaking different languages to communicate. TalkJS provides message translation using the Google Cloud Translation API.

There are three steps to set up real-time translation:

1.  [Obtain and configure a Google API key](https://talkjs.com/docs/Guides/Web_Components/Real-time_Translation/#obtain-and-configure-a-google-api-key)
2.  [Set the user's locale](https://talkjs.com/docs/Guides/Web_Components/Real-time_Translation/#set-the-users-locale)
3.  [Enable translation in your Web Components](https://talkjs.com/docs/Guides/Web_Components/Real-time_Translation/#enable-translation-in-your-web-components)

## Obtain and configure a Google API key

1.  If you don't have one yet, create a project in the [Google Cloud Platform console](https://console.cloud.google.com/).
2.  Make sure that billing is enabled. See [Google's documentation on enabling billing](https://cloud.google.com/billing/docs/how-to/modify-project#enable_billing_for_a_project) for more info.
3.  Enable the [Google Cloud Translation API](https://console.cloud.google.com/apis/library/translate.googleapis.com) for your project.
4.  Go to the [credentials page](https://console.cloud.google.com/apis/credentials), click **Create credentials**, and select **API key**.
5.  Click the edit button next to the newly created API key, and configure it as follows:
    -   Give the API key any name you want.
    -   Under **API restrictions**, click **Restrict key**.
    -   From the **Select APIs** dropdown, select the **Cloud Translation API**.
    -   Click **Save**.
6.  Copy the API key.
7.  On the **Settings** page of your [TalkJS dashboard](https://talkjs.com/dashboard), in the **Localization** section, paste the key into **Google translate API key** and click **Save localization settings**.

## Set the user's locale

TalkJS translates messages into the current user's [`locale`](https://talkjs.com/docs/Data_APIs/JavaScript/Users/#UserSnapshot__locale) (a BCP 47 language tag such as `'en-US'`, `'nl-NL'`, or `'es'`). If a user doesn't have a `locale` set, TalkJS falls back to the **Default locale** configured in the **Localization** section of the TalkJS dashboard (and then `en-US`). See [Localization](https://talkjs.com/docs/Features/Language_Support/#localization) for supported languages.

You can set a user's `locale` when creating or updating the user with the [`@talkjs/core` Data API](https://talkjs.com/docs/Data_APIs/JavaScript/Users/):

```js
import { getTalkSession } from '@talkjs/core';

const session = getTalkSession({
  appId: '<APP_ID>',
  userId: 'sample_user_alice',
});

session.currentUser.createIfNotExists({
  name: 'Alice',
  locale: 'en-US',
});
```

Note that changing the current user's `locale` while a chat component is already mounted does not re-translate messages that have already been translated in that session.

## Enable translation in your Web Components

By default, message translation and the translation toggle are disabled. You can let users toggle translation themselves, enable translation by default, or control it programmatically per conversation.

### Show a translation toggle

Set the [`show-translation-toggle`](https://talkjs.com/docs/UI_Components/Web_Components/t-inbox/#InboxProps__showTranslationToggle) attribute (or `showTranslationToggle` JavaScript property) on `<t-inbox>` or [`<t-chatbox>`](https://talkjs.com/docs/UI_Components/Web_Components/t-chatbox/#ChatboxProps__showTranslationToggle) to display a toggle button below the message list:

**Angular**

```html
<t-inbox
  app-id="<APP_ID>"
  user-id="sample_user_alice"
  [show-translation-toggle]="true"
/>
```

**Vue**

```html
<t-inbox
  app-id="<APP_ID>"
  user-id="sample_user_alice"
  :show-translation-toggle="true"
/>
```

**Svelte**

```js
<t-inbox
  app-id="<APP_ID>"
  user-id="sample_user_alice"
  show-translation-toggle={true}
></t-inbox>
```

**HTML**

```html
<t-inbox
  app-id="<APP_ID>"
  user-id="sample_user_alice"
  show-translation-toggle="true"
></t-inbox>
```

In both the `default` and `classic` themes, this displays a clickable toggle below the messages (for example, "Translate this conversation to English" or "Show original", localized to the user's locale). Whenever translation is active, TalkJS also displays the "Translated by Google" attribution.

### Enable translation by default

Set [`default-translation-enabled`](https://talkjs.com/docs/UI_Components/Web_Components/t-inbox/#InboxProps__defaultTranslationEnabled) (or the `defaultTranslationEnabled` property) on `<t-inbox>`, [`<t-chatbox>`](https://talkjs.com/docs/UI_Components/Web_Components/t-chatbox/#ChatboxProps__defaultTranslationEnabled), or [`<t-conversation-list>`](https://talkjs.com/docs/UI_Components/Web_Components/t-conversation-list/#ConversationListProps__defaultTranslationEnabled) to translate conversations automatically when no preference has been saved yet:

**Angular**

```html
<t-inbox
  app-id="<APP_ID>"
  user-id="sample_user_alice"
  [default-translation-enabled]="true"
  [show-translation-toggle]="true"
/>
```

**Vue**

```html
<t-inbox
  app-id="<APP_ID>"
  user-id="sample_user_alice"
  :default-translation-enabled="true"
  :show-translation-toggle="true"
/>
```

**Svelte**

```js
<t-inbox
  app-id="<APP_ID>"
  user-id="sample_user_alice"
  default-translation-enabled={true}
  show-translation-toggle={true}
></t-inbox>
```

**HTML**

```html
<t-inbox
  app-id="<APP_ID>"
  user-id="sample_user_alice"
  default-translation-enabled="true"
  show-translation-toggle="true"
></t-inbox>
```

You can omit `show-translation-toggle` if you want conversations to be translated without letting users turn translation off. The "Translated by Google" attribution still appears whenever translation is active.

If you render a standalone `<t-conversation-list>` alongside a `<t-chatbox>`, set `default-translation-enabled` on both elements so last-message previews in the conversation list match the open chatbox.

`default-translation-enabled` applies only to conversations that do not yet have a saved preference in `localStorage`. Updating the attribute or property updates any conversation without a saved preference, and setting it alone does not write a preference to `localStorage`.

### Control translation programmatically

Call `setTranslationEnabled` directly on the `<t-inbox>`, `<t-chatbox>`, or `<t-conversation-list>` DOM element:

-   [`ChatboxElement.setTranslationEnabled(enabled)`](https://talkjs.com/docs/UI_Components/Web_Components/t-chatbox/#ChatboxController__setTranslationEnabled): enables or disables translation for the conversation currently shown in `<t-chatbox>`.
-   [`InboxElement.setTranslationEnabled(enabled, conversationId?)`](https://talkjs.com/docs/UI_Components/Web_Components/t-inbox/#InboxController__setTranslationEnabled): enables or disables translation for `conversationId`, or for the selected conversation (or conversation shown in the chatbox) if omitted.
-   [`ConversationListElement.setTranslationEnabled(enabled, conversationId?)`](https://talkjs.com/docs/UI_Components/Web_Components/t-conversation-list/#ConversationListController__setTranslationEnabled): enables or disables translation for `conversationId`, or for the selected conversation if omitted.

```html
<div>
  <button id="enable-translation" type="button">Translate conversation</button>
  <button id="disable-translation" type="button">Show original</button>
</div>

<t-inbox
  id="inbox"
  app-id="<APP_ID>"
  user-id="sample_user_alice"
  style="width: 100%; height: 600px;"
></t-inbox>

<script type="module">
  import '@talkjs/web-components';
  import '@talkjs/web-components/default.css';

  const inbox = document.getElementById('inbox');

  document
    .getElementById('enable-translation')
    .addEventListener('click', () => {
      inbox.setTranslationEnabled(true);
      // Or target a specific conversation:
      // inbox.setTranslationEnabled(true, 'conversation_id');
    });

  document
    .getElementById('disable-translation')
    .addEventListener('click', () => {
      inbox.setTranslationEnabled(false);
    });
</script>
```

## How preferences are stored and synced

-   Clicking the translation toggle or calling `setTranslationEnabled` saves the user's preference in `localStorage` per app, user, and conversation.
-   Components on the same page for the same app ID and user ID share translation state immediately. For example, toggling translation in `<t-chatbox>` immediately updates the last-message preview in `<t-conversation-list>`.
-   Once mounted, components also stay in sync across browser tabs for the same app ID and user ID.
-   A saved preference overrides `defaultTranslationEnabled` on future visits until it is changed again via the toggle or `setTranslationEnabled`.
-   When translation is enabled for a conversation, new incoming messages and edited messages are translated automatically.

## Custom themes and Google attribution

Both pre-built themes (`default` and `classic`) render the translation toggle and Google attribution inside the [`<AfterMessages>`](https://talkjs.com/docs/UI_Components/Theme/Theme_components/AfterMessages/) theme component.

[Google's Cloud Translation attribution policy](https://cloud.google.com/translate/attribution) requires displaying Google attribution whenever translated messages are shown. If you override `<AfterMessages>` in a custom theme, include the [`<TranslationAttribution>`](https://talkjs.com/docs/UI_Components/Theme/System_components/TranslationAttribution/) system component whenever `common.translationEnabled` is `true`:

**JavaScript**

```js
import { html, TranslationAttribution } from '@talkjs/web-components';
import '@talkjs/web-components/default.css';

const theme = {
  AfterMessages: ({ common }) => html`
    <div>
      ${common.translationEnabled &&
      html`<${TranslationAttribution} t=${common.t} />`}
      ${common.showTranslationToggle &&
      html`<button
        type="button"
        onClick=${() =>
          common.chatbox.setTranslationEnabled(!common.translationEnabled)}
      >
        ${common.translationEnabled
          ? common.t.DISABLE_TRANSLATION
          : common.t.ENABLE_TRANSLATION}
      </button>`}
    </div>
  `,
};

const chatbox = document.querySelector('t-chatbox');
chatbox.theme = theme;
```

**JSX**

```jsx
import { TranslationAttribution } from '@talkjs/web-components';
import '@talkjs/web-components/default.css';

const theme = {
  AfterMessages: ({ common }) => (
    <div>
      {common.translationEnabled && <TranslationAttribution t={common.t} />}
      {common.showTranslationToggle && (
        <button
          type="button"
          onClick={() =>
            common.chatbox.setTranslationEnabled(!common.translationEnabled)
          }
        >
          {common.translationEnabled
            ? common.t.DISABLE_TRANSLATION
            : common.t.ENABLE_TRANSLATION}
        </button>
      )}
    </div>
  ),
};

const chatbox = document.querySelector('t-chatbox');
chatbox.theme = theme;
```

Overriding `<AfterMessages>` also replaces the default typing indicator. See the [`<AfterMessages>` reference](https://talkjs.com/docs/UI_Components/Theme/Theme_components/AfterMessages/) for the default theme's implementation and details on how the message list's `flex-direction: column-reverse` layout affects child order.
