Warm tip: This article is reproduced from serverfault.com, please click

Implementing multiple language url nextjs and next-i18n-next

发布于 2020-08-24 05:55:06

I"m about to use the next-i18next internalization library.

https://github.com/isaachinman/next-i18next

I was wondering how I can change the language of the path of my url. Something like this:

/about-us

/over-ons -> goes to dutch version of about us page.

Thanks in advance!

Questioner
Re-Angelo
Viewed
0
Mojtaba Moosavi 2020-11-30 07:17:30

You must set localeSubpaths in your next.config.js

next.config.js

const {nextI18NextRewrites} = require('next-i18next/rewrites');

const localeSubpaths = {
    en: 'en',
    fr: 'fr',
};

module.exports = {
    rewrites: async () => nextI18NextRewrites(localeSubpaths),
    publicRuntimeConfig: {
        localeSubpaths,
    },
};

i18n.js

const NextI18Next = require('next-i18next').default;
const {localeSubpaths} = require('next/config').default().publicRuntimeConfig;
const path = require('path');

module.exports = new NextI18Next({
    defaultLanguage: 'fr',
    otherLanguages: ['en'],
    defaultNS: 'common',
    browserLanguageDetection: false,
    serverLanguageDetection: false,
    localeSubpaths,
    localePath: path.resolve('./public/locales')
});