javascript 如何在不使用 PHP / Jquery 重新加载页面的情况下更改语言
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5284815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change language without reloading page with PHP / Jquery
提问by Mustafa
I am working on a multilangual website right now. I am currently including the related language file ('lange/_en.php') for language phrases. To change languages users will select their language from a < select > item. The thing i want to do is changing related phrases (and urls too if possible) in the page without refreshing or submitting the page.
我现在正在开发一个多语言网站。我目前正在包含语言短语的相关语言文件 ('lange/_en.php')。要更改语言,用户将从 < 选择 > 项目中选择他们的语言。我想要做的是在不刷新或提交页面的情况下更改页面中的相关短语(如果可能,也可以更改 url)。
I remember i saw something like this in web but i have no idea where.
我记得我在网上看到过类似的东西,但我不知道在哪里。
Any help or any ideas about how this thing can be done?
关于如何完成这件事的任何帮助或任何想法?
回答by Andrew Moore
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.
问题在于语言更改不仅会影响页面的一小部分,还会影响整个页面。所以真的,你只剩下三个选择。
The simple waywhich 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.
The complicated waywhich 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 onsettimeout()on your page.The close to impossible waywhich 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 在运行时识别新语言。
通过 AJAX 获取所有新标记并用
<body>重新加载的内容替换标签内容的复杂方法。这将导致运行其他脚本(例如图像轮播等)出现问题,这些脚本持有对元素的引用,因此您必须重新初始化settimeout()页面上正在运行的每个脚本。几乎不可能的方法是拥有一个客户端字典,选择每个相关标签,并使用新语言更改其内容。这是设置和维护的痛苦。您确实需要为每个特定页面量身定制的部分。同样,如果您有带有字符串的脚本,则必须确保它们使用的字符串已更新为新语言。
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.
您最好简单地重新加载页面。它可以在没有 JavaScript 的情况下工作,而且是一次性交易,不会打扰用户。
回答by ngduc
check this plugin out:
检查这个插件:
http://keith-wood.name/localisation.html
It changes language 'on the fly' without going back to server side.
它可以“即时”更改语言,而无需返回服务器端。
回答by Master V
Image reload prototype if-modifiedi posted about image content update to selected language without reloading the page.
图像重新加载原型如果修改我发布了有关图像内容更新到所选语言的信息,而无需重新加载页面。
The image takes the value thats sent and updates to language/country code (which is bound to language) on DB and updates image content (characters) to match their keyboard layout..
图像采用发送的值并更新数据库上的语言/国家代码(与语言绑定)并更新图像内容(字符)以匹配其键盘布局。
Hope its some use
希望它的一些用途

