且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在不使用PHP / Jquery重新加载页面的情况下更改语言

更新时间:2022-05-26 00:40:46

这个问题是语言更改不仅会影响页面的一小部分,而且会影响整个页面。所以真的,你有三个选择。

The issue with this is that a language change doesn't only affect a small section of the page, it affects the whole page. So really, you are left with three choices.


  1. 简单方法确实正在重新加载整页。它易于实现,易于维护,并且不需要您确保页面上当前运行的JavaScript在运行时知道新语言。

  1. The simple way which is indeed reloading the whole page. It's easy to implement, easy to maintain, and doesn't require you to make sure that JavaScript currently running on your page is aware of the new language at runtime.

复杂的方式,它通过AJAX获取所有新标记,并用重新加载的内容替换< body> 标记的内容。这将导致运行其他脚本(例如图像轮播等)的问题,这些脚本包含对元素的引用,因此您必须重新初始化在 settimeout()$ c $上运行的每个脚本。 c>在你的页面上。

The complicated way which is getting all the new markup via AJAX and replacing the content of the <body> tag with the reloaded content. This will cause issues with other scripts running (such as image carousels, etc.) that holds a reference to an element so you have to reinitialize every single script that is running on settimeout() on your page.

接近不可能的方式这是一个客户端字典,选择每个相关标签,并用新语言改变其内容。这是一种痛苦的设置和维护的痛苦。您确实需要为每个特定页面量身定制的部分。同样,如果你有带字符串的脚本,你必须确保他们使用的字符串更新为新语言。

The close to impossible way which is to have a client side dictionary, selecting each relevant tag, and changing its contents with the new language. This is a pain to setup and a pain to maintain. You literally need a section tailored to each specific page. Again, if you have scripts with strings, you'll have to make sure that the strings they use are updated to the new language.

***只是重新加载页面。它可以在没有JavaScript的情况下工作,这是一次性的交易,不会打扰用户。

You are better off simply reloading the page. It will work without JavaScript and it's a one time deal that won't bother users.