Created
August 1, 2021 17:19
-
-
Save rajibahmed/aea146cf82d86e15c19cfd7ca8e3c3a7 to your computer and use it in GitHub Desktop.
Simple i18n with typescript
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
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