Skip to content

Instantly share code, notes, and snippets.

@rajibahmed
Created August 1, 2021 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajibahmed/aea146cf82d86e15c19cfd7ca8e3c3a7 to your computer and use it in GitHub Desktop.
Save rajibahmed/aea146cf82d86e15c19cfd7ca8e3c3a7 to your computer and use it in GitHub Desktop.
Simple i18n with typescript
const translations = {
en: {
searchLable: 'Add to',
addProduct: 'Add product',
menuHeader: 'MENU',
searchHeader: 'SEARCH',
},
sv: {
searchLable: 'Lägg till vara',
addProduct: 'Lägg till',
menuHeader: 'MENY',
searchHeader: 'LÄGG TILL',
},
};
type TranslationType = typeof translations;
type LanguageKeys = keyof TranslationType;
type TranslationKeys = keyof TranslationType['en'];
const translate = (translations: TranslationType) => (lang: LanguageKeys) => (
key: TranslationKeys
) => {
return translations[lang][key];
};
export const t = translate(translations)('sv');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment