Translator
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The Translator
interface of the Translator and Language Detector APIs contains all the associated translation functionality, including checking AI model availability, creating a new Translator
instance, using it to create a translation, and more.
Instance properties
inputQuota
Read only Experimental-
The input quota available to the browser for generating translations.
sourceLanguage
Read only Experimental-
The expected language of the input text to be translated.
targetLanguage
Read only Experimental-
The language that the input text will be translated into.
Static methods
availability()
Experimental-
Returns an enumerated value that indicates whether the browser AI model supports a given
Translator
configuration. create()
Experimental-
Creates a new
Translator
instance from which to generate translations.
Instance methods
destroy()
Experimental-
Destroys the
Translator
instance it is called on. measureInputUsage()
Experimental-
Reports how much input quota would be used by a translation operation for a given text input.
translate()
Experimental-
Generates a new translation string.
translateStreaming()
Experimental-
Generates a new translation as a
ReadableStream
.
Examples
See Using the Translator and Language Detector APIs for a complete example.
Creating a Translator
instance
const translator = await Translator.create({
sourceLanguage: "en",
targetLanguage: "ja",
});
Generating a translation
const translation = await translator.translate(myTextString);
console.log(translation);
Generating a translation stream
const stream = translator.translateStreaming((myTextString);
let translation = "";
for await (const chunk of stream) {
translation += chunk;
}
console.log("Stream complete");
console.log(translation);
Specifications
No specification found
No specification data found for api.Translator
.
Check for problems with this page or contribute a missing spec_url
to mdn/browser-compat-data. Also make sure the specification is included in w3c/browser-specs.