简体   繁体   English

带有 i18next 的多语言网站。 记住语言选择

[英]Multiple languange website with i18next. Memorize language choice

I'm building a multi-language website.我正在建立一个多语言网站。 The problem is that the language selection is not memorized when reloading the page.问题是重新加载页面时不会记住语言选择。

My webpage consists of one landing page and some other pages with additional information.我的网页由一个登录页面和一些包含附加信息的其他页面组成。 I would like that the language choice is remembered and applied also when loading the other pages.我希望在加载其他页面时也能记住并应用语言选择。 Link to my page: https://merry-sprite-2d2487.netlify.app/index.html链接到我的页面: https://merry-sprite-2d2487.netlify.app/index.html

To implement the multi language I used this guideline and basically the same code: https://idesignzone.com/how-to-create-a-multilingual-website-in-html/#comment-48为了实现多语言,我使用了这个指南和基本相同的代码: https://idesignzone.com/how-to-create-a-multilingual-website-in-html/#comment-48

I'm pretty new to coding and just learned it to create my site.我对编码很陌生,刚刚学会了它来创建我的网站。 If answering please keep it simple and easy;) Thank you!如果回答,请保持简单易行;)谢谢!

I tried to replace lng: "eng" with lng: localStorage.我试图用 lng: localStorage 替换 lng: "eng"。 getItem(key) ||获取项目(键)|| "eng" as was suggested in another post but then it didn't load anything. “eng”正如另一篇文章中所建议的那样,但它没有加载任何东西。

you have 3 choices:你有3个选择:

initially, you detect navigator language and you store it using the choice you'll make in the way of storing.最初,您检测导航器语言并使用您在存储方式中做出的选择来存储它。 And you update this value, same way, each time user choose another language.每次用户选择另一种语言时,您都会以同样的方式更新此值。

At page loading, first thing you read the value, if nothing you detect navigator language and store it在页面加载时,首先读取值,如果没有检测到导航器语言并存储它

  • cookie曲奇饼
  • localStorage本地存储
  • URL, adding en=XX after the # URL,#后加en=XX

I won't describe all the ways, you can find a lot on each easily in searching.我不会描述所有的方法,你可以在搜索中很容易地找到很多。

But pro and cons (my opinion only, certainly other people can have other opinions)但有利有弊(我的意见而已,当然其他人可以有其他意见)

cookie pros饼干专家

  • easy to use使用方便
  • not visible by user cookie cons用户 cookie 缺点不可见
  • can have strange behaviour with certain browser某些浏览器可能会有奇怪的行为
  • normally, if european union you have to make a box message at beginning saying you are using cookie, and user has to agree通常情况下,如果是欧盟,你必须在开始时制作一个框消息说你正在使用 cookie,并且用户必须同意

localStorage pros本地存储专家

  • easy to use使用方便
  • not visible by user localStorage cons用户 localStorage 缺点不可见
  • almost nothing几乎没有

URL pros URL 亲

  • it's visible: you, user and SEO see it URL cons它是可见的:你、用户和 SEO 看到它 URL 缺点
  • it's visible (hehe): user see it它是可见的(呵呵):用户看到它
  • it's harder to implement, not a lot but harder实施起来更难,不是很多,而是更难

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

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