import React, { useContext } from 'react'; import IntlContext from '@jetshop/intl/context'; import formatMessage from 'format-message/types'; export const useIntl = () => useContext(IntlContext); type FormatMsgType = typeof formatMessage; type FormatMsgTypes = | FormatMsgType | FormatMsgType['rich'] | FormatMsgType['number'] | FormatMsgType['date'] | FormatMsgType['time'] | FormatMsgType['select'] | FormatMsgType['plural'] | FormatMsgType['selectordinal'] | FormatMsgType['custom']; type WithContextFormatMessage = ReturnType; interface TranslateType extends WithContextFormatMessage { rich: ( ...args: Parameters ) => ReturnType; number: ( ...args: Parameters ) => ReturnType; date: ( ...args: Parameters ) => ReturnType; time: ( ...args: Parameters ) => ReturnType; select: ( ...args: Parameters ) => ReturnType; plural: ( ...args: Parameters ) => ReturnType; selectordinal: ( ...args: Parameters ) => ReturnType; custom: ( ...args: Parameters ) => ReturnType; } const withContextFormatMessage = (callback: (t: FormatMsgType) => ReturnType) => (...args: Parameters) => React.createElement(IntlContext.Consumer, null, ((t: FormatMsgType) => callback(t)(...args)) as unknown as React.ReactNode); const Translate = withContextFormatMessage((t) => t) as TranslateType; Translate.rich = withContextFormatMessage((t) => t.rich); Translate.number = withContextFormatMessage((t) => t.number); Translate.date = withContextFormatMessage((t) => t.date); Translate.time = withContextFormatMessage((t) => t.time); Translate.select = withContextFormatMessage((t) => t.select); Translate.plural = withContextFormatMessage((t) => t.plural); Translate.selectordinal = withContextFormatMessage((t) => t.selectordinal); Translate.custom = withContextFormatMessage((t) => t.custom); export const Intl = IntlContext.Consumer; export default Translate;