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.

PropTypeDefaultDescription
projectKeystringProject API key (starts with bjs_). Mode 2/3.
projectIdstringFor JWT auth (advanced).
apiTokenstringJWT for advanced auth.
preloadedTranslationsRecord<string, Record<string, string>>Bundle translations into your app (Mode 1).
persistCachebooleantruePersist fetched text bundles to localStorage and refresh them in the background.
defaultLangstring"en"Initial language code.
apiUrlstring"https://api.bhashajs.com/api"API base URL (override for self-hosting, e.g. "https://my.host/api").
regionstringRegion override: "IN", "BD", "PK", "LK", "NP". Affects currency + Intl locale.
onLanguageChange(lang: string) => voidCallback whenever language changes.
register"default" | "formal" | "casual""default"Formality/style of translations. Switch at runtime via setRegister().
userSegment / segmentRulesstring / Record<string, Register>Maps a user segment (e.g. "genz") to a register automatically.
voicebooleanfalsePre-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 />

PropTypeDefault
style"dropdown" | "floating""dropdown"
position"top-right" | "top-left" | "bottom-right" | "bottom-left""top-right"
classNamestring

<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?)

OptionTypeDescription
useNativeDigitsbooleanRender digits in the native script (१२३ instead of 123).
compactbooleanCompact notation (1.5 लाख, 2 करोड़).

formatCurrency(value, lang, region?, options?)

OptionTypeDescription
currencystringOverride the auto-detected currency (e.g. "USD", "EUR").
display"symbol" | "code" | "name"How to render the currency.
useNativeDigitsbooleanRender digits in the native script.

formatDate(date, lang, region?, options?)

OptionTypeDescription
preset"short" | "medium" | "long" | "full"Default "medium".
useNativeDigitsbooleanRender 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

EndpointAuthDescription
GET /api/sdk/translations?lang=hi&register=formalx-api-keyFetch a flat text bundle.
POST /api/sdk/pushx-api-keyPush flat or nested locale JSON with a write-capable scoped key. Regulated keys are skipped for dashboard review.