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 React 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 React 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

Pass showTranslationToggle to <Inbox> or <Chatbox> to display a toggle button below the message list:

import { Inbox } from '@talkjs/react-components';
import '@talkjs/react-components/default.css';

export default function Chat() {
  return (
    <Inbox
      appId="<APP_ID>"
      userId="sample_user_alice"
      showTranslationToggle
      style={{ width: '100%', height: '600px' }}
    />
  );
}

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

Pass defaultTranslationEnabled to <Inbox>, <Chatbox>, or <ConversationList> to translate conversations automatically when no preference has been saved yet:

import { Inbox } from '@talkjs/react-components';
import '@talkjs/react-components/default.css';

export default function Chat() {
  return (
    <Inbox
      appId="<APP_ID>"
      userId="sample_user_alice"
      defaultTranslationEnabled
      showTranslationToggle
      style={{ width: '100%', height: '600px' }}
    />
  );
}

You can omit showTranslationToggle 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 <ConversationList> alongside a <Chatbox>, pass defaultTranslationEnabled to both components so last-message previews in the conversation list match the open chatbox.

defaultTranslationEnabled applies only to conversations that do not yet have a saved preference in localStorage. Updating the prop updates any conversation without a saved preference, and setting the prop alone does not write a preference to localStorage.

Control translation programmatically

Use a ref to call setTranslationEnabled on the component controller:

import { useRef } from 'react';
import { Inbox } from '@talkjs/react-components';
import '@talkjs/react-components/default.css';

export default function Chat() {
  const inboxRef = useRef(null);

  return (
    <>
      <div>
        <button
          type="button"
          onClick={() => inboxRef.current?.setTranslationEnabled(true)}
        >
          Translate conversation
        </button>
        <button
          type="button"
          onClick={() => inboxRef.current?.setTranslationEnabled(false)}
        >
          Show original
        </button>
        {/* Or target a specific conversation:
            inboxRef.current?.setTranslationEnabled(true, 'conversation_id') */}
      </div>
      <Inbox
        ref={inboxRef}
        appId="<APP_ID>"
        userId="sample_user_alice"
        style={{ width: '100%', height: '600px' }}
      />
    </>
  );
}

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 <Chatbox> immediately updates the last-message preview in <ConversationList>.
  • 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 { Chatbox, TranslationAttribution } from '@talkjs/react-components';
import '@talkjs/react-components/default.css';

function AfterMessages({ common }) {
  return (
    <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>
  );
}

export default function Chat() {
  return (
    <Chatbox
      appId="<APP_ID>"
      userId="sample_user_alice"
      conversationId="sample_conversation"
      showTranslationToggle
      theme={{ AfterMessages }}
      style={{ width: '100%', height: '600px' }}
    />
  );
}

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.