Skip to content Skip to sidebar Skip to footer

How To Convert Non-English Characters To English Using JavaScript

I have a c# function which converts all non-english characters to proper characters for a given text. like as follows public static string convertString(string phrase) {

Solution 1:

This should be what you are looking for - check the demo to test.

   function convertString(phrase)
{
    var maxLength = 100;

    var returnString = phrase.toLowerCase();
    //Convert Characters
    returnString = returnString.replace(/ö/g, 'o');
    returnString = returnString.replace(/ç/g, 'c');
    returnString = returnString.replace(/ş/g, 's');
    returnString = returnString.replace(/ı/g, 'i');
    returnString = returnString.replace(/ğ/g, 'g');
    returnString = returnString.replace(/ü/g, 'u');  

    // if there are other invalid chars, convert them into blank spaces
    returnString = returnString.replace(/[^a-z0-9\s-]/g, "");
    // convert multiple spaces and hyphens into one space       
    returnString = returnString.replace(/[\s-]+/g, " ");
    // trims current string
    returnString = returnString.replace(/^\s+|\s+$/g,"");
    // cuts string (if too long)
    if(returnString.length > maxLength)
    returnString = returnString.substring(0,maxLength);
    // add hyphens
    returnString = returnString.replace(/\s/g, "-");  

    alert(returnString);
}

Current Demo

Edit: Updated the demo to add for testing of input.


Solution 2:

function convertString(phrase)
{
 var maxLength = 100;
 var str = phrase.toLowerCase();
 var charMap = {
  'ö': 'o',
  'ç': 'c',
  'ş': 's',
  'ı': 'i',
  'ğ': 'g',
  'ü': 'u'
 };

 var rx = /(ö|ç|ş|ı|ğ|ü)/g;

 // if any non-english charr exists,replace it with proper char
 if (rx.test(str)) {
  str = str.replace(rx, function(m, key, index) {
   return charMap[key];
  });
 }

 // if there are other invalid chars, convert them into blank spaces
 str = str.replace(/[^a-z\d\s-]/gi, "");
 // convert multiple spaces and hyphens into one space       
 str = str.replace(/[\s-]+/g, " ");
 // trim string
 str.replace(/^\s+|\s+$/g, "");
 // cut string
 str = str.substring(0, str.length <= maxLength ? str.length : maxLength);
 // add hyphens
 str = str.replace(/\s/g, "-"); 

 return str;
}

Solution 3:

It's certainly possible to convert it...

ToLower -> toLowerCase, Replace => replace, Length => length

You'd have to code up IndexOfAny, but that's no big deal. But here's my question - why bother to do it client side? Why not call back to the server and execute the code all in one place? I do a lot of stuff like this. Check out the following link:

http://aspalliance.com/1922

It explains a way to bind, client-side, to server-side methods.


Solution 4:

Although this is an old question, this is a problem I face frequently. I therefore wrote a tutorial on how to solve it. It's located here: http://nicoschuele.com/Posts/75.html

The short answer is this: first, you need to treat all the diacritical characters within a function and then, using a dictionnary you build, you need to process all the language specific letters. For example, "à" is a diacritical character and "Ø" is a norwegian letter. My tutorial uses .NET to achieve this but the principle, even in javascript, is the same.


Post a Comment for "How To Convert Non-English Characters To English Using JavaScript"