npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

i18nextnext

v1.1.0-rc1

Published

1i8nextnext will help you to achieve `react-i18next` translations on nextjs, it does support `SSR` and `SSG`.

Downloads

10

Readme

i18nextnext

1i8nextnext will help you to achieve react-i18next translations on nextjs, it does support SSR and SSG.

Installation

  1. Install packages with your prefered package manager:
# npm
npm i i18next react-i18next i18nextnext
# or yarn users
yarn add i18next react-i18next i18nextnext
  1. After constructing your i18n instance, call setI18n for allowing us accessing the translation instances, this is typically done under pages/_app.
import i18n from 'i18next'
import { LocaleBackend } from './backend'
import { setI18n } from 'react-i18next'
import detector from 'i18next-browser-languagedetector'
export { LoadLocales, LocaleScript, NextNextProvider } from 'i18nextnext'

i18n
  .use(detector)
  .use(LocaleBackend)
  .init({ fallbackLng: 'es', react: { useSuspense: false } })

+ setI18n(i18n)
  1. Replace I18nextProvider with NextNextProvider where you initialize react-i18next, this will allow us pre-fill your translations store, this is typically done on the same place as the previous step was done (pages/_app):
import i18n from 'i18next'
import { LocaleBackend } from './backend'
import { setI18n } from 'react-i18next'
import detector from 'i18next-browser-languagedetector'
export { LoadLocales, LocaleScript, NextNextProvider } from 'i18nextnext'

i18n
  .use(detector)
  .use(LocaleBackend)
  .init({ fallbackLng: 'es', react: { useSuspense: false } })

setI18n(i18n)

function App({ Component, pageProps }) {
  return (
-    <I18nextProvider i18n={i18n}>
+    <NextNextProvider i18n={i18n}>
      <UIProvider locale="es">
        <Component {...pageProps} />
      </UIProvider>
-     </I18nextProvider>
+     </NextNextProvider>
  )
}

export default App
  1. Create a custom pages/_document, this will allow us to ship the translations namespaces with the payload received on the UI:
import React from 'react'
+ import { LocaleScript } from 'i18nextnext'
import Document, { Html, Head, Main, NextScript } from 'next/document'

export default class AppDocument extends Document {

  render() {
    return (
      <Html>
        <Head>
+          <LocaleScript />
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}
  1. For preloading translastions, on your page file you should add: SSG:
import { LoadLocales } from 'i18nextnext'

/** Your exported page here **/

export async function getStaticProps() {
  // This will preload various namespaces for a given language.
  await LoadLocales('en', ['dashboard'])
  return {
    props: {},
  }
}

SSR:

import { LoadLocales } from 'i18nextnext'

/** Your exported page here **/

export async function getServerSideProps() {
  // This will preload various namespaces for a given language.
  await LoadLocales('en', ['dashboard'])
  return {
    props: {},
  }
}

API

<LocaleScript />:

This will inject a payload on the initial response to the client, allowing i18n to not request the already-loaded namespaces. This is meant to be used on a NextJS Document (pages/_document) only.

Returns: If LoadLocales was set, will return a <script /> if not, null.

<NextNextProvider i18n={i18n} />:

Wrapper on top of I18nextProvider, it will initialize React-i18next with the pre-filled store from <LocaleScript /> (if present).

Returns: Received Children Props:

  • i18n: Required, i18n: i18n instance.

await LoadLocales(lng: string, namespaces: string[]):

Will pre-load namespaces onto the store, can be used multiple times to load multiple languages, should be used only on getServerSideProps or getStaticProps.

Returns: Promise: An empty promise, fulfilled when resources were loaded.

Parameters:

  • lng: Language to be used, should match i18n language.
  • namespaces: Array of namespaces to be loaded.

Troubleshooting:

Translations flashes:

If you don't see any SSR error on the console, most likely you forgot to include LoadLocales on your page, that means that fallbacks are being used both on SSR/G and initial page until i18next picks the resources.

Warning: Text content did not match. Server: Client:

This is most likely forgot to use <LocaleScript /> or <NextNextProvider />, this means that there are mismatch between HTML content and their DOM representation.

Error: i18n has not been set

You forgot to call setI18n as expected by our plugin on LoadLocales.