import { LanguageDetectorModule } from 'i18next' ; let hasLocalStorageSupport = null ; const localStorageAvailable = ( ) => { if ( hasLocalStorageSupport !== null ) { return hasLocalStorageSupport ; } try { const localStorage = window . localStorage ; hasLocalStorageSupport = localStorage !== null ; const testKey = 'i18next.translate.boo' ; localStorage . setItem ( testKey , 'foo' ) ; localStorage . removeItem ( testKey ) ; } catch ( e ) { hasLocalStorageSupport = false ; } return hasLocalStorageSupport ; } ; const LANGUAGE_LOCAL_STORAGE = 'i18nextLng' ; const languageDetector = { type : 'languageDetector' , init : ( ) => undefined , detect : ( ) => { const bcp47Tags = [ ] ; if ( localStorageAvailable ( ) ) { const localStorageLanguage = window . localStorage . getItem ( LANGUAGE_LOCAL_STORAGE ) ; if ( localStorageLanguage ) { bcp47Tags . push ( localStorageLanguage ) ; } } if ( typeof navigator !== 'undefined' ) { if ( navigator . languages ) { for ( let i = 0 ; i < navigator . languages . length ; i += 1 ) { bcp47Tags . push ( navigator . languages [ i ] ! ) ; } } const userLanguage = ( navigator as { userLanguage ? : string ; } ) . userLanguage ; if ( userLanguage ) { bcp47Tags . push ( userLanguage ) ; } if ( navigator . language ) { bcp47Tags . push ( navigator . language ) ; } } return bcp47Tags . length > 0 ? bcp47Tags : undefined ; } , cacheUserLanguage : ( language ) => { if ( localStorageAvailable ( ) ) { window . localStorage . setItem ( LANGUAGE_LOCAL_STORAGE , language ) ; } } } ; export default languageDetector ;