Last active
August 26, 2020 18:26
-
-
Save calderaro/b0478b8f11485a6e590e77f43c7ff3f2 to your computer and use it in GitHub Desktop.
i18n Simulator
This file contains hidden or 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
// this is a simulation of react-i18n's API | |
import React, { ComponentType } from 'react'; | |
import get from 'lodash/get'; | |
import { Diff } from 'utility-types'; | |
type Labels = { [key: string]: string }; | |
export type WithTranslation = { t: (key: string) => string }; | |
export const t = (labels: Labels) => (key: string): string => get(labels, key, key); | |
export const withTranslation = (labels: Labels) => <T extends WithTranslation>(WrappedComponent: ComponentType<T>) => ( | |
props: Diff<T, WithTranslation> | |
) => <WrappedComponent {...(props as T)} t={t(labels)} />; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment