大网数据广告活动
Web 标准与前端开发

LanguageDetector

· 52CE 我爱测

The **`LanguageDetector`** interface of the Translator and Language Detector APIs contains all the language detection functionality, including checking AI model availability, creating a new `LanguageDetector` instance, using it to detect a language, and more.

Translator and Language Detector APIs

The LanguageDetector interface of the Translator and Language Detector APIs contains all the language detection functionality, including checking AI model availability, creating a new LanguageDetector instance, using it to detect a language, and more.

Instance properties

  • LanguageDetector.inputQuota
    • : The input quota available to the browser for detecting languages.
  • LanguageDetector.expectedInputLanguages
    • : The expected languages to be detected in the input text.

Static methods

  • LanguageDetector.availability_static
    • : Returns an enumerated value that indicates whether the browser AI model supports a given LanguageDetector configuration.
  • LanguageDetector.create_static
    • : Creates a new LanguageDetector instance to detect languages.

Instance methods

  • LanguageDetector.destroy
    • : Releases the resources assigned to the LanguageDetector instance it is called on and stops any further activity on it.
  • LanguageDetector.detect
    • : Detects the closest matching language or languages that a given text string is most likely to be written in.
  • LanguageDetector.measureInputUsage
    • : Reports how much input quota would be used by a language detection operation for a given text input.

Examples

See Using the Translator and Language Detector APIs for a complete example.

Creating a LanguageDetector instance

const detector = await LanguageDetector.create({
  expectedInputLanguages: ["en-US", "zh"],
});

[!NOTE] Different implementations will likely support different languages.

Detecting languages

const results = await detector.detect(myTextString);

results.forEach((result) => {
  console.log(`${result.detectedLanguage}: ${result.confidence}`);
});

// Results in logs like this:
// la: 0.8359838724136353
// es: 0.017705978825688362
// sv: 0.012977192178368568
// en: 0.011148443445563316

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。