Characters converter
Posted October 8th 2012, 3:41pm
How can I make using JS cyrillic to latin characters converter. I need it because in my country we use two types of characters: Cyrillic and Latin.

Can somebody give me little code for example: "д" to "d". Thanks in advance. :)
φ
Posts: 37
Joined: August 27th 2011, 11:00pm
Location: Србија/Serbia
Likes Given: 4
Likes Received: 2
Characters converter
Posted October 8th 2012, 7:56pm
You'll need to create an array containing the Latin converted character at the Unicode positions of the corresponding Cyrillic character, and a simple function which uses the array to do the conversion. Here's a simple demo using the characters in your post:

<p id="demo"></p>

<script type="text/javascript">
var cvarray=[];
cvarray[1076]='d'; // 1076 is the Unicode position of 'д'

function UniCvt(unichr) {
return cvarray[unichr.charCodeAt(0)];
}

document.getElementById('demo').innerHTML=UniCvt('д');
</script>

After running, the contents of the id="demo" element will contain the converted character. You can create a loop to convert as many characters as you want. Note: since this is being done via javascript, it will be slow. It is much better suited to server-side scripting (PHP) where character-set conversions are internally supported.
φ
Posts: 1599
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 357
Characters converter
Posted October 8th 2012, 10:36pm
Personally, I think you could make it easier by using an object literal, it depends whether or not you need to then convert back from Latin to Cyrillic. Here's two functions to convert both ways.

(function() {
window.cyr_latin = {
'д' : 'd',
'cyrillic charcter' : 'latin character'
};
window.latin_cyr = {};
for(var c in cyr_latin) latin_cyr[ cyr_latin[c] ] = c;
})();

function toLatin(cyrillic_string) {
for(var i = 0, ret = '', c; (c = cyrillic_string[i++]); ) ret += cyr_latin[c];
return ret;
}

function toCyrillic(latin_string) {
for(var i = 0, ret = '', c; (c = latin_string[i++]); ) ret += latin_cyr[c];
return ret;
}


Alternative to that, you could expand on Dions idea and create the array dynamically. Again saving yourself a lot of work by looking up unicodes.
(function() {
var cyrillic_alphabet = 'дPH'; //I don't know the cyrillic alphabet lol.
var latin_alphabet = 'drn'; //latin alphabet in same order as cyrillic
for(var i = 0, cvarray = [], l = cyrillic_alphabet.length; i < l; i++ ) cvarray[ cyrillic_alphabet.charCodeAt(i) ] = latin_alphabet[ i ];
window.cvarray = cvarray;
})();
φ
Posts: 509
Joined: July 9th 2011, 11:00pm
Location: England
Likes Given: 15
Likes Received: 26
Characters converter
Posted October 8th 2012, 11:49pm
If one is converting a larger amount of text, say 5000 characters, the text will be converted much faster (probably 2 or 3 seconds faster) if using numerically-indexed arrays. That's why I chose the method I did...
φ
Posts: 1599
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 357
Characters converter
Posted October 9th 2012, 3:13am
Thank you Dion & LGForum, I will try something with this scripts, then I will show what I did. :)
φ
Posts: 37
Joined: August 27th 2011, 11:00pm
Location: Србија/Serbia
Likes Given: 4
Likes Received: 2

Who is online

Users browsing this forum: No registered users and 1 guest