MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useDefaultLocale

Function useDefaultLocale

packages/react-aria/src/i18n/useDefaultLocale.ts:55–85  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

53 * Returns the current browser/system language, and updates when it changes.
54 */
55export 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}

Callers 2

useLocaleFunction · 0.90

Calls 5

useIsSSRFunction · 0.90
addEventListenerMethod · 0.80
deleteMethod · 0.80
removeEventListenerMethod · 0.80
addMethod · 0.65

Tested by

no test coverage detected