@alienfast/i18next-loader

中文文档
2.0.2发布于 2023/09/29CommonJS近 30 天下载 3.3万

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

中文使用文档
文档目录9 节
  1. 特征
  2. 使用量
  3. 应用程序样本结构
  4. 备选案文1:使用webpack.config.js(建议)
  5. 备选案文2:使用导入语法
  6. 高级使用
  7. 过滤文件
  8. 覆盖/ 白色标签
  9. 信贷

@alienfast/i18n next-loader (英语)

npm version


注:

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

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


yarn add -D @alienfast/i18next-loader

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

特征

  • 基于 glob 文件过滤
  • 支持再利用的例(白色标签)
  • yaml 和 json 支持
  • 地方块 (帮助想要 - 见 6号(中文(简体) )

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

见 test/data 目录 用于结构和示例数据.

使用量

应用程序样本结构

└── app
    └── src
    │  └── app.js
    └── locales
       ├── index.js
       ├── de
       │   ├── foo.json
       │   └── bar.yaml
       └── en
           ├── foo.json
           └── bar.yaml
module.exports = {
  // ... snip
  module: {
    rules: [
      {
        test: /locales/,
        loader: "@alienfast/i18next-loader",
        // options here
        //query: { overrides: [ '../node_modules/lib/locales' ] }
      },
    ],
  },
  // ... snip
};
// 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:使用导入语法

// 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。 。 。 .

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

只有Json一个

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

所有json 除了一个文件

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

覆盖/ 白色标签

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

{
  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 命名空间

{
  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 命名空间

{
  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) (英语) 因为 我们换了 打破不兼容的方式。 。 。 .
感谢原作者和撰稿人.

版本与发布记录

@alienfast/i18next-loader 共发布 25 个版本,首次发布于 2017/09/25;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.2latest2023/09/293 年前
2.0.2--canary.26.2586783.0canary2023/09/293 年前
2.0.12022/11/083 年前
2.0.1--canary.24.582e5cf.02022/11/083 年前
1.1.42019/10/107 年前
1.1.22019/10/107 年前

关于 @alienfast/i18next-loader 的常见问题

@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 和提交记录判断。