Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/wojtekmaj/react-t. Simple translation module for React applications.
Something went wrong. Try again.
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940414243import { getUserLocales } from 'get-user-locale';
import { getMatchingLocale } from '../utils/locale';
import useDocumentLocale from './useDocumentLocale';
export default function useLocale({ defaultLocale, propsLocale, supportedLocales,}: { defaultLocale?: string; propsLocale?: string; supportedLocales: string[];}): string | null { const documentLocale = useDocumentLocale();
if (propsLocale) { const matchingPropsLocale = getMatchingLocale([propsLocale], supportedLocales);
if (matchingPropsLocale) { return matchingPropsLocale; } }
if (documentLocale) { const matchingDocumentLocale = getMatchingLocale([documentLocale], supportedLocales);
if (matchingDocumentLocale) { return matchingDocumentLocale; } }
const userLocales = getUserLocales(); const matchingUserLocale = getMatchingLocale(userLocales, supportedLocales);
if (matchingUserLocale) { return matchingUserLocale; }
return defaultLocale || null;}