Search code examples
c#javascriptunicodekeyboard-layouthindi

How i can get the word who user type for a special word in non-english language in any language even c# , javascript


Assume that a Non-English user types Namaste Duniya. I need to show नमस्ते दुनिया in Hindi Language. if i have नमस्ते दुनिया stored in a variable then how i can get the word that was typed by the user through keyboard and to show them in unicode. Like in this example namaste Duniya was typed for showing them नमस्ते दुनिया.

So my question is can I convert the chars to match the word they (users) typed in the keyboard? How i can do this in c# or any other language like javascript?


Solution

  • This one seems to handle your texts

    http://pinyin.info/tools/converter/chars2uninumbers.html

    नमस्ते दुनिया became नमस्ते दुनिया

    The code used is quite simple:

      var iString = "नमस्ते दुनिया", oString ="";
      for (var i=0; i<iString.length; i++) {
        oString += (iString.charCodeAt(i)<=127)? iString.charAt(i) : '&#' + iString.charCodeAt(i) + ';': 
      }
    

    UPDATE:

    If I understand the new text of the question, then something like

    var translateLatin2Hindi = { 
    "Namaste Duniya":"नमस्ते दुनिया",
    ,
    ,
    
    "Duniya Namaste":"दुनिया नमस्ते"
    }
    
    var translateHindi2Latin = {}
    for (var o in translateLatin2Hindi) {
      translateHindi2Latin[translateLatin2Hindi[o]]=o;
    }
    

    which then can be used as

    function getHindi(latinString) {
      var val = translateLatin2Hindi[latinString];
      return (val)?val:"Not found";
    }    
    function getLatin(hindiString) {
      var val = translateHindi2Latin[hindiString];
      return (val)?val:"Not found";
    }