Skip to content

Instantly share code, notes, and snippets.

@Rachind
Forked from croisillon/javascript.translit.js
Created June 4, 2019 08:39
Show Gist options
  • Save Rachind/551a6387268d5dad98923b5e8d5c1671 to your computer and use it in GitHub Desktop.
Save Rachind/551a6387268d5dad98923b5e8d5c1671 to your computer and use it in GitHub Desktop.
JavaScript translater russian to translit
function rus_to_latin ( str ) {
var ru = {
'а': 'a', 'б': 'b', 'в': 'v', 'г': 'g', 'д': 'd',
'е': 'e', 'ё': 'e', 'ж': 'j', 'з': 'z', 'и': 'i',
'к': 'k', 'л': 'l', 'м': 'm', 'н': 'n', 'о': 'o',
'п': 'p', 'р': 'r', 'с': 's', 'т': 't', 'у': 'u',
'ф': 'f', 'х': 'h', 'ц': 'c', 'ч': 'ch', 'ш': 'sh',
'щ': 'shch', 'ы': 'y', 'э': 'e', 'ю': 'u', 'я': 'ya'
}, n_str = [];
str = str.replace(/[ъь]+/g, '').replace(/й/g, 'i');
for ( var i = 0; i < str.length; ++i ) {
n_str.push(
ru[ str[i] ]
|| ru[ str[i].toLowerCase() ] == undefined && str[i]
|| ru[ str[i].toLowerCase() ].replace(/^(.)/, function ( match ) { return match.toUpperCase() })
);
}
return n_str.join('');
}
/*
rus_to_latin( 'Иван Алексеев, известный под сценической кличкой Noize MC записал альбом с показательным названием «Неразбериха». Получилась питательная и где-то даже успокоительная смесь из хип-хопа, гранжа, регги и брейк-бита' );
> Ivan Alekseev, izvestnyi pod scenicheskoi klichkoi Noize MC zapisal albom s pokazatelnym nazvaniem «Nerazberiha». Poluchilas pitatelnaya i gde-to daje uspokoitelnaya smes iz hip-hopa, granja, reggi i breik-bita
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment