25
loading...
This website collects cookies to deliver better user experience
npx create-next-app
npm install next-i18next --save
next-i18next
is designed to work hand-in-hand with this new internationalization support.next-i18next
, we need to create an i18next config file at the root of our app called next-i18next.config.js
. This will define the languages we want our app to use. Let’s support English and Swedish.defaultLocale
(the default language for our app) and locales
(a list of languages that we want our app to support):module.exports = {
i18n: {
defaultLocale: 'en',
locales: ['en', 'sv']
}
}
i18n
property to next.config.js
by simply importing the i18n
object from the i18next config:const { i18n } = require('./next-i18next.config');
module.exports = {
i18n,
reactStrictMode: true
}
next-i18next
library uses the same i18n
config structure that Next requires. So instead of having to manage the same settings in two places, we simply import the next-i18next
config into next.config.js
as recommended in the next-i18next docs.next-i18next
is to wrap our app with the appWithTranslation
HOC (higher-order component). This component will provide our i18next context to all of our pages. Our _app.js
file should look like this:import '../styles/globals.css';
import { appWithTranslation } from 'next-i18next';
const MyApp = ({ Component, pageProps }) => <Component {...pageProps} />
export default appWithTranslation(MyApp);
defaultLocale
property in next-i18next.config.js
.sv
(Swedish) to our locales
list in next-i18next.config.js
, we’ll select Swedish:npm install i18nexus-cli -g
i18nexus pull
with our API key and all of our latest translations will be downloaded to our project directory!$ i18nexus pull --api-key <YOUR_API_KEY>
public/locales
, which is where next-i18next
expects them.I18NEXUS_API_KEY
so that we can just use i18nexus pull
without typing your API key every time..env
at the root of our app that contains I18NEXUS_API_KEY=YOUR_API_KEY
.package.json
:...
"scripts": {
"dev": "i18nexus pull && next dev",
"build": "i18nexus pull && next build",
"start": "i18nexus pull && next start"
}
...
i18nexus-cli
as a dev dependency:npm install i18nexus-cli --save-dev
create-next-app
home page:serverSideTranslations
from next-i18next
. This function needs to be run in getStaticProps
on each page-level component. It provides our page with our translations and configuration options as props. We’ll add this to our pages/index.js
:import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
export async function getStaticProps({ locale }) {
return {
props: {
...(await serverSideTranslations(locale, ['home'])),
}
}
}
...
severSideTranslations
function accepts a locale as the first argument and the namespaces required for this page as the second argument. This ensures that our app only has to load the namespaces needed for the page. The locale is passed down from getStaticProps
by Next.useTranslation
hook from next-i18next
. The useTranslation
hook contains a function called t
that takes a key as an argument and renders the proper translation.create-next-app
and just render a single line of text on my Home page. Here’s my entire home page using useTranslation
to render my welcome_msg
string:import Head from "next/head";
import styles from "../styles/Home.module.css";
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
import { useTranslation } from "next-i18next";
export async function getStaticProps({ locale }) {
return {
props: {
...(await serverSideTranslations(locale, ["home"]))
}
};
}
export default function Home() {
const { t } = useTranslation();
return (
<div className={styles.container}>
<Head>
<title>Create Next App</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main className={styles.main}>
<h1 className={styles.title}>{t("home:welcome_msg")}</h1>
</main>
</div>
);
}
useTranslation
, we need to specify the namespace with the key like so: home:welcome_msg
.useTranslation
is when the key we are referencing is in our default namespace. By default, i18next sets our default namespace to “common”. If you’d like, you can change your default namespace via the defaultNS
config option in next-i18next.config.js
.npm run dev
, and check it out!en
because that is what we set as our defaultLocale
in next.config.js
.sv
) to our list of locales. To see our app in Swedish, all we have to do is add /sv
to the end of the URL. If a user’s browser language is set to Swedish, Next will automatically redirect them to the /sv
route. Let’s see what out app looks like in Swedish: