Skip to main content

Function.persistPreferredLocale

function persistPreferredLocale(locale: PreferredLocale, updateUser: (payload: {
preferredLocale: PreferredLocale;
}) => Promise<unknown>): Promise<void>;

Persists a locale preference change to the server via the user update API.

This function:

  • Calls the provided updateUser function with the new locale
  • Handles errors gracefully (logs but does not throw)
  • Is non-blocking; locale has already been applied locally before persistence is attempted

Error handling extracts the .message property from Error objects to avoid leaking server internals, and safely coerces non-Error rejections to strings.

This is fire-and-forget: the returned promise always resolves (never rejects), and callers are not expected to await it. The Promise<void> return only exists so callers may await if they want to sequence work after persistence completes.

Parameters

ParameterTypeDescription
localePreferredLocaleThe locale code to persist. Must be a PreferredLocale — narrow an app-local string locale with isSupportedEmailLocale (from @keyban/types) first.
updateUser(payload: { preferredLocale: PreferredLocale; }) => Promise<unknown>Function that updates the user on the server

Returns

Promise<void>

A promise that always resolves once persistence succeeds or fails (errors are logged).

Example

const handleLocaleChange = (locale: string) => {
setLocale(locale); // apply locally immediately
persistPreferredLocale(locale, updateUser); // persist asynchronously
};