New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@chainplatform/language

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@chainplatform/language

@chainplatform/language

latest
Source
npmnpm
Version
0.3.5
Version published
Weekly downloads
9
350%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Current npm package version. PRs welcome! Follow @doansan

🚀 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.

OptionTypeDescription
fallbackstringDefault fallback language
translationsobjectPredefined translations
lazyLoadfunctionAsync function to load translation file dynamically
loadFromApifunctionAsync function to fetch translations from API
languagestringForce set initial language
storageobjectCustom { get, set } async storage methods
storage_keystringCustom 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

Keywords

react

FAQs

Package last updated on 18 Oct 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts