useTranslator

Enables AI-powered text translation directly within the browser via the Translator API.

Installation

bash

Description

Facilitates text translation between different language pairs using on-device or browser-integrated AI models. It includes built-in methods to verify if specific language pairs (e.g., English to Spanish) are supported before attempting the translation.

Parameters

NameTypeDescription
options.sourceLanguagestringThe BCP 47 code for the source language. Required.
options.targetLanguagestringThe BCP 47 code for the target language. Required.
options.onLanguageSupportCheck(supported: boolean) => voidCallback triggered after checking if the language pair is usable. Default: undefined.

Return values

NameTypeDescription
checkLanguageSupport() => Promise<boolean>Manually triggers a check for the configured language pair.
errorError | nullCaptures API errors or unsupported language exceptions.
isLanguagePairSupportedboolean | nullResult of the support check (null if not yet checked).
isSupportedbooleantrue if the browser supports the window.translator API.
isTranslatingbooleantrue while the AI model is processing the translation.
translate(text: string) => Promise<string>Triggers the translation process for the provided text.
translationstring | nullThe most recent translation result.

Demo

This component is loading.

Source code

tsx