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.

View as Markdown

Real-time message translation is available on the Growth plan 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
  2. Set the user's locale
  3. 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.
  2. Make sure that billing is enabled. See Google's documentation on enabling billing for more info.
  3. Enable the Google Cloud Translation API for your project.
  4. Go to the credentials page, 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, 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 (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 for supported languages.

You can set a user's locale when creating or updating the user with the @talkjs/core Data API:

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 attribute (or showTranslationToggle JavaScript property) on <t-inbox> or <t-chatbox> to display a toggle button below the message list:

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

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 (or the defaultTranslationEnabled property) on <t-inbox>, <t-chatbox>, or <t-conversation-list> to translate conversations automatically when no preference has been saved yet:

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

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:

<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> theme component.

Google's Cloud Translation attribution policy requires displaying Google attribution whenever translated messages are shown. If you override <AfterMessages> in a custom theme, include the <TranslationAttribution> system component whenever common.translationEnabled is true:

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;

Overriding <AfterMessages> also replaces the default typing indicator. See the <AfterMessages> reference for the default theme's implementation and details on how the message list's flex-direction: column-reverse layout affects child order.