Created
February 12, 2014 12:17
-
-
Save cdinu/8954532 to your computer and use it in GitHub Desktop.
Transliterate Romanian Alphabet to Cyrilic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
// the minimum version of jQuery we want | |
var v = "1.3.2"; | |
// check prior inclusion and version | |
if (window.jQuery === undefined || window.jQuery.fn.jquery < v) { | |
var done = false; | |
var script = document.createElement("script"); | |
script.src = "http://ajax.googleapis.com/ajax/libs/jquery/" + v + "/jquery.min.js"; | |
script.onload = script.onreadystatechange = function() { | |
if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) { | |
done = true; | |
initMyBookmarklet(); | |
} | |
}; | |
document.getElementsByTagName("head")[0].appendChild(script); | |
} else { | |
initMyBookmarklet(); | |
} | |
function initMyBookmarklet() { | |
String.prototype.replaceArray = function(find, replace) { | |
var replaceString = this; | |
var regex; | |
for (var i = 0; i < find.length; i++) { | |
regex = new RegExp(find[i], "g"); | |
replaceString = replaceString.replace(regex, replace[i]); | |
} | |
return replaceString; | |
}; | |
String.prototype.transliterate = function() { | |
var text = this; | |
var latin_comp = ['iii', 'ii', 'iu', 'ia', 'șt', 'che', 'chi', 'ch', 'ce', 'ci ', 'cia', 'cio', 'ciu', 'ge', 'gi', 'eea', 'eie', 'x', 'y']; | |
var cyrilic_comp = ['ии', 'ий', 'ю', 'ия', 'щ', 'ке', 'ки', 'ч', 'че', 'чи ', 'чиа', 'чо', 'чу', 'ӂе', 'ӂи', 'ея', 'ее', 'кс', 'ий']; | |
latin_comp.push( 'II', 'III', 'IV', 'VI', 'VII', 'VIII', 'Ii', 'Iu', 'Ia', 'Șt', 'Che', 'Chi', 'Ch', 'Ce', 'Ci ', 'Cia', 'Cio', 'Ciu', 'Ge', 'i', 'X', 'Y'); | |
cyrilic_comp.push('II', 'III', 'IV', 'VI', 'VII', 'VIII', 'Ий', 'Ю', 'Ия', 'Щ', 'Ке', 'Ки', 'Ч', 'Че', 'Чи ', 'Чиа', 'Чо', 'Чу', 'Ӂе', 'и', 'Кс', 'Ий'); | |
var latin_single = 'aăâbcdefghiîjklmnoprsșştţțuvwzAĂÂBCDEFGHIÎJKLMNOPRSȘŞTŢȚUVWZ'.split('');; | |
var cyrilic_single = 'аэыбкдефгхиыжклмнопрсшштццуввзАЫЭБКДЕФГХИЫЖКЛМНОПРСШШТЦЦУВВЗ'.split('');; | |
text = text.replaceArray(latin_comp, cyrilic_comp); | |
text = text.replaceArray(latin_single, cyrilic_single); | |
return text; | |
}; | |
(window.transliterate = function(root) { | |
root = root || $('body'); | |
var excludes = ['html','head','style','title','link','meta','script','object','iframe','canvas']; | |
var tag = root.attr && root.attr('tagName') && root.attr('tagName').toLowerCase(); | |
tag = tag || root.tagName || ''; | |
for (var i=0; i<excludes.length; i++){ | |
if (excludes[i] === tag.toLowerCase){ | |
return ; | |
} | |
} | |
var children = root.childNodes; | |
if (children) { | |
for (var i = 0, chLen = children.length; i < chLen; i++) { | |
var child = children[i]; | |
if (child.data && typeof(child.data) === 'string') { | |
child.data = child.data.transliterate(); | |
} | |
window.transliterate(children[i]); | |
} | |
} | |
children = root.children && typeof(root.children) === 'function' && root.children(); | |
if (children) { | |
for (var i = 0, chLen = children.length; i < chLen; i++) { | |
console.log('ch', i, children[i]); | |
window.transliterate(children[i]); | |
} | |
} | |
} | |
)(); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment