The Language Detector API identifies the most likely language a given text is written in, returning ranked BCP 47 language tags with confidence scores — on-device.

LanguageDetector.availability()

Checks if the user agent can support language detection. If expectedInputLanguages is provided, it specifically checks if those languages are supported.

staticavailability(options?: LanguageDetectorCreateCoreOptions): Promise<Availability>;

Parameters (LanguageDetectorCreateCoreOptions)

PropertyTypeDescription
expectedInputLanguagessequence<DOMString>Optional
An array of BCP 47 language tags that the application expects to detect.

Returns

A promise resolving to 'available', 'downloadable', 'downloading', or 'unavailable'.

Interactive Playground

LanguageDetector.create()

Instantiates a new LanguageDetector object, initiating any necessary model downloads.

staticcreate(options?: LanguageDetectorCreateOptions): Promise<LanguageDetector>;

Parameters (LanguageDetectorCreateOptions)

PropertyTypeDescription
expectedInputLanguagessequence<DOMString>Optional
Helps the user agent download specific models necessary to support these languages.
signalAbortSignalOptional
Allows aborting the creation and download process.
monitorCreateMonitorCallbackOptional
Callback to listen to downloadprogress events.
Interactive Playground

detector.detect()

Detects the language of the input string. Returns an array of candidate results sorted in descending order of confidence. Extremely low confidence results are excluded. The final entry in the array is always for the undetermined language ('und').

detect(input: DOMString, options?: LanguageDetectorDetectOptions): Promise<sequence<LanguageDetectionResult>>;

Parameters

PropertyTypeDescription
inputDOMStringThe string to analyze.
options.signalAbortSignalOptional
Signal to abort the detection request.

Returns (LanguageDetectionResult)

A promise that resolves to an array of objects containing detectedLanguage (a BCP 47 string) and confidence (a double between 0 and 1).

Interactive Playground

detector.measureInputUsage()

Calculates how much quota the string would consume without executing the detection. If this returns a value greater than inputQuota, the returned promise will reject with a QuotaExceededError.

measureInputUsage(input: DOMString, options?: LanguageDetectorDetectOptions): Promise<double>;
Interactive Playground

detector.destroy()

Destroys the language detector instance, aborting any active requests and allowing the browser to unload the underlying machine learning models from memory.

destroy(): undefined;
Interactive Playground

Properties

PropertyTypeDescription
expectedInputLanguagesFrozenArray<DOMString> | null (readonly)The array of languages expected to be detected, provided during creation.
inputQuotaunrestricted double (readonly)The maximum input usage allowed per detection operation. Can be +Infinity if the implementation imposes no input limit (e.g. because it processes input in chunks); in that case measureInputUsage() always returns 0.