Skip to content

Instantly share code, notes, and snippets.

@calderaro
Last active August 26, 2020 18:26
Show Gist options
  • Save calderaro/b0478b8f11485a6e590e77f43c7ff3f2 to your computer and use it in GitHub Desktop.
Save calderaro/b0478b8f11485a6e590e77f43c7ff3f2 to your computer and use it in GitHub Desktop.
i18n Simulator
// 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