[英]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
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.