1- import { useState , useEffect } from 'react'
2- import { getI18n , getDefaults , addUsedNamespaces } from './context'
3- import { warnOnce } from './utils'
1+ import { useState , useEffect } from 'react' ;
2+ import { getI18n , getDefaults , addUsedNamespaces } from './context' ;
3+ import { warnOnce } from './utils' ;
44
55function loadNamespaces ( i18n , ns , cb ) {
66 i18n . loadNamespaces ( ns , ( ) => {
77 // delay ready if not yet initialized i18n instance
88 if ( i18n . isInitialized ) {
9- cb ( )
9+ cb ( ) ;
1010 } else {
1111 const initialized = ( ) => {
1212 // due to emitter removing issue in i18next we need to delay remove
1313 setImmediate ( ( ) => {
14- i18n . off ( 'initialized' , initialized )
15- } )
16- cb ( )
17- }
14+ i18n . off ( 'initialized' , initialized ) ;
15+ } ) ;
16+ cb ( ) ;
17+ } ;
1818
19- i18n . on ( 'initialized' , initialized )
19+ i18n . on ( 'initialized' , initialized ) ;
2020 }
21- } )
21+ } ) ;
2222}
2323
2424export function useTranslation ( ns ) {
2525 // assert we have the needed i18nInstance
26- const i18n = getI18n ( )
26+ const i18n = getI18n ( ) ;
2727 if ( ! i18n ) {
28- warnOnce ( 'You will need pass in an i18next instance by using i18nextReactModule' )
29- return [ k => k , { } ]
28+ warnOnce ( 'You will need pass in an i18next instance by using i18nextReactModule' ) ;
29+ return [ k => k , { } ] ;
3030 }
31- const i18nOptions = getDefaults ( )
31+ const i18nOptions = getDefaults ( ) ;
3232
3333 // prepare having a namespace
34- let namespaces = ns || ( i18n . options && i18n . options . defaultNS )
35- namespaces = typeof namespaces === 'string' ? [ namespaces ] : namespaces || [ 'translation' ]
34+ let namespaces = ns || ( i18n . options && i18n . options . defaultNS ) ;
35+ namespaces = typeof namespaces === 'string' ? [ namespaces ] : namespaces || [ 'translation' ] ;
3636
3737 // report namespaces as used
38- addUsedNamespaces ( namespaces )
38+ addUsedNamespaces ( namespaces ) ;
3939
4040 // are we ready? yes if all namespaces in first language are loaded already (either with data or empty objedt on failed load)
4141 const ready =
@@ -49,40 +49,40 @@ export function useTranslation(ns) {
4949 i18n . services . backendConnector . state [ `${ i18n . languages [ 0 ] } |${ n } ` ] &&
5050 i18n . services . backendConnector . state [ `${ i18n . languages [ 0 ] } |${ n } ` ] !== 1 &&
5151 ( ( i18n . options && ! i18n . options . fallbackLng ) ||
52- i18n . hasResourceBundle ( i18n . languages [ i18n . languages . length - 1 ] , n ) ) ) // we have at least tried to load it and have a fallback if fallbackLng is set
52+ i18n . hasResourceBundle ( i18n . languages [ i18n . languages . length - 1 ] , n ) ) ) ; // we have at least tried to load it and have a fallback if fallbackLng is set
5353
54- return ret
55- } )
54+ return ret ;
55+ } ) ;
5656
5757 // set states
58- const [ t , setT ] = useState ( { t : i18n . getFixedT ( null , namespaces [ 0 ] ) } ) // seems we can't have functions as value -> wrap it in obj
58+ const [ t , setT ] = useState ( { t : i18n . getFixedT ( null , namespaces [ 0 ] ) } ) ; // seems we can't have functions as value -> wrap it in obj
5959
6060 function resetT ( ) {
61- setT ( { t : i18n . getFixedT ( null , namespaces [ 0 ] ) } )
61+ setT ( { t : i18n . getFixedT ( null , namespaces [ 0 ] ) } ) ;
6262 }
6363
6464 useEffect ( ( ) => {
6565 // bind events to trigger change, like languageChanged
66- if ( i18nOptions . bindI18n && i18n ) i18n . on ( i18nOptions . bindI18n , resetT )
66+ if ( i18nOptions . bindI18n && i18n ) i18n . on ( i18nOptions . bindI18n , resetT ) ;
6767
6868 // unbinding
6969 return ( ) => {
7070 if ( i18nOptions . bindI18n ) {
71- const p = i18nOptions . bindI18n . split ( ' ' )
72- p . forEach ( f => i18n . off ( f , resetT ) )
71+ const p = i18nOptions . bindI18n . split ( ' ' ) ;
72+ p . forEach ( f => i18n . off ( f , resetT ) ) ;
7373 }
74- }
75- } )
74+ } ;
75+ } ) ;
7676
7777 // return hook stuff if ready or
7878 // not yet loaded namespaces -> load them -> and trigger suspense
7979 if ( ready ) {
80- return [ t . t , i18n ]
80+ return [ t . t , i18n ] ;
8181 }
8282 throw new Promise ( resolve => {
8383 loadNamespaces ( i18n , namespaces , ( ) => {
84- resetT ( )
85- resolve ( )
86- } )
87- } )
84+ resetT ( ) ;
85+ resolve ( ) ;
86+ } ) ;
87+ } ) ;
8888}
0 commit comments