Last active
February 8, 2023 15:21
-
-
Save cpv123/afccb1a87d235b9300547f00180c3b99 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"helloWorld": "Hello world!", | |
"goodbye": "Goodbye | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { ReactNode } from "react"; | |
import type { IntlFormatters } from "react-intl"; | |
import { useIntl as useReactIntl } from "react-intl"; | |
import enMessages from "./en.json"; | |
// Our new union type of all available message IDs. | |
type IntlMessageKeys = keyof typeof enMessages; | |
// The arguments to the original formatMessage function. | |
type FormatMessageArgs = Parameters<IntlFormatters["formatMessage"]>; | |
export function useIntl() { | |
// Pull out the original formatMessage function. | |
const { formatMessage, ...rest } = useReactIntl(); | |
// Re-write the formatMessage function but with an enhanced id type. | |
const typedFormatMessage = ( | |
descriptor: FormatMessageArgs[0] & { | |
id?: IntlMessageKeys; // <--- override the id type | |
}, | |
values?: FormatMessageArgs[1], | |
options?: FormatMessageArgs[2] | |
) => { | |
return formatMessage(descriptor, values, options); | |
}; | |
return { | |
...rest, | |
formatMessage: typedFormatMessage, | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { ReactNode } from "react"; | |
import type { Props as ReactIntlFormattedMessageProps } from "react-intl/src/components/message"; | |
import { FormattedMessage as ReactIntlFormattedMessage } from "react-intl"; | |
import enMessages from "./en.json"; | |
// Our new union type of all available message IDs. | |
type IntlMessageKeys = keyof typeof enMessages; | |
// Extend the original FormattedMessage props. | |
type FormattedMessageProps = ReactIntlFormattedMessageProps< | |
Record<string, ReactNode> | |
> & { | |
id?: IntlMessageKeys; | |
}; | |
export function FormattedMessage({ id, ...rest }: FormattedMessageProps) { | |
return <ReactIntlFormattedMessage id={id} {...rest} />; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { ReactNode } from "react"; | |
import type { IntlFormatters } from "react-intl"; | |
import type { Props as ReactIntlFormattedMessageProps } from "react-intl/src/components/message"; | |
import { | |
FormattedMessage as ReactIntlFormattedMessage, | |
useIntl as useReactIntl, | |
} from "react-intl"; | |
import enMessages from "./en.json"; | |
// Our new union type of all available message IDs. | |
type IntlMessageKeys = keyof typeof enMessages; | |
// The arguments to the original formatMessage function. | |
type FormatMessageArgs = Parameters<IntlFormatters["formatMessage"]>; | |
// Extend the original FormattedMessage props. | |
type FormattedMessageProps = ReactIntlFormattedMessageProps< | |
Record<string, ReactNode> | |
> & { | |
id?: IntlMessageKeys; | |
}; | |
export function FormattedMessage({ id, ...rest }: FormattedMessageProps) { | |
return <ReactIntlFormattedMessage id={id} {...rest} />; | |
} | |
export function useIntl() { | |
// Pull out the original formatMessage function. | |
const { formatMessage, ...rest } = useReactIntl(); | |
// Re-write the formatMessage function but with a strongly-typed id. | |
const typedFormatMessage = ( | |
descriptor: FormatMessageArgs[0] & { | |
id?: IntlMessageKeys; | |
}, | |
values?: FormatMessageArgs[1], | |
options?: FormatMessageArgs[2] | |
) => { | |
return formatMessage(descriptor, values, options); | |
}; | |
return { | |
...rest, | |
formatMessage: typedFormatMessage, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment