Created
September 3, 2019 13:40
-
-
Save fwojciec/ba3671963cd0fb6c74586e295ae7866f to your computer and use it in GitHub Desktop.
How to build a multilingual website in Next.js #8
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
// hooks/useTranslation.ts | |
import { useContext } from 'react' | |
import { LocaleContext } from '../context/LocaleContext' | |
import strings from '../translations/strings' | |
import { defaultLocale } from '../translations/config' | |
export default function useTranslation() { | |
const { locale } = useContext(LocaleContext) | |
function t(key: string) { | |
if (!strings[locale][key]) { | |
console.warn(`Translation '${key}' for locale '${locale}' not found.`) | |
} | |
return strings[locale][key] || strings[defaultLocale][key] || '' | |
} | |
return { | |
t, | |
locale | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment