Created
April 26, 2020 12:48
-
-
Save horsfallnathan/588905df66364490631c756bf811abff to your computer and use it in GitHub Desktop.
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
function useTranslation() { | |
// here I make locale default to the defaultLocale value | |
const { locale = defaultLocale } = useContext(LocaleContext); | |
function t(key) { | |
const pathArr = [locale, ...key.split(".")]; | |
const translation = pathArr.reduce((obj, path) => { | |
return (obj || {})[path]; | |
}, dict); | |
console.log(translation); | |
if (!translation) { | |
console.warn(`Translation '${key}' for locale '${locale}' not found.`); | |
} | |
return translation || ""; | |
} | |
return { | |
t, | |
locale, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment