y18n

中文文档
5.0.8发布于 2021/04/08ISCESM + CJS零依赖近 30 天下载 7.8亿

the bare-bones internationalization library used by yargs

原始使用文档
文档目录12 节
  1. Examples
  2. Deno Example
  3. JSON Language Files
  4. Methods
  5. require('y18n')(config)
  6. y18n.__(str, arg, arg, arg)
  7. y18n.__n(singularString, pluralString, count, arg, arg, arg)
  8. y18n.setLocale(str)
  9. y18n.getLocale()
  10. y18n.updateLocale(obj)
  11. Supported Node.js Versions
  12. License

y18n

NPM version js-standard-style Conventional Commits

The bare-bones internationalization library used by yargs.

Inspired by i18n.

Examples

simple string translation:

const __ = require('y18n')().__;

console.log(__('my awesome string %s', 'foo'));

output:

my awesome string foo

using tagged template literals

const __ = require('y18n')().__;

const str = 'foo';

console.log(__`my awesome string ${str}`);

output:

my awesome string foo

pluralization support:

const __n = require('y18n')().__n;

console.log(__n('one fish %s', '%d fishes %s', 2, 'foo'));

output:

2 fishes foo

Deno Example

As of v5 y18n supports Deno:

import y18n from "https://deno.land/x/y18n/deno.ts";

const __ = y18n({
  locale: 'pirate',
  directory: './test/locales'
}).__

console.info(__`Hi, ${'Ben'} ${'Coe'}!`)

You will need to run with --allow-read to load alternative locales.

JSON Language Files

The JSON language files should be stored in a ./locales folder. File names correspond to locales, e.g., en.json, pirate.json.

When strings are observed for the first time they will be added to the JSON file corresponding to the current locale.

Methods

require('y18n')(config)

Create an instance of y18n with the config provided, options include:

  • directory: the locale directory, default ./locales.
  • updateFiles: should newly observed strings be updated in file, default true.
  • locale: what locale should be used.
  • fallbackToLanguage: should fallback to a language-only file (e.g. en.json) be allowed if a file matching the locale does not exist (e.g. en_US.json), default true.

y18n.__(str, arg, arg, arg)

Print a localized string, %s will be replaced with args.

This function can also be used as a tag for a template literal. You can use it like this: __`hello ${'world'}`. This will be equivalent to __('hello %s', 'world').

y18n.__n(singularString, pluralString, count, arg, arg, arg)

Print a localized string with appropriate pluralization. If %d is provided in the string, the count will replace this placeholder.

y18n.setLocale(str)

Set the current locale being used.

y18n.getLocale()

What locale is currently being used?

y18n.updateLocale(obj)

Update the current locale with the key value pairs in obj.

Supported Node.js Versions

Libraries in this ecosystem make a best effort to track Node.js' release schedule. Here's a post on why we think this is important.

License

ISC

版本与发布记录

y18n 共发布 22 个版本,首次发布于 2015/07/27;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.0.8latest2021/04/085 年前
4.0.3latest-42021/04/085 年前
5.0.72021/04/075 年前
4.0.22021/04/075 年前
5.0.62021/04/055 年前
3.2.2latest-32021/01/055 年前

其他发布通道:next → 4.0.0,engines-test → 6.0.0-alpha.0,latest-3 → 3.2.2,latest-4 → 4.0.3。

关于 y18n 的常见问题

y18n 是什么?

the bare-bones internationalization library used by yargs

如何安装 y18n?

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

y18n 支持 TypeScript 吗?

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

y18n 支持 ESM 还是 CommonJS?

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

y18n 需要什么版本的 Node.js?

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

y18n 可以商用吗?

y18n 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

y18n 的下载量有多少?

根据 npm 官方下载统计,y18n 在 2026/10/05 – 2026/11/03 这 30 天内被下载 784,097,669 次,日均约 26,136,589 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

y18n 还在维护吗?

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