npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

soon-i18n-react

v2.0.0

Published

a lightweight nested messages i18n library with smart ts prompt can be used in react , vue , svelte , solid , etc...

Downloads

60

Readme

soon-i18n-react v2.0

React adapter for soon-i18n, providing hooks-based i18n integration with full TypeScript support and lazy loading capabilities.

Full Document

中文文档

Install

npm install soon-i18n-react

Full Example

soon-admin-react-nextjs or

npx degit https://github.com/leafio/soon-i18n/examples/react-demo

Instance Usage

Create an instance

// lang/index.ts
import { createI18n } from "soon-i18n-react";

const globalLocales = {
  zh: { g_welcome: "全局:欢迎 {name}", common: { title: "标题" } },
  en: { g_welcome: "Global: Welcome {name}", common: { title: "Title" } },
} as const;

type Lang = "zh" | "en";

export const { tLocales, useLocales, useLang, getLang, setLang } = createI18n(
  { lang: () => "zh", fallbacks: (curLang, lastLang) => ["en"] },
  globalLocales
);

Use in JS/TS (Sync only)

import { tLocales } from "../lang";

// tLocales only supports synchronous loading
export const showToast = () => {
  const t = tLocales({
    zh: { tip: "哈哈,一条中文提醒!!!" },
    en: { tip: "Aha, an English tip" },
  });
  alert(t("tip"));
};

// For lazy loading, use useLocales in components

Use in components

Synchronous Loading

import { useLocales } from "../lang";

const Content = () => {
  const [t, inited] = useLocales({
    zh: { hello: "你好" },
    en: { hello: "Hello" },
  });

  if (!inited) {
    return <div>Loading translations...</div>;
  }

  return (
    <div>
      <h1>{t("common.title")}</h1>
      <p>{t("hello")}</p>
      <p>{t("g_welcome", { name: "张三" })}</p>
    </div>
  );
};

export default Content;

Lazy Loading

import { useLocales } from "../lang";

const LazyComponent = () => {
  const [t, inited] = useLocales({
    zh: { welcome: "欢迎" },
    en: () => import("./locales/en"), // Dynamic import
    ja: () => fetch("/api/translations/ja")
      .then(res => res.json())
      .then(data => ({ default: data })), // API fetch
  });

  if (!inited) {
    return <div>Loading translations...</div>;
  }

  return (
    <div>
      <h2>{t("welcome")}</h2>
      <p>{t("common.title")}</p>
    </div>
  );
};

export default LazyComponent;

Change lang

import { useLang } from "../lang";

const SwitchLang = () => {
  const [lang, setLang] = useLang();

  return (
    <div>
      <p>Current language: {lang}</p>
      <button onClick={() => setLang(lang === "en" ? "zh" : "en")}>
        Switch language
      </button>
    </div>
  );
};

export default SwitchLang;

API Reference

createI18n(config, globalLocales?)

Creates an i18n instance with React-specific hooks.

Parameters:

  • config: Configuration object
    • lang: () => Lang: Function to get current language
    • fallbacks?: (curLang, lastLang) => Lang[]: Fallback languages function
  • globalLocales?: Global translation resources object

Returns: Object with methods:

  • useLocales(locales?): Hook for translations with local resources
  • useLang(): Hook for getting/setting current language
  • tLocales(locales?): Function for translations without reactivity
  • getLang(): Get current language
  • setLang(lang): Set current language

useLocales(locales?)

React hook for using translations with optional local resources.

Parameters:

  • locales?: Local translation resources (supports sync/async)

Returns: [t, inited] tuple

  • t: Translation function with full type safety
  • inited: Boolean indicating if all translations are loaded

useLang()

React hook for getting and setting current language.

Returns: [lang, setLang] tuple

  • lang: Current language
  • setLang: Function to change language

tLocales(locales?)

Create translator without React reactivity (for non-component code).

Parameters:

  • locales?: Local translation resources (only synchronous)

Returns: Translation function

Type Safety

soon-i18n-react uses SafeLocales type to ensure translation keys exist in all languages. Here are different scenarios:

✅ Case 1: All keys exist in all languages

// Type-safe - all keys exist in both languages
const t = tLocales({
  zh: { button: { save: "保存", cancel: "取消" } },
  en: { button: { save: "Save", cancel: "Cancel" } }
});

// ✅ This works fine
t("button.save");  // OK
t("button.cancel"); // OK

❌ Case 2: Different keys in different languages

// Type error - keys don't match
const t = tLocales({
  zh: { button: { save: "保存", cancel: "取消" } },
  en: { button: { save: "Save" } } // ❌ Missing cancel in English
});

// ❌ TypeScript will show error
t("button.cancel"); // Type error

❌ Case 3: Non-existent key

const t = tLocales({
  zh: { button: { save: "保存" } },
  en: { button: { save: "Save" } }
});

// ✅ This works
t("button.save"); // OK

// ❌ Type error - key doesn't exist
// t("button.delete"); // Type error

Examples

Check out the example projects:

Documentation

License

MIT