Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript detect input character is traditional Chinese or simplified Chinese

Tags:

javascript

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 {

  }
like image 449
Stephen Chen Avatar asked Sep 01 '16 04:09

Stephen Chen


1 Answers

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 */
like image 163
nickdrewe Avatar answered Oct 26 '22 15:10

nickdrewe