import { Component, ReactNode } from 'react'; import I18nContext from './I18nContext'; import { I18nComponentName, I18nLocaleDataType } from './locale'; export interface II18nReceiverProps { componentName: T; children(i18n: I18nLocaleDataType): ReactNode; } export default class I18nReceiver< T extends I18nComponentName > extends Component> { static contextType = I18nContext; context!: React.ContextType; receive(): I18nLocaleDataType { const { componentName } = this.props; const i18n = this.context[componentName]; return (typeof i18n === 'function' ? i18n() : i18n) as unknown as I18nLocaleDataType; } render() { const { children } = this.props; return children(this.receive()); } }