@alienfast/i18next-loader

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

Webpack loader for client embedded i18next locales from json or yaml.

原始使用文档
文档目录9 节
  1. Features
  2. Usage
  3. Sample app structure
  4. Option 1: use with webpack.config.js (recommended)
  5. Option 2: use with import syntax
  6. Advanced Usage
  7. Filtering files
  8. Overriding/White labeling
  9. Credit

@alienfast/i18next-loader

npm version


MAINTENANCE NOTE:

We have switched our toolchain to use vite instead of webpack. While this plugin will still work, I'm not likely to be putting effort into maintaining this project, but will certainly accept and merge pull requests.

If you haven't used vite yet, I encourage you to try it! AND...I have migrated this plugin to work with vite so you can keep doing the same thing with your locale files! 1.0.0 has been released https://github.com/alienfast/vite-plugin-i18next-loader.


yarn add -D @alienfast/i18next-loader

This webpack loader generates the resources structure necessary for i18next. The structure is webpacked with the client bundle at build time, thus avoiding loading any language resources via extra HTTP requests.

Features

  • glob based file filtering
  • one to many overrides supporting reuse cases (white labeling)
  • yaml and json support
  • locale chunking (help wanted - see #6)

Given a locales directory, by default, the loader will find and parse any json|yaml|yml file and attribute the contents to the containing lang folder e.g. en. There is no need to add lang such as en or de inside your json or yaml files.

See the test/data directory for structure and example data.

Usage

Sample app structure

└── 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");

Option 2: use with import syntax

// 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");

And you're done! The index.js can be empty, it's just needed to point the loader to the root directory of the locales.

Advanced Usage

Options are set via the loader query. See webpack documentation for more details regarding how this mechanism works. The following examples assume you understand these values are used as the query value.

Filtering files

You can filter files in your file structure by specifying any glob supported by glob-all.

By default, any json|yaml|yml will be loaded.

Only json

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

All json except one file

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

Overriding/White labeling

Applications that reuse libraries e.g. white labeling, can utilize one to many sets of locale directories that the app will override.

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

This configures the loader to work on a file structure like the following:

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

Everything from app/locales will override anything specified in one to many libraries.

Use file basename as the i18next namespace

{
  basenameAsNamespace: true;
}

The following file structure would result in resources loaded as below:

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

foo.json

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

bar.yaml

footer:
  aboutUs: About us

Results in this object loaded:

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

Use relative path as the i18next namespace

{
  relativePathAsNamespace: true;
}

The following file structure would result in resources loaded as below:

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

green.yaml

tree:
  species: Oak

blue/foo.yaml

water:
  ocean: Quite large

Results in this object loaded:

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

NOTE: If you have a file and a folder with the same name, you MIGHT overwrite one with the other. For example:

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

blue.yaml

foo: Welcome

blue/foo.yaml

eggs: delicious

Results in this object loaded:

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

But it's just overwriting based on the return value of glob-all, so you shouldn't depend on it.

Credit

This was forked from i18next-resource-store-loader because we changed it in breaking ways that are incompatible.
Thanks to the original authors and contributors.

版本与发布记录

@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 是什么?

Webpack loader for client embedded i18next locales from json or yaml.

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