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 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:
- Obtain and configure a Google API key
- Set the user's locale
- Enable translation in your Web Components
- If you don't have one yet, create a project in the Google Cloud Platform console.
- Make sure that billing is enabled. See Google's documentation on enabling billing for more info.
- Enable the Google Cloud Translation API for your project.
- Go to the credentials page, click Create credentials, and select API key.
- 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.
- Copy the API key.
- 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.
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.
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.
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.
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.
Call setTranslationEnabled directly on the <t-inbox>, <t-chatbox>, or <t-conversation-list> DOM element:
ChatboxElement.setTranslationEnabled(enabled): enables or disables translation for the conversation currently shown in<t-chatbox>.InboxElement.setTranslationEnabled(enabled, conversationId?): enables or disables translation forconversationId, or for the selected conversation (or conversation shown in the chatbox) if omitted.ConversationListElement.setTranslationEnabled(enabled, conversationId?): enables or disables translation forconversationId, or for the selected conversation if omitted.
<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>- Clicking the translation toggle or calling
setTranslationEnabledsaves the user's preference inlocalStorageper 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
defaultTranslationEnabledon future visits until it is changed again via the toggle orsetTranslationEnabled. - When translation is enabled for a conversation, new incoming messages and edited messages are translated automatically.
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.