useLocale

Returns the current locale and layout direction.

installyarn add @react-aria/i18n
version3.3.9
usageimport {useLocale} from '@react-aria/i18n'

Introduction#


useLocale allows components to access the current locale and interface layout direction. By default, this is automatically detected based on the browser or system language, but it can be overridden by using the I18nProvider at the root of your app.

useLocale should be used in the root of your app to define the lang and dir attributes so that the browser knows which language and direction the user interface should be rendered in.

API#


useLocale(): Locale

Interface#


NameTypeDescription
localestringThe BCP47 language code for the locale.
directionDirectionThe writing direction for the locale.

Example#


import {useLocale} from '@react-aria/i18n';

function YourApp() {
  let {locale, direction} = useLocale();

  return (
    <div lang={locale} dir={direction}>
      {/* your app here */}
    </div>
  );
}
import {useLocale} from '@react-aria/i18n';

function YourApp() {
  let {locale, direction} = useLocale();

  return (
    <div lang={locale} dir={direction}>
      {/* your app here */}
    </div>
  );
}
import {useLocale} from '@react-aria/i18n';

function YourApp() {
  let {
    locale,
    direction
  } = useLocale();

  return (
    <div
      lang={locale}
      dir={direction}
    >
      {/* your app here */}
    </div>
  );
}