Instance properties
inputQuotaRead only Experimental-
The input quota available to the browser for generating translations.
sourceLanguageRead only Experimental-
The expected language of the input text to be translated.
targetLanguageRead only Experimental-
The language that the input text will be translated into.
Static methods
availability()Experimental-
Returns an enumerated value that indicates the availability of the AI model for the given
Translatorconfiguration. create()Experimental-
Creates a new
Translatorinstance from which to generate translations.
Instance methods
destroy()Experimental-
Releases the resources assigned to the
Translatorinstance it is called on and stops any further activity on it. measureInputUsage()Experimental-
Reports how much input quota would be used by a translation operation for a given text input.
translate()Experimental-
Returns a string containing a translation of the input string.
translateStreaming()Experimental-
Generates a translation of the input string 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
| Specification |
|---|
| Translator and Language Detector APIs # translator |