@ef-carbon/fluent-langneg

中文文档
0.7.0发布于 2025/11/24Apache-2.0TypeScriptESM + CJS零依赖近 30 天下载 5134

Language Negotiation API for Fluent

原始使用文档
文档目录8 节
  1. Installation
  2. How to use
  3. Strategies
  4. filtering (default)
  5. matching
  6. lookup
  7. API use:
  8. Likely subtags

@fluent/langneg

@fluent/langneg provides an API for negotiating languages. It's part of Project Fluent. Its main function is to provide functionality around the Intl.Locale API with a focus on language negotiation, matching and selection.

Installation

@fluent/langneg can be used both on the client-side and the server-side. You can install it from the npm registry or use it as a standalone script.

npm install @fluent/langneg

How to use

import { negotiateLanguages } from "@fluent/langneg";

const supportedLocales = negotiateLanguages(
  navigator.languages, // requested locales
  ["de", "en-US", "pl"], // available locales
  { defaultLocale: "en-US" }
);

The API reference is available at https://projectfluent.org/fluent.js/langneg.

Strategies

The API supports three negotiation strategies:

filtering (default)

In this strategy the algorithm will try to match as many available locales as possible for each of the requested locale.

Example:

requested: ['de-DE', 'fr-FR'] available: ['it', 'de', 'en-US', 'fr-CA', 'de-DE', 'fr', 'de-AU']

supported: ['de-DE', 'de', 'fr', 'fr-CA']

matching

In this strategy the algorithm will look for the best matching available locale for each requested locale.

Example:

requested: ['de-DE', 'fr-FR'] available: ['it', 'de', 'en-US', 'fr-CA', 'de-DE', 'fr', 'de-AU']

supported: ['de-DE', 'fr']

lookup

In this strategy the algorithm will try to find the single best locale for the requested locale list among the available locales.

Example:

requested: ['de-DE', 'fr-FR'] available: ['it', 'de', 'en-US', 'fr-CA', 'de-DE', 'fr', 'de-AU']

supported: ['de-DE']

API use:

let supported = negotiateLanguages(requested, available, {
  strategy: "matching",
});

Likely subtags

Fluent Language Negotiation module carries its own minimal list of likely subtags data, which is useful in finding most likely available locales in case the requested locale is too generic.

An example of that scenario is when the user requests en locale, and the application supports en-GB and en-US.

版本与发布记录

@ef-carbon/fluent-langneg 共发布 1 个版本,首次发布于 2025/11/24;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.7.0latest2025/11/2411 个月前

关于 @ef-carbon/fluent-langneg 的常见问题

@ef-carbon/fluent-langneg 是什么?

Language Negotiation API for Fluent

如何安装 @ef-carbon/fluent-langneg?

在项目目录运行 npm install @ef-carbon/fluent-langneg,或使用 pnpm add / yarn add / bun add。

@ef-carbon/fluent-langneg 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ef-carbon/fluent-langneg 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@ef-carbon/fluent-langneg 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=14.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ef-carbon/fluent-langneg 可以商用吗?

@ef-carbon/fluent-langneg 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@ef-carbon/fluent-langneg 的下载量有多少?

根据 npm 官方下载统计,@ef-carbon/fluent-langneg 在 2026/10/13 – 2026/11/11 这 30 天内被下载 5,134 次,日均约 171 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ef-carbon/fluent-langneg 还在维护吗?

最新版本 0.7.0 发布于 2025/11/24(11 个月前);至今共发布 1 个版本,其中过去 12 个月发布了 1 个;首次发布于 2025/11/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。