import * as React from 'react'; export type I18nFn = ( key: string, substitutions?: Array ) => string; const I18nContext = React.createContext(() => 'NO LOCALE LOADED'); export type I18nProps = { children: React.ReactNode; messages: { [key: string]: { message: string } }; }; export const I18n = ({ messages, children }: I18nProps) => { const getMessage = React.useCallback( (key, substitutions = []) => substitutions.reduce( (res, sub) => res.replace(/\$.+?\$/, sub), messages[key].message ), [messages] ); return ( {children} ); }; export const useI18n = () => React.useContext(I18nContext);