Created
March 24, 2022 12:00
-
-
Save GarryOne/79b9d8c728c70e3ac4836cbf4ff69841 to your computer and use it in GitHub Desktop.
Custom implementation of useTranslation() from i18next
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 Cookies from 'js-cookie'; | |
import enTrans from '@public/locales/en'; | |
import deTrans from '@public/locales/de'; | |
import esTrans from '@public/locales/es'; | |
import { DEFAULT_LOCALE } from 'src/global/constants'; | |
const useCustomTranslation = () => { | |
const translationsByLocale = { | |
en: enTrans, | |
de: deTrans, | |
es: esTrans | |
} | |
const locale = Cookies.get('NEXT_LOCALE') || DEFAULT_LOCALE; | |
const t = (key: string) => { | |
const [namespace, index] = key.split(':'); | |
const trans = translationsByLocale[locale]; | |
return trans[namespace] && trans[namespace][index]; | |
} | |
return { t }; | |
} | |
export default useCustomTranslation; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment