# @alienfast/i18next-loader 中文文档

规范地址：https://npmcn.com/package/%40alienfast/i18next-loader/

版本：2\.0\.2
npm 元数据更新时间：2023-09-29T00:01:35.817Z
中文译文更新时间：2026-11-05T19:55:44.626Z
软件包许可：未声明

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40alienfast/i18next-loader
原文对照：https://npmcn.com/package/%40alienfast/i18next-loader/?lang=original
项目主页：https://github.com/alienfast/i18next-loader
源码仓库：https://github.com/alienfast/i18next-loader

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：23.7 kB，55 个文件
- 模块格式：CommonJS
- TypeScript 类型：未声明
- 运行依赖：3 个
- 首次发布：2017-09-24T18:58:10.509Z
- 最新版本发布：2023-09-29T00:01:35.568Z
- 近 30 天下载量：32664（2026-10-06 至 2026-11-04，npm 官方下载统计）
- 已发布版本数：25（近 12 个月 0 个）

## 中文简介

来自json或yaml的客户端嵌入式i18next locals的Webpack加载器.

## 安装

```sh
npm install @alienfast/i18next-loader
```

也可以使用：`pnpm add @alienfast/i18next-loader`、`yarn add @alienfast/i18next-loader`、`bun add @alienfast/i18next-loader`

## README

# @alienfast/i18n next-loader (英语)

[![npm version](https://badge.fury.io/js/%40alienfast%2Fi18next-loader.svg)](https://badge.fury.io/js/%40alienfast%2Fi18next-loader)

* * *

注:

我们把工具链换了用 `vite` 改为 `webpack`。虽然这个插件仍然可以工作,但我不太可能努力维持这个项目,但肯定会接受并合并拉动请求.

如果你还没有用过 `vite` 我鼓励你试试看! 和... 我迁移了这个插件, 与 `vite` 这样你就可以继续做同样的事情 你的地盘文件! 1.0.0 已经释放 [https://github.com/alienfast/vite-plugin-i18n-ext-loader (中文(简体) )](https://github.com/alienfast/vite-plugin-i18next-loader)。 。 。 .

* * *

`yarn add -D @alienfast/i18next-loader`

此网包加载器生成 `resources` a. 所需结构 [下一个i18](https://github.com/i18next/i18next)。该结构在构建时与客户端捆绑在一起,从而避免通过额外的HTTP请求来加载任何语言资源.

## 特征

-   [x] 基于 glob 文件过滤
-   [x] 支持再利用的例(白色标签)
-   [x] yaml 和 json 支持
-   [ ] 地方块 (帮助想要 - 见 [6号](https://github.com/alienfast/i18next-loader/issues/6)(中文(简体) )

鉴于一个本地目录, 默认情况下, 加载器会找到并分析任意 `json|yaml|yml` 文件并属性 包含的 lang 文件夹的内容, 例如 . `en`中。不需要添加诸如 `en` 或者说 `de` 在你体内 `json` 或者说 `yaml` 文档.

见 [`test/data` 目录](https://github.com/alienfast/i18next-loader/tree/develop/test/data) 用于结构和示例数据.

## 使用量

### 应用程序样本结构

```
└── app
    └── src
    │  └── app.js
    └── locales
       ├── index.js
       ├── de
       │   ├── foo.json
       │   └── bar.yaml
       └── en
           ├── foo.json
           └── bar.yaml
```

### 备选案文1:使用webpack.config.js(建议)

```javascript
module.exports = {
  // ... snip
  module: {
    rules: [
      {
        test: /locales/,
        loader: "@alienfast/i18next-loader",
        // options here
        //query: { overrides: [ '../node_modules/lib/locales' ] }
      },
    ],
  },
  // ... snip
};
```

```javascript
// File: app.js
import i18n from "i18next";
import resources from "../locales"; // typescript: import * as resources from '../locales'

i18n.init({
  resources,
});

// Use the resources as documented on i18next.com
i18n.t("key");
```

### 备选案文2:使用导入语法

```javascript
// File: app.js
import i18n from "i18next";
import resources from "@alienfast/i18next-loader!../locales/index.js";

i18n.init({
  resources,
});

// Use the resources as documented on i18next.com
i18n.t("key");
```

你完蛋了! 滚开! 这个 `index.js` 可以是空的,只需要将加载器指向地块的根目录.

## 高级使用

通过加载器设置选项 `query`。关于这一机制如何运作的更详细情况,见网页文件. 以下例子假设你理解这些值被用作 `query` 数值.

### 过滤文件

您可以通过指定所支持的任何 glob 来过滤文件结构中的文件 [`glob-all`](https://github.com/jpillora/node-glob-all)。 。 。 .

默认情况下,任何 `json|yaml|yml` 将会被载入.

#### 只有Json一个

```javascript
{
  include: ["**/*.json"];
}
```

#### 所有json 除了一个文件

```javascript
{
  include: ["**/*.json", "!**/excludeThis.json"];
}
```

### 覆盖/ 白色标签

复用库的应用程序,例如白色标签,可以使用一到多套本地目录 应用程序会覆盖.

```javascript
{
  overrides: ["../node_modules/lib1/locales"];
} // relative or absolute paths
```

此配置加载器可在如下文件结构上工作:

```
└── app
    ├── src
    │  └── app.js
    ├── locales
    │  ├── index.js
    │  └── en
    │      ├── foo.json
    │      └── bar.yaml
    └── node_modules
        └── lib1
            └── locales
               ├── index.js
               └── en
                   ├── foo.json
                   └── bar.yaml
```

一切从 `app/locales` 将覆盖一个库中指定的任何内容到许多库.

#### 使用文件底名作为 i18n 命名空间

```javascript
{
  basenameAsNamespace: true;
}
```

下述文件结构将导致资源加载如下:

```
└── app
    ├── src
    │  └── app.js
    └── locales
       ├── index.js
       └── en
           ├── foo.json
           └── bar.yaml
```

(原始内容存档于2018-09-26). foo.json

```
{
  "header": {
    "title": "TITLE"
  }
}
```

栏

```
footer:
  aboutUs: About us
```

装入对象中的结果 :

```
"en": {
  "foo": {
    "header": {
      "title":"TITLE"
    }
  },
  "bar": {
    "footer":{
      "aboutUs":"About us"
    }
  }
}
```

#### 使用相对路径作为 i18n 命名空间

```javascript
{
  relativePathAsNamespace: true;
}
```

下述文件结构将导致资源加载如下:

```
└── app
    └── locales
       ├── index.js
       └── en
           ├── green.yaml
           ├── blue
           ├──── foo.yaml
```

绿色. yaml

```
tree:
  species: Oak
```

蓝色/ foo.yaml

```
water:
  ocean: Quite large
```

装入对象中的结果 :

```
"en": {
  "green": {
    "tree": {
      "species":"Oak"
    }
  },
  "blue": {
    "foo":{
      "water": {
        "ocean": "Quite large"
      }
    }
  }
}
```

**注:** 如果您有同名的文件和文件夹, 您将使用 MGHT 覆盖其中之一 。 例如:

```
└── app
    └── locales
       ├── index.js
       └── en
           ├── blue.yaml
           ├── blue
           ├──── foo.yaml
```

蓝色的. yaml

```
foo: Welcome
```

蓝色/ foo.yaml

```
eggs: delicious
```

装入对象中的结果 :

```
"en": {
  "blue": {
    "foo": {
      "eggs": "delicious"
    }
  }
}
```

但它只是根据 返回值 `glob-all`所以,你不应该依赖它.

## 信贷

这是从 [i18next-resources-store-loader (i18n-next-resources-tore-loader) (英语)](https://github.com/atroo/i18next-resource-store-loader) 因为 我们换了 [打破不兼容的方式](https://github.com/atroo/i18next-resource-store-loader/issues/14#issuecomment-331726268)。 。 。 .  
感谢原作者和撰稿人.

## 常见问题

### @alienfast/i18next-loader 是什么？

来自json或yaml的客户端嵌入式i18next locals的Webpack加载器\.

### 如何安装 @alienfast/i18next-loader？

在项目目录运行 npm install @alienfast/i18next-loader，或使用 pnpm add / yarn add / bun add。

### @alienfast/i18next-loader 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/alienfast\_\_i18next-loader 是否存在。

### @alienfast/i18next-loader 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @alienfast/i18next-loader 可以商用吗？

@alienfast/i18next-loader 使用 未声明 许可。package\.json 没有声明许可证。没有许可证的代码默认保留全部权利，商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括，不构成法律意见，请以许可原文为准。

### @alienfast/i18next-loader 的下载量有多少？

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

### @alienfast/i18next-loader 还在维护吗？

最新版本 2\.0\.2 发布于 2023/09/29（3 年前）；至今共发布 25 个版本，其中过去 12 个月发布了 0 个；首次发布于 2017/09/25。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

