aralroca / next-translate

Next.js plugin + i18n API for Next.js 🌍 - Load page translations and use them in an easy way!
MIT License
2.62k stars 208 forks source link

translate function in node.js global variable #481

Open ali-hellani opened 3 years ago

ali-hellani commented 3 years ago

I have implemented this in _app.js file

const { t } = useTranslation('common')
global.t = t

the goal is to store the function in node.js global to use it anywhere without importing it every time i want the t function.

My question here is it okay to do that or it will put me into troubles later on? am i missing something?

Note: new to Next.js world

aralroca commented 3 years ago

You can use the getT on Node.js:

import getT from 'next-translate/getT'
// ...
export async function getStaticProps({ locale }) {
  const t = await getT(locale, 'common')
  const title = t('title')
  return { props: { title } }
}
AlexandreCassagne commented 3 years ago

How do you load the locale from this context?

aralroca commented 3 years ago

locale is provided inside getStaticProps arguments by Next.js