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

uncleseneca-next-i18next

v0.8.0

Published

The easiest way to translate your NextJs apps.

Downloads

2

Readme

next-i18next

npm version CircleCI dependencies Status Package Quality

The easiest way to translate your NextJs apps.

What is this?

next-i18next is a plugin for Next.js projects that allows you to get translations up and running quickly and easily, while fully supporting SSR, multiple namespaces with codesplitting, etc.

While next-i18next uses i18next and react-i18next under the hood, users of next-i18next simply need to include their translation content as JSON files and don't have to worry about much else.

Setup

1. Installation

yarn add next-i18next

2. Translation content

By default, next-i18next expects your translations to be organised as such:

.
├── static
    ├── en
    |   └── common.json
    └── de
        └── common.json

This structure can also be seen in the example directory.

If you want to structure your translations/namespaces in a custom way, you will need to pass modified localePath and localeStructure values into the initialisation config.

3. Project setup

The default export of next-i18next is a class constructor, into which you pass your config options. The resulting class has all the methods you will need to translate your app:

import NextI18Next from 'next-i18next'

const options = {}
export default new NextI18Next(options)

A full list of options can be seen here.

It's recommended to export this NextI18Next instance from a single file in your project, where you can continually import it from to use the class methods as needed. You can see this approach in the example/i18n.js file.

After creating and exporting your NextI18Next instance, you need to take the following steps to get things working:

  1. Create an _app.js file inside your pages directory, and wrap it with the NextI18Next.appWithTranslation higher order component (HOC). You can see this approach in the example/pages/_app.js.
  2. Create a server.js file inside your root directory, initialise an express server, and pass both the express server and NextJs app into NextI18Next.nextI18NextMiddleware. You can see this approach in the example/server.js

That's it! Your app is ready to go. You can now use the NextI18Next.withNamespaces HOC to make your components or pages translatable, based on namespaces:

import React from 'react'

// This is our initialised `NextI18Next` instance
import { withNamespaces } from '../i18n'

class Footer extends React.Component {
  render() {
    return (
      <footer>{this.props.t('description')}</footer>
    )
  }
}

export default withNamespaces('footer')(Footer)

3. Locale subpaths

One of the main features of this package, besides translation itself, are locale subpaths. It's easiest to explain by example:

myapp.com         ---> Homepage in default lang
myapp.com/de/     ---> Homepage in German

This functionality is not enabled by default, and must be passed as an option into the NextI18Next constructor:

new NextI18Next({ localeSubpaths: true })

Now, all your page routes will be duplicated across all your non-default language subpaths. If our static/locales folder included fr, de, and es translation directories, we will automatically get:

myapp.com
myapp.com/fr/
myapp.com/de/
myapp.com/es/

The main "gotcha" with locale subpaths is routing. We want to be able to route to "naked" routes, and not have to worry about the locale subpath part of the route:

<Link href='/some-page'>

With this link, we would expect someone whose language is set to French to automatically be directed to /fr/some-page.

To do that, we must import Link from your NextI18Next, not next/router:

import React from 'react'

// This is our initialised `NextI18Next` instance
import { Link } from '../i18n'

class SomeLink extends React.Component {
  render() {
    return (
      <Link href='/some-page'>
        This will magically prepend locale subpaths
      </Link>
    )
  }
}

Options

| Key | Default value | | ------------- | ------------- | | defaultLanguage | "en" | | otherLanguages | [] | | localePath | 'static/locales' | | localeStructure | '{{lng}}/{{ns}}' | | localeSubpaths | false | | defaultNS | 'common' |

Notes

Contributing

Please do! All PRs and issues will be thoroughly reviewed.