Last active
December 16, 2015 05:19
-
-
Save umutakturk/5383504 to your computer and use it in GitHub Desktop.
Translate special characters to ASCII characters in Ruby.
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
def slugify(text) | |
accents = { | |
['à','á','â','ã','å','ǻ','ā','ă','ą','ǎ','ª'] => 'a', | |
['ä','æ','ǽ'] => 'ae', | |
['Æ','Ǽ'] => 'AE', | |
['Ä'] => 'Ae', | |
['Ü'] => 'Ue', | |
['Ç','Ć','Ĉ','Ċ','Č'] => 'C', | |
['ç','ć','ĉ','ċ','č'] => 'c', | |
['Ð','Ď','Đ'] => 'D', | |
['ð','ď','đ'] => 'd', | |
['È','É','Ê','Ë','Ē','Ĕ','Ė','Ę','Ě'] => 'E', | |
['è','é','ê','ë','ē','ĕ','ė','ę','ě'] => 'e', | |
['ƒ'] => 'f', | |
['Ĝ','Ğ','Ġ','Ģ'] => 'G', | |
['ĝ','ğ','ġ','ģ'] => 'g', | |
['Ĥ','Ħ'] => 'H', | |
['ĥ','ħ'] => 'h', | |
['Ì','Í','Î','Ï','Ĩ','Ī','Ĭ','Ǐ','Į','İ'] => 'I', | |
['ì','í','î','ï','ĩ','ī','ĭ','ǐ','į','ı'] => 'i', | |
['IJ'] => 'IJ', | |
['ij'] => 'ij', | |
['Ĵ'] => 'J', | |
['ĵ'] => 'j', | |
['Ķ'] => 'K', | |
['ķ'] => 'k', | |
['Ĺ','Ļ','Ľ','Ŀ','Ł'] => 'L', | |
['ĺ','ļ','ľ','ŀ','ł'] => 'l', | |
['Ñ','Ń','Ņ','Ň'] => 'N', | |
['ñ','ń','ņ','ň','ʼn'] => 'n', | |
['Ò','Ó','Ô','Õ','Ō','Ŏ','Ǒ','Ő','Ơ','Ø','Ǿ','Ö'] => 'O', | |
['ò','ó','ô','õ','ō','ŏ','ǒ','ő','ơ','ø','ǿ','º','ö'] => 'o', | |
['Œ'] => 'OE', | |
['œ'] => 'oe', | |
['Ŕ','Ŗ','Ř'] => 'R', | |
['ŕ','ŗ','ř'] => 'r', | |
['Ś','Ŝ','Ş','Š'] => 'S', | |
['ś','ŝ','ş','š','ſ'] => 's', | |
['ß'] => 'ss', | |
['Ţ','Ť','Ŧ'] => 'T', | |
['ţ','ť','ŧ'] => 't', | |
['Ù','Ú','Û','Ũ','Ū','Ŭ','Ů','Ű','Ų','Ư','Ǔ','Ǖ','Ǘ','Ǚ','Ǜ','Ü'] => 'U', | |
['ù','ú','û','ũ','ū','ŭ','ů','ű','ų','ư','ǔ','ǖ','ǘ','ǚ','ǜ','ü'] => 'u', | |
['Ý','Ÿ','Ŷ'] => 'Y', | |
['ý','ÿ','ŷ'] => 'y', | |
['Ŵ'] => 'W', | |
['ŵ'] => 'w', | |
['Ź','Ż','Ž'] => 'Z', | |
['ź','ż','ž'] => 'z' | |
} | |
accents.each do |ac,rep| | |
ac.each do |s| | |
text = text.gsub(s, rep) | |
end | |
end | |
text = text.gsub(/[^a-zA-Z0-9 ]/, "") | |
text = text.gsub(/[ ]+/, " ") | |
text = text.gsub(/ /, "-") | |
text = text.downcase | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment