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.
- 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
npm install @chainplatform/language
# or
yarn add @chainplatform/languageimport 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>
);
}You can use the LanguageContext hook or withLanguage HOC to access translation functions.
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")} />
</>
);
}import React from "react";
import { withLanguage } from "@chainplatform/language";
function MyComponent({ t, language }) {
return <Text>{t("hello")} ({language})</Text>;
}
export default withLanguage(MyComponent);<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><LanguageProvider
fallback="en"
loadFromApi={async (lang) => {
const res = await fetch(`https://example.com/i18n/${lang}.json`);
return await res.json();
}}
>
<App />
</LanguageProvider>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>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" });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 |
Translates a key with optional variables.
Changes the current language and saves it to storage.
Subscribes to language changes.
locales/en.json
{
"hello": "Hello",
"welcome": "Welcome, {name}!"
}locales/vi.json
{
"hello": "Xin chào",
"welcome": "Chào mừng, {name}!"
}MIT License © ChainPlatform