Skip to content

Instantly share code, notes, and snippets.

@horsfallnathan
Created April 26, 2020 12:48
Show Gist options
  • Save horsfallnathan/588905df66364490631c756bf811abff to your computer and use it in GitHub Desktop.
Save horsfallnathan/588905df66364490631c756bf811abff to your computer and use it in GitHub Desktop.
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