()
| 53 | * Returns the current browser/system language, and updates when it changes. |
| 54 | */ |
| 55 | export function useDefaultLocale(): Locale { |
| 56 | let isSSR = useIsSSR(); |
| 57 | let [defaultLocale, setDefaultLocale] = useState(currentLocale); |
| 58 | |
| 59 | useEffect(() => { |
| 60 | if (listeners.size === 0) { |
| 61 | window.addEventListener('languagechange', updateLocale); |
| 62 | } |
| 63 | |
| 64 | listeners.add(setDefaultLocale); |
| 65 | |
| 66 | return () => { |
| 67 | listeners.delete(setDefaultLocale); |
| 68 | if (listeners.size === 0) { |
| 69 | window.removeEventListener('languagechange', updateLocale); |
| 70 | } |
| 71 | }; |
| 72 | }, []); |
| 73 | |
| 74 | // We cannot determine the browser's language on the server, so default to |
| 75 | // en-US. This will be updated after hydration on the client to the correct value. |
| 76 | if (isSSR) { |
| 77 | let locale = typeof window !== 'undefined' && window[localeSymbol]; |
| 78 | return { |
| 79 | locale: locale || 'en-US', |
| 80 | direction: 'ltr' |
| 81 | }; |
| 82 | } |
| 83 | |
| 84 | return defaultLocale; |
| 85 | } |
no test coverage detected