web api static method
Translator: create() static method
Limited availabilitySecure context
The create() static method of the Translator interface creates a new Translator instance that can be used to translate text.
[!NOTE] The
create()method requires transient activation, that is, it must be invoked in response to a user action such as a mouse click or button press.
Syntax
Translator.create(options)
Parameters
options- : An object specifying configuration options for the
Translator. Possible values include:sourceLanguage- : A string specifying the expected language of the input text to be translated, which should be a valid BCP 47 language tag.
targetLanguage- : A string specifying the language that the input text will be translated into, which should be valid BCP 47 language tag.
monitorOptional- : A callback function with a
CreateMonitorargument that enables monitoring download progress of the AI model.
- : A callback function with a
signalOptional- : An
AbortSignalobject instance, which allows acreate()operation to be aborted via the associatedAbortController. The exact effect is dependent on whenabort()is called:- If
abort()is called before thecreate()promise resolves, thecreate()operation is cancelled. - If
abort()is called after thecreate()promise fulfills, it has the same effect as callingdestroy(): The resources assigned to the resultingTranslatorinstance are released, and any ongoing and subsequentTranslatormethod calls will reject with anAbortError.
- If
- : An
- : An object specifying configuration options for the
Return value
A Promise that fulfills with a Translator object instance.
Exceptions
InvalidStateErrorDOMException- : Thrown if the page’s
Documentis not yet active.
- : Thrown if the page’s
NetworkErrorDOMException- : Thrown if:
- The network was not available to download the AI model.
- The user has cancelled the AI model download.
- : Thrown if:
NotAllowedErrorDOMException- : Thrown if:
- Usage of the method is blocked by a
translatorPermissions-Policy. - The user has blocked the AI model download in some way.
- The
create()method wasn’t called via transient activation.
- Usage of the method is blocked by a
- : Thrown if:
NotSupportedErrorDOMException- : Thrown if:
- The language tags specified in
sourceLanguageortargetLanguageare invalid, or not supported. This is the case if the specifiedsourceLanguageandtargetLanguageare the same. - An AI model to support the specified combination of
sourceLanguageandtargetLanguageis not available.
- The language tags specified in
- : Thrown if:
OperationErrorDOMException- : General-purpose exception thrown if
Translatorcreation failed for any other reason.
- : General-purpose exception thrown if
Examples
Basic Translator creation
const translator = await Translator.create({
sourceLanguage: "en",
targetLanguage: "ja",
});
Specifications
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.