Skip to content

Instantly share code, notes, and snippets.

@fwojciec
Created September 3, 2019 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fwojciec/ba3671963cd0fb6c74586e295ae7866f to your computer and use it in GitHub Desktop.
Save fwojciec/ba3671963cd0fb6c74586e295ae7866f to your computer and use it in GitHub Desktop.
How to build a multilingual website in Next.js #8
// 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