Language recognition and automatic textbox directi

2019-07-15 15:33发布

问题:

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.