2024-08-28 04:41:08 -07:00
|
|
|
import { getLocale } from 'pl-fe/actions/settings';
|
2022-07-22 15:01:26 -07:00
|
|
|
|
2023-03-13 12:44:09 -07:00
|
|
|
import { useAppSelector } from './useAppSelector';
|
2022-07-22 15:01:26 -07:00
|
|
|
|
2022-12-23 09:12:48 -08:00
|
|
|
/** Locales which should be presented in right-to-left. */
|
|
|
|
const RTL_LOCALES = ['ar', 'ckb', 'fa', 'he'];
|
|
|
|
|
|
|
|
interface UseLocaleResult {
|
2023-10-02 11:54:02 -07:00
|
|
|
locale: string;
|
2023-10-11 11:33:45 -07:00
|
|
|
direction: 'ltr' | 'rtl';
|
2022-12-23 09:12:48 -08:00
|
|
|
}
|
|
|
|
|
2022-07-22 15:01:26 -07:00
|
|
|
/** Get valid locale from settings. */
|
2022-12-23 09:12:48 -08:00
|
|
|
const useLocale = (fallback = 'en'): UseLocaleResult => {
|
2023-03-13 12:44:09 -07:00
|
|
|
const locale = useAppSelector((state) => getLocale(state, fallback));
|
2022-12-23 09:12:48 -08:00
|
|
|
|
2023-10-11 11:33:45 -07:00
|
|
|
const direction: 'ltr' | 'rtl' =
|
2022-12-23 09:12:48 -08:00
|
|
|
RTL_LOCALES.includes(locale)
|
|
|
|
? 'rtl'
|
2022-12-23 15:34:14 -08:00
|
|
|
: 'ltr';
|
2022-07-22 15:01:26 -07:00
|
|
|
|
2022-12-23 09:12:48 -08:00
|
|
|
return {
|
|
|
|
locale,
|
|
|
|
direction,
|
|
|
|
};
|
2022-07-22 15:01:26 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
export { useLocale };
|