有没有办法检测用户在 input/textarea 字段中输入的语言?我在 facebook 上看到过,如果用户开始输入 RTL 语言,那么光标会移动到输入框的右侧。我试图找到但无法看到任何想法,感谢您的帮助
问问题
10533 次
2 回答
17
https://stackoverflow.com/a/14824756/104380
我想出了一个新的、更短的解决方案:
function isRTL(s){
var ltrChars = 'A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02B8\u0300-\u0590\u0800-\u1FFF'+'\u2C00-\uFB1C\uFDFE-\uFE6F\uFEFD-\uFFFF',
rtlChars = '\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC',
rtlDirCheck = new RegExp('^[^'+ltrChars+']*['+rtlChars+']');
return rtlDirCheck.test(s);
};
于 2013-05-01T17:10:18.587 回答
4
<input type="txt" id="give_lang"></br>
<button onclick='lngtype()'>Language</button>
<div id="lang_her">0</div>
<script type="text/javascript">
function lngtype(text) {
var text = document.getElementById("give_lang").value.replace(/\s/g); //read input value, and remove "space" by replace \s
//Dictionary for Unicode range of the languages
var langdic = {
"arabic" : /[\u0600-\u06FF]/,
"persian" : /[\u0750-\u077F]/,
"Hebrew" : /[\u0590-\u05FF]/,
"Syriac" : /[\u0700-\u074F]/,
"Bengali" : /[\u0980-\u09FF]/,
"Ethiopic" : /[\u1200-\u137F]/,
"Greek and Coptic" : /[\u0370-\u03FF]/,
"Georgian" : /[\u10A0-\u10FF]/,
"Thai" : /[\u0E00-\u0E7F]/,
"english" : /^[a-zA-Z]+$/
//add other languages her
}
//const keys = Object.keys(langdic); //read keys
//const keys = Object.values(langdic); //read values
const keys = Object.entries(langdic); // read keys and values from the dictionary
Object.entries(langdic).forEach(([key, value]) => { // loop to read all the dictionary items if not true
if (value.test(text) == true){ //Check Unicode to see which one is true
return document.getElementById("lang_her").innerHTML = key; //print language name if unicode true
}
});
}
</script>
于 2019-04-25T17:52:08.893 回答