API reference
New in 0.3: a framework-agnostic engine (bhasha-js/vanilla),
a server-safe entry (bhasha-js/server) for React Server Components / Next.js App Router,
and a npx bhasha pull CLI that makes t() type-safe (autocomplete + a compile
error on a typo'd key).
<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). |
persistCache | boolean | true | Persist fetched text bundles to localStorage and refresh them in the background. |
defaultLang | string | "en" | Initial language code. |
apiUrl | string | "https://api.bhashajs.com/api" | API base URL (override for self-hosting, e.g. "https://my.host/api"). |
region | string | — | Region override: "IN", "BD", "PK", "LK", "NP". Affects currency + Intl locale. |
onLanguageChange | (lang: string) => void | — | Callback whenever language changes. |
register | "default" | "formal" | "casual" | "default" | Formality/style of translations. Switch at runtime via setRegister(). |
userSegment / segmentRules | string / Record<string, Register> | — | Maps a user segment (e.g. "genz") to a register automatically. |
voice | boolean | false | Pre-fetch IPA/SSML so formatPhonetic() / formatSSML() return data. |
useTranslation()
const {
t, // (key, params?) => string
currentLang, // "hi"
setLang, // (lang: string) => void
supportedLangs, // string[]
isLoading, // boolean
error, // string | null — surfaces auth/network errors
register, // "default" | "formal" | "casual"
setRegister, // (register) => void
setSegment, // (segment: string) => void
formatNumber, // (value, options?) => string
formatCurrency, // (value, options?) => string
formatDate, // (date, options?) => string
formatPhonetic, // (key) => string — IPA (needs voice:true)
formatSSML, // (key) => string — SSML (needs voice:true)
} = 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.
Public SDK API
| Endpoint | Auth | Description |
|---|---|---|
GET /api/sdk/translations?lang=hi®ister=formal | x-api-key | Fetch a flat text bundle. |
POST /api/sdk/push | x-api-key | Push flat or nested locale JSON with a write-capable scoped key. Regulated keys are skipped for dashboard review. |