检测用户输入语言javascript

时间:2013-05-01 14:48:31

标签: javascript html

有没有办法检测用户在input / textarea字段中输入的语言? 我在facebook上看过这样的话,如果用户开始输入RTL语言,那么光标会在输入框的右侧移动。 我试图找到,但不能看到任何想法,谢谢你的帮助

2 个答案:

答案 0 :(得分:10)

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);
};

playground page

答案 1 :(得分:0)

import requests

urls = ['https://www.khanacademy.org/api/internal/user/kaid_329989584305166460858587/profile/widgets?lang=en&_=190424-1429-bcf153233dc9_1556201931959',
        'https://www.khanacademy.org/api/internal/user/kaid_901866966302088310331512/profile/widgets?lang=en&_=190424-1429-bcf153233dc9_1556201931959']

for url in urls:
    response = requests.get(url).json()
    result = []
    for item in response:
        try:
            result.append(item['renderData']['discussionData']['statistics']['votes'])
        except KeyError:
            pass # Could not find votes
    print(result)