Skip to main content
Call window.translate() to translate the page to any supported language without requiring the user to interact with the widget UI. This is useful when you want to trigger translation from your own code — for example, from a custom language picker, a URL change handler, or on page load.

Signature

window.translate(
  langCode: string,
  onComplete?: (result: TranslationResult) => void,
  onError?: (error: Error) => void
): Promise<TranslationResult>

Parameters

langCode
string
required
BCP 47 language code of the target language. Examples: "hi" (Hindi), "fr" (French), "es" (Spanish), "zh" (Chinese).
onComplete
function
Callback called when the translation finishes successfully. Receives a TranslationResult object with details about the completed translation.
onError
function
Callback called if the translation fails. Receives an Error object describing what went wrong.

Return value

Returns a Promise<TranslationResult>. You can either await the promise or use the onComplete/onError callbacks — both are supported.
success
boolean
required
Whether the translation completed successfully.
targetLanguage
string
required
The language code that was translated to.
translatedNodes
number
required
Number of DOM nodes that were translated on the page.
error
string
Error message if the translation failed. Only present when success is false.
duration
number
Time taken to complete the translation, in milliseconds.

Examples

// Translate to Hindi
window.translate(
  "hi",
  (res) => {
    console.log(res);
  },
  (err) => {
    console.error(err);
  }
);
Make sure the widget is initialized before calling window.translate(). If the widget has not been initialized, the call will fail with "Translation widget not initialized".
If a translation is already in progress, the call will fail immediately with "Translation already in progress". Wait for the current translation to complete before calling window.translate() again.
Use window.translate() together with showUI: false to build a fully custom language selector UI without rendering the default widget trigger button.

Build docs developers (and LLMs) love