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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@chainplatform/language

v0.3.5

Published

@chainplatform/language

Readme

Language Manager for React Native

A lightweight and flexible language management system for React Native apps. Supports local storage, lazy loading, API-based translation loading, and full React Context integration.


🚀 Features

  • Persistent language storage
  • Auto-detect device locale
  • Lazy-load translation files
  • Optional API-based translation loading
  • Simple string interpolation
  • Number and date formatting using Intl
  • React Context + HOC integration
  • Type-safe and lightweight

📦 Installation

npm install @chainplatform/language
# or
yarn add @chainplatform/language

🧠 Usage

1️⃣ Basic Setup

import React from "react";
import { LanguageProvider } from "@chainplatform/language";
import App from "./App";

export default function Root() {
  return (
    <LanguageProvider
      fallback="en"
      translations={{
        en: { hello: "Hello" },
        vi: { hello: "Xin chào" }
      }}
    >
      <App />
    </LanguageProvider>
  );
}

2️⃣ Using the t() function

You can use the LanguageContext hook or withLanguage HOC to access translation functions.

✅ Using React Context

import React, { useContext } from "react";
import { LanguageContext } from "@chainplatform/language";

export default function MyComponent() {
  const { t, changeLanguage } = useContext(LanguageContext);

  return (
    <>
      <Text>{t("hello")}</Text>
      <Button title="Switch to Vietnamese" onPress={() => changeLanguage("vi")} />
    </>
  );
}

✅ Using HOC

import React from "react";
import { withLanguage } from "@chainplatform/language";

function MyComponent({ t, language }) {
  return <Text>{t("hello")} ({language})</Text>;
}

export default withLanguage(MyComponent);

3️⃣ Lazy Loading Translations

<LanguageProvider
  fallback="en"
  lazyLoad={async (lang) => {
    switch (lang) {
      case "vi":
        return await import("./locales/vi.json").then(m => m.default);
      case "en":
        return await import("./locales/en.json").then(m => m.default);
      default:
        return {};
    }
  }}
>
  <App />
</LanguageProvider>

4️⃣ API-Based Loading

<LanguageProvider
  fallback="en"
  loadFromApi={async (lang) => {
    const res = await fetch(`https://example.com/i18n/${lang}.json`);
    return await res.json();
  }}
>
  <App />
</LanguageProvider>

5️⃣ Persistent Storage

By default, it uses @chainplatform/sdk’s retrieveStorage and saveStorage.
You can override with your own implementation:

<LanguageProvider
  storage={{
    get: async (key) => AsyncStorage.getItem(key),
    set: async (key, val) => AsyncStorage.setItem(key, val)
  }}
>
  <App />
</LanguageProvider>

6️⃣ Formatting Helpers

t("welcome", { name: "John" }); // "Welcome, John"

formatNumber(123456.78); // 123,456.78 or 123.456,78 depending on locale
format("DateTimeFormat", new Date(), { dateStyle: "medium" });

⚙️ API Reference

Language.init(options)

Initializes the language manager.
Called automatically by LanguageProvider.

| Option | Type | Description | |--------|------|-------------| | fallback | string | Default fallback language | | translations | object | Predefined translations | | lazyLoad | function | Async function to load translation file dynamically | | loadFromApi | function | Async function to fetch translations from API | | language | string | Force set initial language | | storage | object | Custom { get, set } async storage methods | | storage_key | string | Custom key for language storage |


Language.t(key, vars)

Translates a key with optional variables.

Language.changeLanguage(lang)

Changes the current language and saves it to storage.

Language.onLanguageChange(callback)

Subscribes to language changes.


🧩 Example Translation Files

locales/en.json

{
  "hello": "Hello",
  "welcome": "Welcome, {name}!"
}

locales/vi.json

{
  "hello": "Xin chào",
  "welcome": "Chào mừng, {name}!"
}

📄 License

MIT License © ChainPlatform