i18n-provider.tsx 867 B

12345678910111213141516171819202122232425262728293031
  1. "use client";
  2. import { parse } from "bcp-47";
  3. import type { ComponentProps } from "react";
  4. import { useMemo } from "react";
  5. import { I18nProvider as I18nProviderBase, useIsSSR } from "react-aria";
  6. const SUPPORTED_LANGUAGES = new Set(["en"]);
  7. const DEFAULT_LOCALE = "en-US";
  8. export const I18nProvider = (
  9. props: Omit<ComponentProps<typeof I18nProviderBase>, "locale">,
  10. ) => {
  11. const isSSR = useIsSSR();
  12. const locale = useMemo(
  13. () =>
  14. isSSR
  15. ? DEFAULT_LOCALE
  16. : (globalThis.navigator.languages.find((locale) => {
  17. const language = parse(locale).language;
  18. return (
  19. language !== undefined &&
  20. language !== null &&
  21. SUPPORTED_LANGUAGES.has(language)
  22. );
  23. }) ?? DEFAULT_LOCALE),
  24. [isSSR],
  25. );
  26. return <I18nProviderBase locale={locale} {...props} />;
  27. };