Translator: create() static method
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The create()
static method of the Translator
interface creates a new Translator
instance from which to generate translations.
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 (as specified in RFC 5646). Defaults to
en
. targetLanguage
-
A string specifying the language that the input text will be translated into, which should be valid BCP 47 language tag. Defaults to
en
. monitor
Optional-
A callback function with a
CreateMonitor
argument that enables monitoring download progress of the AI model. signal
Optional-
An
AbortSignal
object instance, which allows thecreate()
operation to be aborted via the associatedAbortController
.
Return value
A Promise
that fulfills with a Translator
object instance.
Exceptions
InvalidStateError
DOMException
-
Thrown if the page's
Document
is not yet active. NetworkError
DOMException
-
Thrown in any of the following circumstances:
- The network was not available to download the AI model.
- The user has cancelled the AI model download.
NotAllowedError
DOMException
-
Thrown in any of the following circumstances:
- Usage of the method is blocked by a
translator
Permissions-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
NotSupportedError
DOMException
-
Thrown in any of the following circumstances:
- The language tags specified in
sourceLanguage
ortargetLanguage
are invalid, or not supported. - An AI model to support the specified combination of
sourceLanguage
andtargetLanguage
is not available.
- The language tags specified in
OperationError
DOMException
-
General-purpose exception thrown if
Translator
creation failed for any other reason.
Examples
Basic Translator
creation
const translator = await Translator.create({
sourceLanguage: "en",
targetLanguage: "ja",
});
Specifications
No specification found
No specification data found for api.Translator.create_static
.
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.