Internationalization I18下一个浏览器语言检测器路径不工作
我在检测基于路径的语言时遇到问题,即,或者应该将我的页面翻译为英语。 我可以通过单击按钮并调用i18n.changeLanguage('en')来翻译它,但检测器似乎不起作用Internationalization I18下一个浏览器语言检测器路径不工作,internationalization,i18next,react-i18next,Internationalization,I18next,React I18next,我在检测基于路径的语言时遇到问题,即,或者应该将我的页面翻译为英语。 我可以通过单击按钮并调用i18n.changeLanguage('en')来翻译它,但检测器似乎不起作用 import i18n from "i18next"; import { reactI18nextModule } from "react-i18next"; import LngDetector from "i18next-browser-languagedetector"; import backend from "i
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;
解决方案:使用语言检测器时不应将i18n.lng属性设置为解决方案:使用语言检测器时不应将i18n.lng属性设置为。当同时启用queryString和path optoin时,它将无法正常工作。它还将读取路径并忽略查询部分 我的代码看起来像这样。但我想这是一个插件问题? 从“i18next”导入i18next; 从“i18next browser LanguageDetector”导入LanguageDetector
我对i18next browser languagedetector的订单选项也有问题。当同时启用queryString和path optoin时,它将无法正常工作。它还将读取路径并忽略查询部分 我的代码看起来像这样。但我想这是一个插件问题? 从“i18next”导入i18next; 从“i18next browser LanguageDetector”导入LanguageDetector
希望这对将来的人有所帮助。文档并没有完全告诉您如何设置检测,然后我发现有几个人问了一个合理的问题,维护人员的回答有点粗鲁,但碰巧提供了一个文档中应该包含的问题。它解决了我的问题,对当前文档中的说明做了一些小的调整 然后,我可以通过https:www.domain.com?lng=es
在我的url中进行语言检测,以及使用允许我更改浏览器语言的浏览器扩展时进行语言检测
这是我的工作i18n.ts
文件:
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
希望这对将来的人有所帮助。文档并没有完全告诉您如何设置检测,然后我发现有几个人问了一个合理的问题,维护人员的回答有点粗鲁,但碰巧提供了一个文档中应该包含的问题。它解决了我的问题,对当前文档中的说明做了一些小的调整
然后,我可以通过https:www.domain.com?lng=es在我的url中进行语言检测,以及使用允许我更改浏览器语言的浏览器扩展时进行语言检测
这是我的工作i18n.ts
文件:
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
查询字符串->查询字符串查询字符串->查询字符串谢谢您的回答。这对我很有用,因为我正在寻找查询字符串参数,您给了我一个我很难找到的示例lng=es
。谢谢您的回答。这对我很有用,因为我正在寻找查询字符串参数,您给了我一个我很难找到的示例lng=es
。
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