@denysvuika/preact-translate

中文文档
0.6.0发布于 2025/05/26MITTypeScriptESM + CJS零依赖近 30 天下载 5.6万

Minimalistic translate (i18n) library for Preact

原始使用文档
文档目录7 节
  1. Installing
  2. Basic usage
  3. Configuring assets root folder
  4. Formatted translations
  5. Nested translations
  6. Default language
  7. Custom translation data

preact-translate

Minimalistic translate (i18n) library for Preact
Bundle size: ~1KB

Buy Me A Coffee

Installing

Minimal requirements: Preact 10.0.0

Using Yarn:

yarn add @denysvuika/preact-translate

Using NPM:

npm i @denysvuika/preact-translate

Basic usage

You should wrap your application with the TranslateProvider component:

import { TranslateProvider } from '@denysvuika/preact-translate';

export default function App() {
  return (
    <TranslateProvider>
      <MainComponent />
    </TranslateProvider>
  );
}

Create an assets/en.json file with the following content:

{
  "title": "Hello",
  "subtitle": "World"
}

Create a second file assets/ua.json to be able to switch between different resources.

{
  "title": "[ua] Hello",
  "subtitle": "[ua] World"
}

You can now use the TranslateContext in your components to access the translation API and data:

import { useContext } from 'preact/hooks';
import { TranslateContext } from '@denysvuika/preact-translate';

export default function MainComponent() {
  const { setLang, t, lang } = useContext(TranslateContext);

  return (
    <div>
      <div>Lang: {lang}</div>
      <div>{t('title')}</div>
      <div>{t('subtitle')}</div>
      <div>
        <button onClick={() => setLang('en')}>EN</button>
        <button onClick={() => setLang('ua')}>UA</button>
      </div>
    </div>
  );
}

For the EN locale you should see:

Lang: en
Hello
World

For the UA locale you should see:

Lang: ua
[ua] Hello
[ua] World

The language loading performs on demand. The en.json gets loaded and cached only when first requested by your application.

Configuring assets root folder

By default, the assets folder is used to fetch locales. You can change it via the TranslateProvider.root property:

import { TranslateProvider } from '@denysvuika/preact-translate';

export default function App() {
  return (
    <TranslateProvider root="i18n">
      <MainComponent />
    </TranslateProvider>
  );
}

Formatted translations

You can use runtime string substitution when translating text

{
  "hello": "Hello, {name}"
}

Then in the JSX:

<div>{t('hello', { name: 'Bob' })}</div>

Nested translations

The library supports complex objects with nested levels.

Put the following in the en.json file:

{
  "messages": {
    "errors": {
      "404": "Sorry, not found"
    }
  }
}

Then in the JSX use:

<div>{t('messages.errors.404')}</div>

You can also use composite strings like the following:

{
  "messages.errors.404": "Sorry, not found"
}

Default language

You can set the default language to use with the application by assigning the TranslateProvider.lang property.

<TranslateProvider lang="ua">
  <Application />
</TranslateProvider>

Please note that in this case provider is going to load and cache two locales at startup: en.json (as a fallback) and ua.json (as an active lang).

Custom translation data

You can use TranslateProvider.translations property to provide a custom translation data from the code. That helps with unit testing as well.

const data = {
  en: {
    messages: {
      404: 'Not found'
    }
  }
};

<TranslateProvider translations={data}>
  <Application />
</TranslateProvider>;

Note that the TranslateProvider is not going to fetch translation files for the en locale, and will use your custom data instead.

版本与发布记录

@denysvuika/preact-translate 共发布 14 个版本,首次发布于 2019/07/18;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.6.0latest2025/05/261.5 年前
0.5.12024/12/151.9 年前
0.5.02023/03/063 年前
0.4.12022/06/244 年前
0.3.02020/06/066 年前
0.2.62020/04/156 年前

关于 @denysvuika/preact-translate 的常见问题

@denysvuika/preact-translate 是什么?

Minimalistic translate (i18n) library for Preact

如何安装 @denysvuika/preact-translate?

在项目目录运行 npm install @denysvuika/preact-translate,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:preact(>=10.13.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@denysvuika/preact-translate 支持 TypeScript 吗?

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

@denysvuika/preact-translate 支持 ESM 还是 CommonJS?

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

@denysvuika/preact-translate 可以商用吗?

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

@denysvuika/preact-translate 的下载量有多少?

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

@denysvuika/preact-translate 还在维护吗?

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