Translator
Translator and Language Detector APIs
Translator and Language Detector APIs
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
- : The input quota available to the browser for generating translations.
- sourceLanguage
- : The expected language of the input text to be translated.
- targetLanguage
- : The language that the input text will be translated into.
## Static methods
- availability()
- : Returns an enumerated value that indicates the availability of the AI model for the given `Translator` configuration.
- create()
- : Creates a new `Translator` instance from which to generate translations.
## Instance methods
- destroy()
- : Releases the resources assigned to the `Translator` instance it is called on and stops any further activity on it.
- measureInputUsage()
- : Reports how much input quota would be used by a translation operation for a given text input.
- translate()
- : Returns a string containing a translation of the input string.
- translateStreaming()
- : Generates a translation of the input string as a ReadableStream.
## Examples
See [Using the Translator and Language Detector APIs](/docs/Web/API/Translator_and_Language_Detector_APIs/Using) for a complete example.
### Creating a `Translator` instance
```js
const translator = await Translator.create({
sourceLanguage: "en",
targetLanguage: "ja",
});
```
### Generating a translation
```js
const translation = await translator.translate(myTextString);
console.log(translation);
```
### Generating a translation stream
```js
const stream = translator.translateStreaming(myTextString);
let translation = "";
for await (const chunk of stream) {
translation += chunk;
}
console.log("Stream complete");
console.log(translation);
```
## Specifications
## Browser compatibility
## See also
- [Using the Translator and Language Detector APIs](/docs/Web/API/Translator_and_Language_Detector_APIs/Using)
Cite this
"Translator". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/API/Translator JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy