首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >i18next-browser-languageDetector路径不起作用

i18next-browser-languageDetector路径不起作用
EN

Stack Overflow用户
提问于 2019-02-04 19:07:56
回答 4查看 6.8K关注 0票数 0

我在检测基于路径的语言时遇到了一个问题,即http://localhost:3000/enhttp://localhost:3000/en/subpage应该将我的页面翻译成英语。我可以通过点击按钮并调用i18n.changeLanguage('en')来翻译它,但是检测器似乎不工作。

代码语言:javascript
复制
import i18n from "i18next";
import { reactI18nextModule } from "react-i18next";
import LngDetector from "i18next-browser-languagedetector";
import backend from "i18next-xhr-backend";


const detectionOptions = {
    order: ['path', 'cookie', 'navigator', 'localStorage', 'subdomain', 'queryString', 'htmlTag'],
    lookupFromPathIndex: 0

}


i18n
    .use(LngDetector)
    .use(backend)
    .use(reactI18nextModule) // passes i18n down to react-i18next
    .init({
        ns: ['translation', 'main'],
        defaultNS: 'translation',
        lng: "pl",
        fallbackLng: 'pl',
        detection: detectionOptions,
        keySeparator: false, // we do not use keys in form messages.welcome

        interpolation: {
            escapeValue: false // react already safes from xss
        },
        debug: true,
        react: {
            wait: true
        }
    }, (err, t) => {
        if (err)
            console.error(err)
    });


export default i18n;
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-03-13 22:07:48

解决方案:使用语言检测器时,不应将i18n.lng属性设置为

票数 9
EN

Stack Overflow用户

发布于 2020-05-12 15:44:21

我仍然无法使用i18next-browser-languagedetector的order选项。当同时启用queryString和path optoin时,它将无法正常工作。它总是读取路径并忽略查询部分。

我的代码如下所示。但我猜这是插件的问题吧?从“i18next”导入i18next;从“I18next-browser- LanguageDetector”导入languagedetector;

代码语言:javascript
复制
const detectionOptions = {
  order: ["queryString", "path", "cookie"],
  lookupFromPathIndex: 0,
  lookupQuerystring: "lng",
  lookupCookie: "meine_nanny_i18next",
};

i18next.use(LanguageDetector).init({
  fallbackLng: "de",
  resources: {
    de: {
      common: require("../locales/de/common.json"),
    },
    en: {
      common: require("../locales/en/common.json"),
    },
  },
  ns: ["common"],
  defaultNS: "common",
  detection: detectionOptions,
  returnObjects: true,
  debug: process.env.NODE_ENV === "development",
  interpolation: {
    escapeValue: false, // not needed for react!!
  },
  react: {
    wait: true,
  },
});

i18next.languages = ["de", "en"];

export default i18next;
票数 1
EN

Stack Overflow用户

发布于 2020-09-05 07:10:29

希望这能在未来对某些人有所帮助。文档并没有给出如何设置检测的全貌,然后我发现了一个closed Github issue,其中有几个人问了一个合理的问题,维护人员在他们的回答中有点粗鲁,但也碰巧提供了文档中本应提供的link。它澄清了我的问题,对当前文档中规定要做的事情做了一些小的调整。

然后,我可以通过https:www.domain.com?lng=es在url中检测语言,也可以在使用允许我更改浏览器语言的浏览器扩展时进行检测。

下面是我的i18n.ts工作文件:

代码语言:javascript
复制
import i18n from 'i18next'
import LanguageDetector from 'i18next-browser-languagedetector'
import { initReactI18next } from 'react-i18next'
import XHR from "i18next-http-backend" // <---- add this

import commonDe from './locales/de/common.json'
import commonEn from './locales/en/common.json'
import commonEs from './locales/es/common.json'
import commonFr from './locales/fr/common.json'

const resources = {
  de: { common: commonDe },
  en: { common: commonEn },
  es: { common: commonEs },
  fr: { common: commonFr }
}

const options = {
  order: ['querystring', 'navigator'],
  lookupQuerystring: 'lng'
}

i18n
  .use(XHR) // <---- add this
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    // lng: 'en' // <--- turn off for detection to work
    detection: options,
    resources,
    ns: ['common'],
    defaultNS: 'common',
    fallbackLng: 'en',
    supportedLngs: ['de', 'en', 'es', 'fr'],
    interpolation: {
      escapeValue: false,
    },
    debug: false,
  })

export default i18n

(奖励帮助-如果有人在这一部分卡住了)

我在一个Next.js项目中工作,上面的文件被加载到project-root/pages/_app.tsx文件中,如下所示:

代码语言:javascript
复制
import React from 'react'
import { AppProps } from 'next/app'
import '../i18n/i18n'

import '../public/styles.css'

const TacoFridayApp = ({ Component, pageProps}: AppProps): JSX.Element => {
  
  return <Component {...pageProps} />
}

export default TacoFridayApp
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54514834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档