import React , { useContext } from 'react' ; import defaultLocales from './locales' ; type ILocales = { [ k ] : any ; } ; type ILocaleContext = { locale ? : string ; locales ? : ILocales ; } ; const LocaleContext = React . createContext < ILocaleContext > ( undefined ! ) ; const DEFAULT_LOCALE = 'en-US' ; const LocaleProvider = ( { locale , locales , children } ) => ( < LocaleContext . Provider value = { { locale , locales } } > { children } < /LocaleContext.Provider>); LocaleProvider . defaultProps = { locale : DEFAULT_LOCALE , } ; const useLocale = ( comp ? , fallback ? ) => { const localeContext = useContext ( LocaleContext ) ; const { locale , locales } = localeContext || { } ; const defaultStrings = ( locale && ( defaultLocales as ILocales ) [ locale ] ) || defaultLocales [ DEFAULT_LOCALE ] ; let strings = locales ? { ... defaultStrings , ... locales } : defaultStrings ; if ( ! localeContext ) { strings = fallback ; } else if ( comp ) { strings = strings [ comp ] || { } ; } return { locale , trans : ( key ? ) => ( key ? strings [ key ] : strings ) , } ; } ; export { LocaleProvider , LocaleContext , useLocale } ;