DocsReact

Hooks

Reference for useGenericTranslation, useDictionary, useLocale, useSetLocale, useLocales, useLocaleProperties, useLocaleDirection and useGenericTranslationContext.

On this page

All hooks are exported from @generic-translation/react and must be used inside client components. Outside a GenericTranslationProvider they return source-language defaults.

useGenericTranslation

const t = useGenericTranslation();
t(message: string, values?: MessageValues): string

Translates and formats an ICU message. message is written in your default locale. The hash of the message (and optional context) is looked up in the current translations. values holds the arguments, plus two reserved keys:

Key Meaning
$context Disambiguating note. Part of the hash.
$id Stable identifier for the dashboard. Not part of the hash.
const t = useGenericTranslation();

t("Save");
t("Hello {name}", { name });
t("{count, plural, =0 {No results} one {# result} other {# results}}", { count });
t("Close", { $context: "verb", $id: "modal.close" });

For the CLI to extract the message, the first argument must be a string literal (or a template literal without substitutions). If a translation is missing or incompatible, t formats the source; if the source is not valid ICU it returns it as is. It never throws. The returned function is memoized per locale and translation map.

Use t for attributes and other places where JSX does not fit (title, placeholder, aria-label). Use <T> for rich content with elements.

useDictionary

const d = useDictionary();
d(id: string, values?: Record<string, unknown>): string

Looks up an entry of the provider’s dictionary by dot path and translates it. An unknown id returns the id and logs a development warning. See Dictionaries.

const d = useDictionary();
<a href="/">{d("nav.home")}</a>
<p>{d("cart.items", { count })}</p>

useLocale

const locale: string = useLocale();

The locale being rendered (BCP 47, for example "pt-BR").

useSetLocale

const setLocale = useSetLocale(); // (locale: string) => Promise<void>
await setLocale("fr");

Switches locale. The target is matched against the provider’s locales (fr-CA can match fr). The promise resolves once the new translations are loaded and applied. With localeNavigation (Next.js) it saves the cookie and navigates to the localized URL instead.

useLocales

const locales: string[] = useLocales();

Every supported locale, default locale first.

function Switcher() {
  const locales = useLocales();
  const setLocale = useSetLocale();
  return locales.map((l) => <button key={l} onClick={() => setLocale(l)}>{l}</button>);
}

useLocaleProperties

const props: LocaleProperties = useLocaleProperties(locale?: string);

Names, flag, direction and more for locale (default: the current one), with display names written in the current locale. The LocaleProperties type comes from @generic-translation/core: code, name, nativeName, languageCode, languageName, regionCode?, regionName?, scriptCode?, emoji, direction.

const { nativeName, emoji } = useLocaleProperties("ja"); // in "en": "Japanese" shown as name, nativeName "日本語"

useLocaleDirection

const dir: "ltr" | "rtl" = useLocaleDirection(locale?: string);

Text direction of locale (default: the current one). The provider already keeps <html dir> in sync unless updateHtmlAttributes={false}.

useGenericTranslationContext

const ctx: GenericTranslationContextValue = useGenericTranslationContext();

The raw provider value: locale, defaultLocale, locales, translations, dictionary, setLocale, isLoading, reportMissing. Use it to show a loading state while a locale switch fetches translations:

const { isLoading } = useGenericTranslationContext();

Types

TranslateFunction, DictionaryFunction, MessageValues, Dictionary, DictionaryLeaf, TranslationMap, GenericTranslationContextValue and LocaleProperties are exported as types.

localizePath

Also exported from @generic-translation/react: builds the public URL of a path in another locale under a prefix strategy. Useful for locale-aware links in Next.js.

import { localizePath } from "@generic-translation/react";

localizePath("/about", "fr", { locales: ["es", "fr"], defaultLocale: "en", localePrefix: "as-needed" }); // "/fr/about"
localizePath("/fr/about", "en", { locales: ["es", "fr"], defaultLocale: "en", localePrefix: "as-needed" }); // "/about"

localePrefix is "always", "as-needed" (default) or "never".

Something wrong or unclear on this page? Open an issue.

Type to search the documentation.