import i18n from 'i18next' ; export const fetchLocale = async ( locale ) => { const res = await window . fetch ( `./_locales/${ locale }/messages.json` ) ; const data = await res . json ( ) ; return Object . keys ( data ) . reduce ( ( res , key ) => { return { ... res , [ key . replace ( /__/g , ' ' ) ] : data [ key ] . message , } ; } , { } ) ; } ; i18n . init ( { fallbackLng : 'en' , defaultNS : 'translations' , interpolation : { escapeValue : false , } , } ) ; export const I18N_NS = 'translations' ; export const addResourceBundle = async ( locale ) => { if ( i18n . hasResourceBundle ( locale , I18N_NS ) ) return ; const bundle = await fetchLocale ( locale ) ; i18n . addResourceBundle ( locale , 'translations' , bundle ) ; } ; addResourceBundle ( 'en' ) ; i18n . on ( 'languageChanged' , function ( lng ) { addResourceBundle ( lng ) ; } ) ; export default i18n ;