简体   繁体   English

当我使用 i18next 处理翻译时,如何在 React/Gatsby 应用程序中添加路由?

[英]How to add routes in a React/Gatsby app when I use i18next to handle translation?

so recently I have been implementing an internationalization feature in my Gatsby app, but it should be similar to a React app, so if you know React, please tell me your experience too, thanks!所以最近我在我的 Gatsby 应用程序中实现了一个国际化功能,但它应该类似于一个 React 应用程序,所以如果你知道 React,请也告诉我你的经验,谢谢!

To enable the translation feature, I use react-i18next .为了启用翻译功能,我使用react-i18next My file structure looks like this:我的文件结构如下所示:

-src
  -locale
   -en
     -translation.js
   -zh
     -translation.js
  -pages
   -index.js
   -......js

So what is going on is that, I extract all the text on files in pages folder and put them in translation.js in different language folders, and when I click a button to change the language, all the files in pages will find the corresponding text from different language translation.js file accordingly.那么是怎么回事呢,我把pages文件夹中文件的所有文本都提取出来放到translation.js在不同的语言文件夹中,当我点击一个按钮改变语言时, pages所有文件都会找到对应的相应地来自不同语言的translation.js文件中的文本。

But the route doesn't change at all.但路线根本没有改变。 Is there a way for me to be able to do this: when I change the language, my route will change from /en/index to /zh/index ?有没有办法让我能够做到这一点:当我更改语言时,我的路线将从/en/index更改为/zh/index

I know that I can make different pages, but I don't want to do that, I want to keep my current handling to separate my text from the app.我知道我可以制作不同的页面,但我不想那样做,我想保持当前的处理方式以将我的文本与应用程序分开。 Is there a package that I can use or what do you use to handle this?有没有我可以使用的包,或者你用什么来处理这个问题? Thank you!谢谢!

Changing the language itself will not change the route.改变语言本身不会改变路线。 You will need to use the gatsby router to do that (via: await navigate('/:lang/path/slug') ).您将需要使用 gatsby 路由器来执行此操作(通过: await navigate('/:lang/path/slug') )。 Butt like @jMadelaine said, you shouldn't need to change the route as it supports multi-language url routes in a single page component.就像@jMadelaine 所说的那样,您不需要更改路由,因为它支持单个页面组件中的多语言 url 路由。

However, if you wish to do so can do the following below:但是,如果您愿意,可以执行以下操作:

You can use the i18next-browser-languagedetector plugin and will need to include path or querystring in the detection.order array to be initialized in the i18next config for language detection.您可以使用i18next-browser-languagedetector插件,并且需要在detection.order数组中包含pathquerystring ,以便在 i18next 配置中初始化以进行语言检测。 https://github.com/i18next/i18next-browser-languageDetector#detector-options https://github.com/i18next/i18next-browser-languageDetector#detector-options

Then when initializing the i18next client you can use the language detector like this for example:然后在初始化 i18next 客户端时,您可以使用语言检测器,例如:

import i18next from "i18next"
import LanguageDetector from "i18next-browser-languagedetector"

const detection = {
  // order and from where user language should be detected
  order: [
    "querystring",
    "cookie",
    "localStorage",
    "navigator",
    "htmlTag",
    "path",
    "subdomain",
  ],

  // keys or params to lookup language from
  lookupQuerystring: "lng",
  lookupCookie: "i18next",
  lookupLocalStorage: "i18nextLng",
  lookupFromPathIndex: 0,
  lookupFromSubdomainIndex: 0,
  ...
}

i18next.use(LanguageDetector).init({
  detection, // <-- passed in here
  whitelist: ["en", "es"],
  fallbackLng: "en",
  resources: i18nResources,
  ns: ["translations"],
  defaultNS: "translations",
  returnObjects: true,
  debug: process.env.NODE_ENV === "development",
  react: { wait: true },
  keySeparator: false,
  supportedLngs: ["en", "es"],
  lowerCaseLng: true,
})

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

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