@ayub-begimkulov/i18n

中文文档
0.2.1发布于 2023/05/01MITTypeScriptESM + CJS零依赖近 30 天下载 1991

Small and type-safe package to create multi-language interfaces.

原始使用文档
文档目录13 节
  1. Installation
  2. Usage
  3. Basic example
  4. Using with React
  5. Recipes
  6. Reference
  7. I18N
  8. useI18n
  9. useTranslate
  10. createPluralize
  11. I18NProvider
  12. TaggedText
  13. License

@ayub-begimkulov/i18n

Small and type-safe package to create multi-language interfaces.

Features

  • Base i18n functionality (interpolation, rich text, pluralization).
  • Full TS support. They types are inferred, no need to write anything by hand.
  • React hooks and components.
  • Ability to load translations asynchronously (from the server or by lazy loading).
  • Ability to work with plurals format that is comfortable for you.

Installation

npm i @ayub-begimkulov/i18n

Usage

Basic example

import { I18N, createPluralize } from "@ayub-begimkulov/i18n";
import en from "./keys/en.json";
import ru from "./keys/ru.json";

const pluralizeEn = createPluralize("en");
const pluralizeRu = createPluralize("ru");

const i18n = new I18N({
  defaultLang: "en",
  languages: {
    en: {
      keyset: en,
      pluralize: pluralizeEn,
    },
    ru: {
      keyset: ru,
      pluralize: pluralizeRu,
    },
  },
});

i18n.get("greeting"); // Hello

Using with React

// i18n.ts
import {
  I18N,
  createPluralize,
  useTranslate as useTranslateBase,
  useI18n as useI18nBase,
} from "@ayub-begimkulov/i18n";
import en from "./keys/en.json";
import ru from "./keys/ru.json";

const pluralizeEn = createPluralize("en");
const pluralizeRu = createPluralize("ru");

const i18n = new I18N({
  defaultLang: "en",
  languages: {
    en: {
      keyset: en,
      pluralize: pluralizeEn,
    },
    ru: {
      keyset: ru,
      pluralize: pluralizeRu,
    },
  },
});

export const useTranslate = useTranslateBase<typeof i18n>;
export const useI18n = useI18nBase<typeof i18n>;

// index.tsx
import { I18NProvider } from "@ayub-begimkulov/i18n";
import { i18n } from "./i18n";

// ...

root.render(
  <I18NProvider i18n={i18n}>
    <App />
  </I18NProvider>
);

// component.ts
import { useTranslate } from "./i18n";

const Component = () => {
  const translate = useTranslate();

  return <div>{translate("some_key")}</div>;
};

Recipes

TODO

Reference

I18N

Class that is responsible for loading/storing translations and updating language. All the react functionality leverages I18N API to update components whenever needed.

Example:

import { I18N, createPluralize } from "@ayub-begimkulov/i18n";
import en from "./keys/en.json";
import ru from "./keys/ru.json";

const pluralizeEn = createPluralize("en");
const pluralizeRu = createPluralize("ru");

const i18n = new I18N({
  defaultLang: "en",
  languages: {
    en: {
      keyset: en,
      pluralize: pluralizeEn,
    },
    ru: {
      keyset: ru,
      pluralize: pluralizeRu,
    },
  },
});

i18n.get("greeting"); // 'Hello'

useI18n

A hook that returns an object with properties/methods of the i18n. Updates a component whenever the language changes.

Example:

const allLanguages = ["en", "ru", "ar"];

const Component = () => {
  const { lang, setLang } = useI18n();

  return (
    <>
      <div>{lang}</div>
      {allLanguages.map((lang) => (
        <div onClick={() => setLang(lang)}>Select {lang}</div>
      ))}
    </>
  );
};

useTranslate

A hook that returns a translate function. The component that uses this hook will update whenever the language changes.

Example:

import { useTranslate } from "@ayub-begimkulov/i18n";

const Component = () => {
  const t = useTranslate();

  return <div>{t("welcome")}</div>;
};

createPluralize

Creates a pluralize function for a given locale that will return a plural format for a specific number of items. Leverages Intl.PluralRules under the hood.

Example:

import { createPluralize } from "@ayub-begimkulov/i18n";

const pluralizeEn = createPluralize("en");

pluralizeEn(1); // 'one'
pluralizeEn(0); // 'other'

I18NProvider

A wrapper around React context provider. Used to share I18N instance across the components. Mostly used inside of the library hooks (useI18n/useTranslate).

Example:

import { I18NProvider } from '@ayub-begimkulov/i18n';
import { App } from './App';
import { i18n } form './i18n';

root.render(
  <I18NProvider i18n={i18n}>
    <App />
  </I18NProvider>
)

TaggedText

A component that allows to use React component inside of your translations.

Example:

import { TaggedText, useTranslate } from "@ayub-begimkulov/i18n";

export const Component = () => {
  const t = useTranslate();

  console.log(t("key")); // "<1>Important!</1> Check out <2>the project documentation</2> before using this library"

  return (
    <div>
      <TaggedText
        text={t("key")}
        tags={{
          1: (text) => <strong>{text}</strong>,
          2: (text) => <a>{text}</a>,
        }}
      />
    </div>
  );
};

License

MIT.

版本与发布记录

@ayub-begimkulov/i18n 共发布 9 个版本,首次发布于 2023/03/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.2.1latest2023/05/013 年前
0.2.02023/03/203 年前
0.1.12023/03/063 年前
0.1.02023/03/063 年前
0.0.52023/03/063 年前
0.0.42023/03/063 年前

关于 @ayub-begimkulov/i18n 的常见问题

@ayub-begimkulov/i18n 是什么?

Small and type-safe package to create multi-language interfaces.

如何安装 @ayub-begimkulov/i18n?

在项目目录运行 npm install @ayub-begimkulov/i18n,或使用 pnpm add / yarn add / bun add。

@ayub-begimkulov/i18n 支持 TypeScript 吗?

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

@ayub-begimkulov/i18n 支持 ESM 还是 CommonJS?

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

@ayub-begimkulov/i18n 可以商用吗?

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

@ayub-begimkulov/i18n 的下载量有多少?

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

@ayub-begimkulov/i18n 还在维护吗?

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