How can I convert language of a div?

丶灬走出姿态 提交于 2019-12-03 18:15:53

问题


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?


回答1:


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

  1. Use AJAX to load the correct text from the database and replace the contents of the div. There are tons and tons of tutorials on the internet about AJAX content replacement.
  2. Put both languages on the website and hide one using CSS display:none. Then use some JavaScript to hide/display the correct div when a button is clicked.

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.

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.




回答2:


If you mean translating the text, you cannot do it easily. To get some idea of the best attempts that software can make at translating natural languages, go to Google Translate or Babelfish. It's not that good, but it's sometimes an intelligible starting point.

If you just mean setting the language attribute on an element, then assign a new language code to the lang property of the div element object.

document.getElementById("normaltext").lang = "en-US";

I don't know the language code for Japanese; possibly ja-ja.




回答3:


Assuming your literals have an id in your database you could put that id as a class in your div. Then with jquery fetch the ID, send it to your Ajax back-end and fetch the translated one.




回答4:


First, if you have the texts in a database it really doesn't matter if you render it in divs, tables or whatever.

First you need a php api for some translation service. Here is just an example that might give you some ideas.

$textArray = getTextForThisPage();

?> ...

english_to_japanese($textArray["text1"]);?>

...



来源:https://stackoverflow.com/questions/1135789/how-can-i-convert-language-of-a-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!