如何转换 div 的语言?

2022-01-18 00:00:00 internationalization php html

我最近在一个项目中工作.在那里我需要通过按钮单击事件将语言从英语转换为日语.文本位于 div 中.像这样:

I am recently working in a project. There I need to convert language from English to Japanese by button click event. The text is in a div. Like this:

"<div id="sampletext"> here is the text </div>"
"<div id="normaltext"> here is the text </div>"

文本来自数据库.如何轻松转换此文本?

The text is come from database. How can I convert this text easily?

推荐答案

假设你的数据库中有英文版和日文版,你可以做两件事:

Assuming that you have both the English and the Japanese version in the database, you can do two things:

  1. 使用 AJAX 从数据库中加载正确的文本并替换 div 的内容.互联网上有大量关于 AJAX 内容替换的教程.
  2. 将两种语言都放在网站上,并使用 CSS display:none 隐藏其中一种.然后在单击按钮时使用一些 JavaScript 隐藏/显示正确的 div.

第一个在技术上更复杂,但可以保持页面较小.第二个很容易做到,但是你的页面比较大,因为你需要发送两种语言.

The first is technically more complex but keeps your page size small. The second one is very easy to do, but your page size is larger because you need to send both languages.

如果 div 很小,页面上只有一两个,我推荐第二个,CSS 技术.如果 div 很大(即一篇完整的文章)或有很多,则使用第一种方法.

If the div is small and there is only one or two of these on the page, I recommend number two, the CSS technique. If the div is large (i.e. a complete article) or there are many of them then use the first method.

相关文章