@fluent/dom

中文文档
0.10.2发布于 2025/07/18Apache-2.0TypeScriptESM + CJS近 30 天下载 3.3万

Fluent bindings for DOM

原始使用文档

@fluent/dom

@fluent/dom provides the DOM bindings for Project Fluent.

Installation

@fluent/dom 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 (as the FluentDOM global).

npm install @fluent/dom

How to use

The DOMLocalization constructor provides the core functionality of full-fallback ready message formatting. It uses a lazy-resolved FluentBundle objects from the @fluent/bundle package to format messages.

On top of that, DOMLocalization can localize any DOMFragment by identifying localizable elements with data-l10n-id and translating them.

import { DOMLocalization } from "@fluent/dom";

const l10n = new DOMLocalization(
  ["/browser/main.ftl", "/toolkit/menu.ftl"],
  generateBundles
);

l10n.connectRoot(document.documentElement);

l10n.translateRoots();

const h1 = document.querySelector("h1");

// Sets `data-l10n-id` and `data-l10n-args` which triggers
// the `MutationObserver` from `DOMLocalization` and translates the
// element.
l10n.setAttributes(h1, "welcome", { user: "Anna" });

For imperative uses straight from the JS code, there's also a Localization class that provides just the API needed to format messages in the running code.

import { Localization } from "@fluent/dom";

function* generateBundles() {
  // Some lazy logic for yielding FluentBundles.
  yield* [bundle1, bundle2];
}

const l10n = new Localization(
  document,
  ["/browser/main.ftl", "/toolkit/menu.ftl"],
  generateBundles
);

async function main() {
  const msg = await l10n.formatValue("welcome", { name: "Anna" });
  // → 'Welcome, Anna!'
}

版本与发布记录

@fluent/dom 共发布 9 个版本,首次发布于 2019/07/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.10.2latest2025/07/181.3 年前
0.10.12025/03/281.6 年前
0.10.02024/06/252 年前
0.9.02023/03/133 年前
0.8.12021/12/224 年前
0.8.02021/09/135 年前

关于 @fluent/dom 的常见问题

@fluent/dom 是什么?

Fluent bindings for DOM

如何安装 @fluent/dom?

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

@fluent/dom 支持 TypeScript 吗?

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

@fluent/dom 支持 ESM 还是 CommonJS?

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

@fluent/dom 需要什么版本的 Node.js?

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

@fluent/dom 可以商用吗?

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

@fluent/dom 的下载量有多少?

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

@fluent/dom 还在维护吗?

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