Skip to main content

Function.usePreferredLocaleSyncWithUser

function usePreferredLocaleSyncWithUser<T>(user:
| {
preferredLocale?: string | null;
}
| null
| undefined, options: UsePreferredLocaleSyncOptions<T>): void;

Variant of usePreferredLocaleSync that accepts a user object directly. Use this when the component is not wrapped with KeybanAuthProvider.

This is the primitive both variants share; usePreferredLocaleSync delegates here.

Callers may pass freshly-built supportedLocales (e.g. Object.values(Locale)) and inline onLocaleChange arrows without memoizing them: the callback and active locale are read from refs, and the effect is keyed solely on the resolved target locale (a primitive) rather than the array/function identity, so it only re-runs when the user's server preference actually changes — not on every render, and not when the user changes locale manually.

Type Parameters

Type ParameterDefault type
T extends stringstring

Parameters

ParameterTypeDescription
user{ preferredLocale?: string
optionsUsePreferredLocaleSyncOptions<T>Configuration for locale syncing

Returns

void

Example

function AppRoot() {
const { setLocale } = useLocaleContext();
const { data: session } = useSession(); // from better-auth or other
usePreferredLocaleSyncWithUser(session?.user, {
supportedLocales: ['en-US', 'fr-FR', 'es-ES'],
onLocaleChange: setLocale,
});
return <App />;
}