Language recognition and automatic textbox direction switch

耗尽温柔 提交于 2019-12-10 23:35:27

问题


Say I have a textbox in HTML using the following code:

<input type="text" name="text" id="text" />

And my site is intended to be for right-to-left as well as left-to-right languages. That means that I have some textboxes that will be typed in a right-to-left language, but the email textbox, for example, will be left-to-right.

My question is not how to declare specific direction using CSS. Please no CSS here.

My question is if it's possible to use javascript to automatically detect the language, and seeing if it's a right-to-left language, make the text-direction go from right-to-left, or if it's a left-to-right language, make the text direction go from left-to-right. (AKA, it could possibly auto detect the language, THEN set the CSS "direction: rtl;" or whatever).

Is this possible to accomplish using javascript? (I know google does it in their google translate and various other sites do it as well, I was just wondering if it's difficult to implement)...

Thanks, Amit


回答1:


Use the Google API! They have a Language Detect example here: http://code.google.com/apis/ajax/playground/#language_detect

--

What I would do is create a database with two columns: "LANGUAGE" and "FORMAT". Fill in the database with the languages/formats (i.e "fr" and "left-to-right"), and when you get the language result from the Google API, you find it in your database and get its format.



来源:https://stackoverflow.com/questions/3453036/language-recognition-and-automatic-textbox-direction-switch

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