简体   繁体   English

如何在用户输入时检测用户的语言

[英]how to detect user's language while he/she is typing

guys i have a problem with this thing , i must know which language the user is using while typing in an input text to handle some bad shaped things and i even looked at this in here but it didn't helped me , can u guys help me with the right code ? 伙计们,我有这个问题的问题,我必须知道用户在输入文字时使用哪种语言来处理一些不良形状的东西,我甚至在这里看了这个,但它没有帮助我,你们可以帮助吗我有正确的代码?
the languages i must know are english and persian :| 我必须知道的语言是英语和波斯语:| thanks :) i need some javascript helps 谢谢:) i need some javascript helps

<input type="text" id="give_me_the_lang">
<div id="show_lang_in_here"></div>

Here is the solution that doesn't really detect language, but just checks if users input contains only English alphabet. 这是一个没有真正检测语言的解决方案,只是检查用户输入是否只包含英文字母。 If it does, it writes "English" in div, otherwise it writes "Persian". 如果是,它在div中写“英语”,否则写“波斯语”。 Code: 码:

 document.getElementById("give_me_the_lang").addEventListener("keyup", function() { if (/^[a-zA-Z]+$/.test(this.value)) { document.getElementById("show_lang_in_here").innerHTML = "English"; } else { document.getElementById("show_lang_in_here").innerHTML = "Persian"; } }); 
 <input type="text" id="give_me_the_lang"> <div id="show_lang_in_here"></div> 

Expression ^[a-zA-Z]+$ only checks the letters of English alphabet, if you write in number or any other character, the result will be "Persian". 表达式^[a-zA-Z]+$只检查英文字母的字母,如果你用数字或任何其他字符书写,结果将是“波斯语”。

Detecting language is a task that a plain if else or switch case won't be able to solve... you got a computer learning problem to solve here my friend. 检测语言是一个普通的if或switch案例无法解决的任务......你有一个计算机学习问题要在这里解决我的朋友。

The good thing is, that there are already crazy geniuses out there that already solved your problem, so why don't use their available solution? 好消息是,已经有疯狂的天才已经解决了你的问题,为什么不使用他们的可用解决方案呢?

https://cloud.google.com/translate/v2/getting_started#language_detect https://cloud.google.com/translate/v2/getting_started#language_detect

  1. Signup for the Google Cloud platform 注册Google云端平台
  2. Activate the access to the Google Translate API 激活对Google Translate API的访问权限
  3. Get a Browser access token for your API. 获取API的浏览器访问令牌。 You need this token in order to use it from JavaScript. 您需要此令牌才能在JavaScript中使用它。
  4. Now wire up your UI and let them do the magic. 现在连接你的用户界面,让他们发挥魔力。

Your HTML will remain the same. 您的HTML将保持不变。 Just make sure you also include jQuery. 只要确保你也包含jQuery。

<input type="text" id="give_me_the_lang" />
<div id="show_lang_in_here"></div>

Now lets wire up the UI 现在让我们连接UI

$(function(){

    /* We want to listen to the keypress events from the user 
    *  Each time the user stops typing we wait a few milliseconds and then
    *  we ask Google to detect the language for us */
    var timeoutID;

    /* Replace this with your Google Translate API key */
    var myKey = 'XXXX';

    var detectLanguage = function(){

        $.ajax({
            method: 'GET',
            url: 'https://www.googleapis.com/language/translate/v2/detect',
            data: {
               key: myKey,
               q: $('#give_me_the_lang').val()
            }           
        }).done(function(response){

           /* Push the response to the UI */
           $('#show_lang_in_here').text(JSON.stringify(response.data.detections));
        });        
    };

    $('#give_me_the_lang').on('keyup, keypress, change', function(){
        clearTimeout(timeoutID);

        /* Lets wait half a second */
        setTimeout(detectLanguage, 500);
    });
});

The thing to parse in the response is the array of detections that Google will send back. 要在解析中解析的是Google将发回的一系列检测。

{
    "data": {
       "detections": [
         [
           {
            "language": "de",
            "isReliable": false,
            "confidence": 0.049747143
           }
         ]
       ]
    }
}

Which is a perfect example of overly complicated computation result... so, I would say, if the guy gives you one guess, use it, if the guy gives you more, the pick the one you think is the most approximate for your user base (you don;t have customers in North Korea or in Bolivian Quechua territory do you?) 这是一个过于复杂的计算结果的完美例子...所以,我想说,如果这个人给你一个猜测,使用它,如果这个人给你更多,那么选择你认为最适合你的用户的那个基地(你没有在朝鲜或玻利维亚盖丘亚地区有客户吗?)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在用户当前键入时从用户那里获取数据? - How can I capture a data from the user while he/she is currently typing? 如何检测访问者的国家/地区并将其重定向到特定网站? - How can I detect visitor's country and redirect he/she to an specific website? 如何从Javascript中的Firebase数据库中删除用户的信息(即使他/她当前未登录)? - How to delete a user's info(even if he/she is not currently logged in) from firebase database in javascript? 当用户单击某个按钮时,存储用户的电子邮件 - Store user's email when he/she clicks on a certain button 如何使任何视图知道用户角色(ASP.NET MVC标识)已更改,以强制任何用户在浏览时注销? - How to make any view aware that the User Roles (ASP.NET MVC Identity) have changed in order to force any user to logout while he or she is browsing? 在用户完成输入而不是输入每个字符后,如何获取用户的输入? - How can i get the user's input after he's done typing instead of every character typed? 当用户单击按钮时,将其电子邮件存储在其他页面上 - Store user's email on different page when he/she clicks a button 我如何找到用户仍在输入的搜索值? - How do I find the value a user is search for while he is still typing? 如何在javascript上检测用户的输入语言? - How to detect user's type language on javascript? 在jQuery中,如何在用户输入时检测指定的字符串? - In jQuery, how to detect specified string while user is typing it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM