Skip to content

Localization

@granit/localization provides framework-agnostic i18next configuration for loading translations from the .NET backend — mirroring Granit.Localization. It handles locale detection (localStorage → user setting → browser → default), i18next instance creation, and backend response application.

@granit/react-localization adds React integration with initReactI18next, a useLocale hook for language switchers, and re-exports useTranslation, Trans, and I18nextProvider from react-i18next.

Peer dependencies: i18next, react-i18next, react ^19

  • Directory@granit/localization/ i18next factory, locale resolution, translation loader (framework-agnostic)
    • @granit/react-localization React factory, useLocale hook, react-i18next re-exports
PackageRoleDepends on
@granit/localizationcreateLocalization, resolveInitialLocale, applyTranslationsi18next
@granit/react-localizationcreateReactLocalization, useLocale, re-exports@granit/localization, react-i18next, react
import { createReactLocalization } from '@granit/react-localization';
import { I18nextProvider } from '@granit/react-localization';
import { applyTranslations } from '@granit/localization';
const i18n = createReactLocalization();
// After fetching translations from the backend:
const data = await fetch('/api/v1/localization?cultureName=fr');
applyTranslations(i18n, data);
function App({ children }) {
return (
<I18nextProvider i18n={i18n}>
{children}
</I18nextProvider>
);
}
interface LanguageInfo {
cultureName: string;
displayName: string;
flagIcon?: string;
isDefault: boolean;
}
interface ApplicationLocalizationDto {
cultureName: string;
resources: Record<string, Record<string, string>>;
languages: LanguageInfo[];
}
interface LocalizationConfig {
storageKey?: string; // localStorage key (default: 'locale' → dd:locale)
defaultNS?: string; // i18next namespace (default: 'translation')
plugins?: Module[]; // i18next plugins (e.g. initReactI18next)
}

Creates an isolated i18next instance (not the global singleton). The instance is initialized without a lng option — the locale is resolved separately and applied via applyTranslations.

function createLocalization(config?: LocalizationConfig): i18n;

resolveInitialLocale(languages?, storageKey?, userLocale?)

Section titled “resolveInitialLocale(languages?, storageKey?, userLocale?)”

Resolves the initial locale before backend data is available.

Detection cascade:

  1. localStorage (dd:locale) — if present, use it
  2. userLocale from user settings — if provided, use it
  3. navigator.language — try exact match, then base code
  4. languages array — use the one with isDefault: true
  5. Fallback: 'fr'
function resolveInitialLocale(
languages?: LanguageInfo[],
storageKey?: string,
userLocale?: string | null
): string;

Applies the backend response to the i18next instance. Backend resources are grouped by module (e.g. { "Granit": {...}, "Guava": {...} }) — this function merges them into the single translation namespace.

function applyTranslations(instance: i18n, data: ApplicationLocalizationDto): void;
const LOCALE_STORAGE_KEY = 'locale'; // prefixed as dd:locale by @granit/storage

Convenience wrapper that injects initReactI18next as a plugin automatically.

function createReactLocalization(
config?: Omit<LocalizationConfig, 'plugins'>
): i18n;

Hook for language switchers. Returns the current locale and a setter that persists to localStorage, updates i18next, and optionally notifies the backend.

interface UseLocaleOptions {
onLocaleChange?: (locale: string) => void;
}
function useLocale(options?: UseLocaleOptions): {
locale: string;
setLocale: (locale: string) => void;
};

For convenience, the following are re-exported from react-i18next:

  • I18nextProvider — Wrap your app to provide the i18next instance
  • useTranslation — Access translations in components
  • Trans — Component for interpolated translations with JSX
CategoryKey exportsPackage
TypesLanguageInfo, ApplicationLocalizationDto, LocalizationConfig@granit/localization
FactorycreateLocalization()@granit/localization
Locale resolutionresolveInitialLocale(), LOCALE_STORAGE_KEY@granit/localization
Translation loaderapplyTranslations()@granit/localization
React factorycreateReactLocalization()@granit/react-localization
Locale hookuseLocale()@granit/react-localization
Re-exportsI18nextProvider, useTranslation, Trans@granit/react-localization
  • Granit.Localization module — .NET i18n with 17 cultures, override store, and source-generated keys
  • SettingsSETTING_NAMES.PREFERRED_CULTURE stores the user’s language preference
  • Reference Data — Country labels use the same multilingual pattern