实现多种语言的URL nextjs和next-i18n-next

时间:2020-08-24 05:55:06

标签: javascript reactjs url next.js i18next

我将要使用next-i18next内部化库。

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

我想知道如何更改网址路径的语言。像这样:

/关于我们

/ over-ons->转到关于我们页面的荷兰语版本。

谢谢!

1 个答案:

答案 0 :(得分:1)

您必须在next.config.js中设置localeSubpaths

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')
});