I am new to web develop and now developing an internal use website by js for different languages user, Its worked great for detect English character input and Chinese character input . and I want further more to detect traditional and simplified Chinese . Any idea to done this task ?
below is my code to detect chinese and english
if (string.match(/^[A-Za-z]*$/)) {
//....English
} else if (string.match(/[\u3400-\u9FBF]/)) {
//....Chinese
} else {
}
I am new to web develop and now developing an internal use website by js for different languages user, Its worked great for detect English character input and Chinese character input . and I want further more to detect traditional and simplified Chinese . Any idea to done this task ?
below is my code to detect chinese and english
if (string.match(/^[A-Za-z]*$/)) {
//....English
} else if (string.match(/[\u3400-\u9FBF]/)) {
//....Chinese
} else {
}
Share
Improve this question
asked Sep 1, 2016 at 4:35
Stephen ChenStephen Chen
3,0572 gold badges28 silver badges40 bronze badges
3
- Use Unicode Characters (eg. \u3400) in "string.match" for traditional Chinese detection and another condition to detect simplified Chinese. Please update here if it doesn't work. – user5312477 Commented Sep 1, 2016 at 4:43
- This link may help you. Chinese Unicode Table - StackOverflow – user5312477 Commented Sep 1, 2016 at 4:48
- thx for the resource , I will tried later ... – Stephen Chen Commented Sep 1, 2016 at 6:14
1 Answer
Reset to default 7I've built a library traditional-or-simplified to detect if a string contains a majority of Traditional or Simplified Chinese characters by paring the number of Simplified/Traditional characters that appear in an input string.
var TradOrSimp = require('traditional-or-simplified');
// Detect if a string contains Simplified Chinese
TradOrSimp.isSimplified('无需注册或设置')
// True
// Detect if a string contains Traditional Chinese
TradOrSimp.isTraditional('無需帳戶或註冊。')
// True
// Detect if a string contains Traditional or Simplified Chinese characters
TradOrSimp.detect('無需帳戶或註冊。')
/*
{ inputLength: 8, // Length of input string
simplifiedCharacters: 0, // Count of Simplified Chinese characters
traditionalCharacters: 4, // Count of Traditional Chinese characters
detectedCharacters: 'traditional', // Detected character set
detectionRate: 1 } // Ratio of majority/minority character sets */