import * as React from "react" ; /** Get the locale using navigator */ const getLocale = ( initial ? ) => { let modern ; if ( navigator . languages && navigator . languages . length ) { [ modern ] = navigator . languages ; } const old = navigator . language ; return modern || old || initial || "en-US" ; } ; param initial example export const useLocale = ( initial ? ) => { const [ locale , setLocale ] = React . useState ( getLocale ( initial ) ) ; /** Watch for locale changes */ React . useEffect ( ( ) => { /** Set locale state */ const languageListener = ( ) => { setLocale ( getLocale ( ) ) ; } ; window . addEventListener ( "languagechange" , languageListener ) ; return ( ) => { window . removeEventListener ( "languagechange" , languageListener ) ; } ; } ) ; return locale ; } ;