Skip to main content
Version: Next

Function: useTranslator()

useTranslator<C>(config, options?): object

Defined in: hooks/useTranslator.ts:39

React hook to load and run a translation pipeline: Marian (Opus-MT), one model per language pair, or an LLM (EuroLLM, Hy-MT2) that translates between any two of its languages.

This hook manages downloading (if remote URLs are provided) and loading the model and tokenizer, tracking download progress and load errors, and releasing native memory when the component unmounts or the configuration changes.

For imperative usage, see createMarianTranslator and createLLMTranslator.

Type Parameters​

C​

C extends TranslatorModel

Parameters​

config​

C

The translation model configuration. See TranslatorModel.

options?​

ResourceOptions

Load and caching options. See ResourceOptions.

Returns​

The same object as Translator (without dispose), combined with loading state and download progress.

downloadProgress​

downloadProgress: number

Download progress across every asset, in percent.

error​

error: Error | undefined

The download or load error, if any.

isReady​

isReady: boolean

Whether the pipeline is loaded and ready to translate.

resource​

resource: C | undefined

The config with every remote URL resolved to a local path.

translate​

translate: (text, languages, onToken?) => Promise<string> | undefined

Translates text. Undefined until the pipeline is ready.

translateStop​

translateStop: () => void | undefined

Cancels an in-flight translation. Undefined until the pipeline is ready.

See​

Translator