DocsReact
GenericTranslationProvider
The root component that holds the active locale, its translation map and the dictionary.
On this page
GenericTranslationProvider is the client-side root of @generic-translation/react. It holds the current locale, the translation map for it, the optional dictionary, and setLocale. Everything else (<T>, useGenericTranslation(), <LocaleSelector>) reads from it.
import { GenericTranslationProvider } from "@generic-translation/react";
<GenericTranslationProvider defaultLocale="en" locales={["es", "fr"]} loadTranslations={load}>
<App />
</GenericTranslationProvider>
Outside any provider, components work but show their source content, with en as the locale.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
children |
ReactNode |
Your app. | |
locale |
string |
The locale to render. Supply it, together with translations, when rendering on a server so the first client render matches. When omitted, the provider starts in defaultLocale and, after mount, switches to the visitor’s saved or browser language. |
|
defaultLocale |
string |
"en" |
The language your source text is written in. |
locales |
string[] |
[] |
The locales you translate into. The default locale is added automatically and listed first. |
translations |
Record<string, string> |
Translation map for the initial locale ({ [hash]: message }). |
|
loadTranslations |
(locale) => Promise<TranslationMap> |
Loads the map of another locale, for example fetch("/_generic-translation/es.json"). |
|
projectId |
string |
With no loadTranslations, maps are fetched from the CDN of this project. |
|
baseUrl |
string |
hosted API | API origin used for the CDN and dev translation. |
devApiKey |
string |
Development only. See below. | |
dictionary |
Dictionary |
Nested dictionary for useDictionary(). |
|
onLocaleChange |
(locale) => void |
Called after the locale changed. | |
detectLocale |
boolean |
true |
Pick up the saved or browser language after mount when no locale prop is given. |
updateHtmlAttributes |
boolean |
true |
Keep <html lang> and <html dir> in sync with the locale. |
localeNavigation |
{ localePrefix: "always" | "as-needed" | "never" } |
For apps whose server decides the locale from the URL (the Next.js integration sets this). Switching locale then saves the cookie and navigates to the localized URL. |
Where translations come from
For the initial locale, translations is used. For any other locale, the provider calls loadTranslations(locale) or, if you gave a projectId instead, fetches /cdn/{projectId}/{locale}.json from the API origin. Loaded maps are cached per locale; concurrent loads for one locale share one request. A failed load logs one warning and the app shows source text.
Translations whose variables or tags do not match the source are ignored (in development you get a console warning) and the source is shown instead.
Locale detection
When no locale prop is passed and detectLocale is not false, the provider looks, after mount, at: the generic-translation-locale cookie, localStorage["generic-translation-locale"], then navigator.languages. The best match among your locales wins (via determineLocale from core). Nothing is read during render, so server output and the first client render agree.
Switching locale
setLocale (see useSetLocale) validates the target against your locales, saves the choice to the cookie and localStorage, loads the map if needed, then re-renders. While a map loads, isLoading is true and the old locale stays visible. If a newer switch starts first, the older one is dropped. Unknown locales are ignored with a development warning.
Development-only translation
devApiKey lets the provider translate strings that are missing from the loaded map while you develop. Missing messages are batched (50 per request, debounced 50 ms) and sent to the API; results are merged into the map.
<GenericTranslationProvider projectId="prj_..." devApiKey={import.meta.env.DEV ? import.meta.env.VITE_GENERIC_TRANSLATION_DEV_KEY : undefined} ... />
Do not ship it. When NODE_ENV is production, the key is ignored and a console error is logged. API keys must never reach a browser bundle; in production, publish translations with the CLI and read them from files or the CDN. See API keys and permissions.
Server rendering
With locale and translations both supplied, the provider renders translated output on the server and hydrates identically. For Next.js use the server wrapper from @generic-translation/react/next, which does this for you: see Next.js setup.
GenericTranslationContext
The provider’s value is exported as GenericTranslationContext (and its type as GenericTranslationContextValue) for building custom hooks. useGenericTranslationContext() reads it.
| Field | Type |
|---|---|
locale |
string |
defaultLocale |
string |
locales |
string[] |
translations |
Record<string, string> |
dictionary |
flattened dictionary (id -> { source, context? }) |
setLocale |
(locale: string) => Promise<void> |
isLoading |
boolean |
reportMissing |
development-only hook, or undefined |
Something wrong or unclear on this page? Open an issue.