简体   繁体   English

通过Url的路径确定语言

[英]Determining the language by the path of Url

I tried to find a way to change the language by changing the site's sub-path in the next-i18next package, I searched the Internet ( https://github.com/isaachinman/next-i18next/issues/32 , https://github.com/i18next/i18next-browser-languageDetector#detector-options ) for an answer to this question, but it did not work.我试图通过在next-i18next包中更改站点的子路径来找到一种更改语言的方法,我在互联网上搜索( https://github.com/isaachinman/next-i18next/issues/32https:// /github.com/i18next/i18next-browser-languageDetector#detector-options )来回答这个问题,但它没有用。 After changing the subpath in the url it is duplicated and redirects me to a page that does not exist.更改 url 中的子路径后,它会被复制并将我重定向到一个不存在的页面。

在此处输入图片说明

my code:我的代码:

// path-to-my-project/i18n.js
const NextI18Next = require('next-i18next').default;
const i18nextBrowserLanguageDetector = require('i18next-browser-languagedetector').default;
const { localeSubpaths } = require('next/config').default().publicRuntimeConfig;
const path = require('path');

module.exports = new NextI18Next({
    otherLanguages: ['ru'],
    defaultNS: 'common',
    localeSubpaths,
    localePath: path.resolve('./public/static/locales'),
    use: [i18nextBrowserLanguageDetector],
});
// path-to-my-project/pages/_app.js
import '../styles/main.scss';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import Router from 'next/router';
import App from 'next/app';
import { appWithTranslation } from '../i18n';

Router.events.on('routeChangeStart', () => NProgress.start());
Router.events.on('routeChangeComplete', () => NProgress.done());
Router.events.on('routeChangeError', () => NProgress.done());

const MyApp = ({ Component, pageProps }) => (
    <Component {...pageProps} />
);

MyApp.getInitialProps = async (appContext) => ({ ...await App.getInitialProps(appContext) });

export default appWithTranslation(MyApp);

maybe I just missed something, because it's my first project on next.js, so I'm asking for help in the community and would be grateful for any help or hint.也许我只是错过了一些东西,因为这是我在 next.js 上的第一个项目,所以我在社区中寻求帮助,并感谢任何帮助或提示。

By default next-i18next will try to detect the language to show from users browser .默认情况next-i18next将尝试检测用户浏览器显示的语言。

Try to disable it.尝试禁用它。

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

module.exports = new NextI18Next({
  browserLanguageDetection: false, // <---
  serverLanguageDetection: false, // <---
  otherLanguages: ['de'],
  localeSubpaths,
  localePath: path.resolve('./public/static/locales')
})

in file next.config.js i have this settings:在文件 next.config.js 我有这个设置:

// path/to/project/next.config.js

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

const localeSubpaths = {
    ru: 'ru',
};

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

but there was not enough configuration for English localization, so you just need to add it:但是没有足够的英文本地化配置,所以你只需要添加它:

// path/to/project/next.config.js

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

const localeSubpaths = {
    en: 'en', // <------
    ru: 'ru',
};

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM