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 React 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.
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.
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.
Use a ref to call setTranslationEnabled on the component controller:
ChatboxController.setTranslationEnabled(enabled): enables or disables translation for the conversation currently shown in the<Chatbox>.InboxController.setTranslationEnabled(enabled, conversationId?): enables or disables translation forconversationId, or for the selected conversation (or conversation shown in the chatbox) if omitted.ConversationListController.setTranslationEnabled(enabled, conversationId?): enables or disables translation forconversationId, or for the selected conversation if omitted.
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' }}
/>
</>
);
}- 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
<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
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 { 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.