Transliterating Russian to English in JavaScript
Transliterating a text in Russian to English in JavaScript. Three ways:
- Characters are stored in a JSON object
- Characters are stored in an array
- Replacing characters in a text one by one with replace()
1. Characters in JSON object
Transliterating: "Транслитерация с русского на английский с помощью JavaScript"
HTML code:
<div class="example"></div>
JavaScript code:
<script>
var text = 'Транслитерация с русского на английский с помощью JavaScript #1';
var result = '';
var characters = {'а':'a', 'б':'b', 'в':'v', 'г':'g', 'д':'d', 'е':'e', 'ё':'yo', 'ж':'zh', 'з':'z', 'и':'i', 'й':'y', 'к':'k', 'л':'l', 'м':'m', 'н':'n', 'о':'o', 'п':'p', 'р':'r', 'с':'s', 'т':'t', 'у':'u', 'ф':'f', 'х':'h', 'ц':'c', 'ч':'ch', 'ш':'sh', 'щ':'shch', 'ь':'', 'ы':'y', 'ъ':'', 'э':'e', 'ю':'yu', 'я':'ya', 'А':'A', 'Б':'B', 'В':'V', 'Г':'G', 'Д':'D', 'Е':'E', 'Ё':'Yo', 'Ж':'Zh', 'З':'Z', 'И':'I', 'Й':'Y', 'К':'K', 'Л':'L', 'М':'M', 'Н':'N', 'О':'O', 'П':'P', 'Р':'R', 'С':'S', 'Т':'T', 'У':'U', 'Ф':'F', 'Х':'H', 'Ц':'C', 'Ч':'Ch', 'Ш':'Sh', 'Щ':'Shch', 'Ь':'', 'Ы':'Y', 'Ъ':'', 'Э':'E', 'Ю':'Yu', 'Я':'Ya'};
for ( var i = 0; i < text.length; i++ ) {
if ( characters[ text[i] ] == undefined ){
result += text[i];
} else {
result += characters[ text[i] ];
}
};
// Printing the resulting text
document.querySelector('.example').textContent = result;
</script>
2. Characters in array
Transliterating: "Транслитерация с русского на английский с помощью JavaScript #2"
HTML code:
<div class="example2"></div>
JavaScript code:
<script>
var text = 'Транслитерация с русского на английский с помощью JavaScript #2';
var result2 = '';
var chars = [];
chars['А']='A'; chars['Б']='B'; chars['В']='V'; chars['Г']='G'; chars['Д']='D'; chars['Е']='E'; chars['Ё']='YO'; chars['Ж']='ZH'; chars['З']='Z'; chars['И']='I'; chars['Й']='Y'; chars['К']='K'; chars['Л']='L'; chars['М']='M'; chars['Н']='N'; chars['О']='O'; chars['П']='P'; chars['Р']='R'; chars['С']='S'; chars['Т']='T'; chars['У']='U'; chars['Ф']='F'; chars['Х']='H'; chars['Ц']='C'; chars['Ч']='CH'; chars['Ш']='SH'; chars['Щ']='SHCH'; chars['Ь']=''; chars['Ы']='Y'; chars['Ъ']=''; chars['Э']='E'; chars['Ю']='YU'; chars['Я']='YA'; chars['а']='a'; chars['б']='b'; chars['в']='v'; chars['г']='g'; chars['д']='d'; chars['е']='e'; chars['ё']='yo'; chars['ж']='zh'; chars['з']='z'; chars['и']='i'; chars['й']='y'; chars['к']='k'; chars['л']='l'; chars['м']='m'; chars['н']='n'; chars['о']='o'; chars['п']='p'; chars['р']='r'; chars['с']='s'; chars['т']='t'; chars['у']='u'; chars['ф']='f'; chars['х']='h'; chars['ц']='c'; chars['ч']='ch'; chars['ш']='sh'; chars['щ']='shch'; chars['ь']=''; chars['ы']='i'; chars['ъ']=''; chars['э']='e'; chars['ю']='yu'; chars['я']='ya';
for ( var i = 0; i < text.length; ++i ) {
if ( chars[ text[i] ] == undefined ){
result2 += text[i];
} else {
result2 += chars[ text[i] ];
}
}
// Printing the resulting text
document.querySelector('.example2').textContent = result2;
</script>
3. Replacing characters one by one with replace()
Transliterating: "Транслитерация с русского на английский с помощью JavaScript #3"
HTML code:
<div class="example3"></div>
JavaScript code:
<script>
var text = 'Транслитерация с русского на английский с помощью JavaScript #3';
text = text.replace(/\u0410/g, 'А');
text = text.replace(/\u0411/g, 'B');
text = text.replace(/\u0412/g, 'V');
text = text.replace(/\u0413/g, 'G');
text = text.replace(/\u0414/g, 'D');
text = text.replace(/\u0415/g, 'E');
text = text.replace(/\u0401/g, 'Yo');
text = text.replace(/\u0416/g, 'Zh');
text = text.replace(/\u0417/g, 'Z');
text = text.replace(/\u0418/g, 'I');
text = text.replace(/\u0419/g, 'Y');
text = text.replace(/\u041A/g, 'K');
text = text.replace(/\u041B/g, 'L');
text = text.replace(/\u041C/g, 'M');
text = text.replace(/\u041D/g, 'N');
text = text.replace(/\u041E/g, 'O');
text = text.replace(/\u041F/g, 'P');
text = text.replace(/\u0420/g, 'R');
text = text.replace(/\u0421/g, 'S');
text = text.replace(/\u0422/g, 'T');
text = text.replace(/\u0423/g, 'U');
text = text.replace(/\u0424/g, 'F');
text = text.replace(/\u0425/g, 'H');
text = text.replace(/\u0426/g, 'C');
text = text.replace(/\u0427/g, 'Ch');
text = text.replace(/\u0428/g, 'Sh');
text = text.replace(/\u0429/g, 'Shch');
text = text.replace(/\u042A/g, '');
text = text.replace(/\u042B/g, 'Y');
text = text.replace(/\u042C/g, '');
text = text.replace(/\u042D/g, 'E');
text = text.replace(/\u042E/g, 'Yu');
text = text.replace(/\u042F/g, 'Ya');
text = text.replace(/\u0430/g, 'a');
text = text.replace(/\u0431/g, 'b');
text = text.replace(/\u0432/g, 'v');
text = text.replace(/\u0433/g, 'g');
text = text.replace(/\u0434/g, 'd');
text = text.replace(/\u0435/g, 'e');
text = text.replace(/\u0451/g, 'yo');
text = text.replace(/\u0436/g, 'zh');
text = text.replace(/\u0437/g, 'z');
text = text.replace(/\u0438/g, 'i');
text = text.replace(/\u0439/g, 'y');
text = text.replace(/\u043A/g, 'k');
text = text.replace(/\u043B/g, 'l');
text = text.replace(/\u043C/g, 'm');
text = text.replace(/\u043D/g, 'n');
text = text.replace(/\u043E/g, 'o');
text = text.replace(/\u043F/g, 'p');
text = text.replace(/\u0440/g, 'r');
text = text.replace(/\u0441/g, 's');
text = text.replace(/\u0442/g, 't');
text = text.replace(/\u0443/g, 'u');
text = text.replace(/\u0444/g, 'f');
text = text.replace(/\u0445/g, 'h');
text = text.replace(/\u0446/g, 'c');
text = text.replace(/\u0447/g, 'ch');
text = text.replace(/\u0448/g, 'sh');
text = text.replace(/\u0449/g, 'shch');
text = text.replace(/\u044A/g, '');
text = text.replace(/\u044B/g, 'y');
text = text.replace(/\u044C/g, '');
text = text.replace(/\u044D/g, 'e');
text = text.replace(/\u044E/g, 'yu');
text = text.replace(/\u044F/g, 'ya');
// Printing the resulting string
document.querySelector('.example3').textContent = text;
</script>
Links
- Transliterating cyrillic to latin with javascript function: stackoverflow.com/questions/11404047/transliterating-cyrillic-to-latin-with-javascript-function
- Перевод текста в транслит на JavaScript: snipp.ru/jquery/translit-js
- String.prototype.replace(): developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
Browser support
- Windows
- Internet Explorer 4.0+ ("Replacing characters one by one with replace()" only)
- Internet Explorer 8.0+
- Edge 12.0+
- Firefox 1.0+
- Google Chrome
- Opera 6.0+ ("Replacing characters one by one with replace()" only)
- Opera 9.0+
- Safari 3.1+
- SeaMonkey 1.0+
- Mozilla 0.6+
- Netscape 6.0+
- Linux
- Firefox 1.0+
- Google Chrome / Chromium
- Opera 6.0+ ("Replacing characters one by one with replace()" only)
- Opera 9.0+
- SeaMonkey 1.0+
- Mozilla 0.6+
- Netscape 6.0+
- iOS
- Safari 1.0+
- Android
- Samsung Internet 1.0+
- Chrome 18.0+
- Firefox 4.0+
- Opera 10.1+
More
- Adding a class to all sibling elements of a specific type in JavaScript
- Array to string in JavaScript
- Calculate sum of HTML table column in JavaScript
- Detecting a newly generated element and adding a class to it in JavaScript
- Detect if a class has been added (JavaScript)
- Getting the last class name in classList collection
- Removing the last character from a string in Javascript