Next-translate - Version 1.0 Released

nextjs javascript react i18n

Today is the day. The Vinissimus Team is very proud and happy to announce the much-anticipated version 1.0 of Next-translate library. It's been a year since the first version 0.1 and a lot happened (+160 closed issues).

Showing version 1.0 when it was experimental

What is Next-translate?

Next-translate is a library to keep the translations as simple as possible in a Next.js environment. It arose from the need in Vinissimus to reduce the bundle size when we realized that the next-i18next library we used occupied 20 times more than Preact. We decided to create our own library with clear goals. In addition, we took advantage of this to support SSG, since next-i18next required the translations to be loaded into a getInitialProps, sacrificing automatic page optimization.

Goals

What does version 1.0 provide?

Next.js plugin

Last year, to achieve the goals of the previous point, we had to create a workaround by doing a "build step" to generate the static pages with all the languages. We had to work in a different directory than "pages". It worked, but it was a bit uncomfortable. Today, in version 1.0, we have been able to remove this workaround while maintaining all the goals.

Now, the Next.js plugin is the new toy. It is responsible for loading the necessary translations on each page through a webpack loader. This way, you don't have to write on each page a getStaticProps, getServerSideProps or any other method you want to use to load the translations. The plugin will take care of it by overwriting the method you have or using a default one (getStaticProps).

Labelai logo
Working with Next-translate 1.0

The plugin is needed to cover the last two goals mentioned in the previous point:

If you don't want the plugin to inject the webpack loader so you can have control over how to load the namespaces on each page, you can use the loader=false in the configuration, and then manually load the namespaces with loadNamespaces.

Improve plurals support

In version 0.x the support of plurals was quite simple. Now with 1.0 we've improved the support by adding 6 plural forms (taken from CLDR Plurals page):

Consume translations outside pages / components

We add the getT asynchronous function to load the t function outside components / pages. It works on both server-side and client-side.

Unlike the useTranslation hook, we can use here any namespace. It doesn't have to be a namespace defined in the "pages" configuration. It will download the namespace indicated as a parameter on runtime.

Example inside getStaticProps:

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

Example inside API Route, ex: /fr/api/example:

import getT from 'next-translate/getT'

export default async function handler(req, res) {
  const t = await getT(req.query.__nextLocale, 'common')
  const title = t('title')

  res.statusCode = 200
  res.setHeader('Content-Type', 'application/json')
  res.end(JSON.stringify({ title }))
}

Useful links

Contributors

During 2020, +20 people contributed to the Next-translate codebase, implementing new features, fixing bugs and issues, writing documentation, and so on. The Vinissimus team would like to thank all of you who helped us build Next-translate to become what it is today.

@vincentducorps, @giovannigiordano, @dnepro, @BjoernRave, @croutonn, @justincy, @YannSuissa, @thanhlmm, @stpch, @shunkakinoki, @rekomat, @psanlorenzo, @pgrimaud, @lone-cloud, @kidnapkin, @hibearpanda, @ftonato, @dhobbs, @bickmaev5, @Faulik, @josephfarina, @gurkerl83, @aralroca

Discuss on Dev.toDiscuss on TwitterEdit on GitHub

Subscribe to new posts! 📩

More...
🏝️ i18n translations in Next.js 13's app-dir for server/client components 🌊

🏝️ i18n translations in Next.js 13's app-dir for server/client components 🌊

Next-translate released 🎉

Next-translate released 🎉

OpenCV directly in the browser (webassembly + webworker)

OpenCV directly in the browser (webassembly + webworker)

Power of Partial Prerendering with Bun

Power of Partial Prerendering with Bun