2023-03-13 12:44:09 -07:00
|
|
|
import { getLocale } from 'soapbox/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
|
|
|
import type { CSSProperties } from 'react';
|
|
|
|
|
|
|
|
/** Locales which should be presented in right-to-left. */
|
|
|
|
const RTL_LOCALES = ['ar', 'ckb', 'fa', 'he'];
|
|
|
|
|
|
|
|
interface UseLocaleResult {
|
|
|
|
locale: string
|
|
|
|
direction: CSSProperties['direction']
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
const direction: CSSProperties['direction'] =
|
|
|
|
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 };
|