import React , { createContext , useState , useRef , useEffect } from 'react' ; import rosetta from 'rosetta' ; import { defaultLanguage } from '@island.is/shared/constants' ; import { Locale } from '@island.is/shared/types' ; export const isLocale = ( x ) : x is Locale => { return x === 'is' || x === 'en' ; } ; const wrapTranslations = < T extends { [ key ] : string ; } > ( translations ) : T => { if ( process . env . NODE_ENV === 'development' && typeof Proxy !== 'undefined' ) { const warnedKeys = { } ; return new Proxy ( translations , { get ( target , p ) { if ( p in target ) { return target [ p ] ; } if ( ! ( p in warnedKeys ) ) { console . warn ( `Missing translation for ${ p }` ) ; warnedKeys [ p ] = true ; } return p ; } , } ) ; } return translations ; } ; const i18n = rosetta ( ) ; i18n . locale ( defaultLanguage ) ; interface I18nContextType { activeLocale : Locale ; t : any ; locale : ( locale : string , dict ? : object ) => void ; } export const I18nContext = createContext < I18nContextType | null > ( null ) ; export default function I18n ( { children , locale , translations } ) { const [ activeDict , setActiveDict ] = useState ( ( ) => translations ) ; const activeLocaleRef = useRef ( locale || defaultLanguage ) ; const [ , setTick ] = useState ( 0 ) ; const firstRender = useRef ( true ) ; if ( locale && firstRender . current === true ) { firstRender . current = false ; i18n . locale ( locale ) ; i18n . set ( locale , activeDict ) ; } useEffect ( ( ) => { if ( locale ) { i18n . locale ( locale ) ; i18n . set ( locale , activeDict ) ; activeLocaleRef . current = locale ; setTick ( ( tick ) => tick + 1 ) ; } } , [ locale , activeDict ] ) ; const i18nWrapper = { activeLocale : activeLocaleRef . current , t : wrapTranslations ( translations ) , locale : ( l , dict ) => { i18n . locale ( l ) ; activeLocaleRef . current = l ; if ( dict ) { i18n . set ( l , dict ) ; setActiveDict ( dict ) ; } else { setTick ( ( tick ) => tick + 1 ) ; } } , } ; return ( < I18nContext . Provider value = { i18nWrapper } > { children } < /I18nContext.Provider>); }