Reputation: 3057
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 {
}
Upvotes: 6
Views: 4723
Reputation: 86
I've built a library traditional-or-simplified to detect if a string contains a majority of Traditional or Simplified Chinese characters by comparing 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 */
Upvotes: 7