API reference
<I18nProvider>
Top-level provider. Wrap your app once.
| Prop | Type | Default | Description |
|---|---|---|---|
projectKey | string | — | Project API key (starts with bjs_). Mode 2/3. |
projectId | string | — | For JWT auth (advanced). |
apiToken | string | — | JWT for advanced auth. |
preloadedTranslations | Record<string, Record<string, string>> | — | Bundle translations into your app (Mode 1). |
defaultLang | string | "en" | Initial language code. |
apiUrl | string | "https://api.bhashajs.com" | API base URL (override for self-hosting). |
region | string | — | Region override: "IN", "BD", "PK", "LK", "NP". Affects currency + Intl locale. |
onLanguageChange | (lang: string) => void | — | Callback whenever language changes. |
useTranslation()
const {
t, // (key, params?) => string
currentLang, // "hi"
setLang, // (lang: string) => void
supportedLangs, // string[]
isLoading, // boolean
error, // string | null — surfaces auth/network errors
formatNumber, // (value, options?) => string
formatCurrency, // (value, options?) => string
formatDate, // (date, options?) => string
} = useTranslation(); t(key, params?)
The translation function. Returns the translated string for the current language with fallback chain support.
t("hero.title"); // simple lookup
t("greeting", { name: "Rohan" }); // interpolation
t("items", { count: 5 }); // pluralization (uses items_one / items_other) setLang(lang)
Switch the active language. Triggers font load + RTL switch + re-render.
useLangInfo()
const {
code, // "hi"
name, // "हिन्दी"
englishName, // "Hindi"
dir, // "ltr" | "rtl"
font, // "Noto Sans Devanagari, sans-serif"
script, // "Devanagari"
defaultRegion, // "IN"
intlLocale, // "hi-IN"
defaultCurrency, // "INR"
} = useLangInfo(); <LanguageSwitcher />
| Prop | Type | Default |
|---|---|---|
style | "dropdown" | "floating" | "dropdown" |
position | "top-right" | "top-left" | "bottom-right" | "bottom-left" | "top-right" |
className | string | — |
<Trans />
Component-based translation, useful when wrapping rendered output.
<Trans id="hero.title" />
<Trans id="greeting" params={{ name: "Rohan" }} as="h1" /> Standalone utilities
These don't need React. Import directly.
formatNumber(value, lang, region?, options?)
| Option | Type | Description |
|---|---|---|
useNativeDigits | boolean | Render digits in the native script (१२३ instead of 123). |
compact | boolean | Compact notation (1.5 लाख, 2 करोड़). |
formatCurrency(value, lang, region?, options?)
| Option | Type | Description |
|---|---|---|
currency | string | Override the auto-detected currency (e.g. "USD", "EUR"). |
display | "symbol" | "code" | "name" | How to render the currency. |
useNativeDigits | boolean | Render digits in the native script. |
formatDate(date, lang, region?, options?)
| Option | Type | Description |
|---|---|---|
preset | "short" | "medium" | "long" | "full" | Default "medium". |
useNativeDigits | boolean | Render digits in the native script. |
getPluralCategory(count, lang)
Returns "one" or "other" based on the language's CLDR plural rules.
getPluralCategory(0, "hi"); // "one" (Hindi: 0 is singular)
getPluralCategory(0, "en"); // "other" (English: 0 is plural)
getPluralCategory(1, "ta"); // "one" getFallbackChain(lang)
Returns the culturally-aware fallback chain for the language.
getFallbackChain("bn"); // ["bn", "hi", "en"]
getFallbackChain("ta"); // ["ta", "en"] (Dravidian — skips Hindi)
getFallbackChain("pa-PK");// ["pa-PK", "ur", "en"] getLangInfo(lang), LANGUAGES, REGION_OVERRIDES
Read-only access to the language metadata database.