ref: master
lib/extensions/string.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# encoding: utf-8 class String TRANSLITERATIONS = { [ 'Á', 'À', 'À', 'Â', 'Ã', 'Ä', 'Å' ] => 'A', [ 'á', 'à', 'à', 'â', 'ã', 'ä', 'å' ,'ª' ] => 'a', [ 'É', 'È', 'Ê', 'Ë' ] => 'E', [ 'é', 'è', 'ê', 'ë' ] => 'e', [ 'Í', 'Ì', 'Î', 'Ï' ] => 'I', [ 'í', 'ì', 'î', 'ï' ] => 'i', [ 'Ó', 'Ò', 'Ô', 'Ö', 'Õ', 'º' ] => 'O', [ 'ó', 'ò', 'ô', 'ö', 'õ', 'º' ] => 'o', [ 'Ú', 'Ù', 'Û', 'Ü' ] => 'U', [ 'ú', 'ù', 'û', 'ü' ] => 'u', [ 'ß' ] => 'ss', [ 'Ç' ] => 'C', [ 'ç' ] => 'c', [ 'Ñ' ] => 'N', [ 'ñ' ] => 'n', [ 'Ÿ' ] => 'Y', [ 'ÿ' ] => 'y', # Cyrillic alphabet transliteration [ 'а', 'А' ] => 'a', [ 'б', 'Б' ] => 'b', [ 'в', 'В' ] => 'v', [ 'г', 'Г' ] => 'g', [ 'д', 'Д' ] => 'd', [ 'е', 'Е' ] => 'e', [ 'ё', 'Ё' ] => 'yo', [ 'ж', 'Ж' ] => 'zh', [ 'з', 'З' ] => 'z', [ 'и', 'И' ] => 'i', [ 'й', 'Й' ] => 'y', [ 'к', 'К' ] => 'k', [ 'л', 'Л' ] => 'l', [ 'м', 'М' ] => 'm', [ 'н', 'Н' ] => 'n', [ 'о', 'О' ] => 'o', [ 'п', 'П' ] => 'p', [ 'р', 'Р' ] => 'r', [ 'с', 'С' ] => 's', [ 'т', 'Т' ] => 't', [ 'у', 'У' ] => 'u', [ 'ф', 'Ф' ] => 'f', [ 'х', 'Х' ] => 'h', [ 'ц', 'Ц' ] => 'ts', [ 'ч', 'Ч' ] => 'ch', [ 'ш', 'Ш' ] => 'sh', [ 'щ', 'Щ' ] => 'sch', [ 'э', 'Э' ] => 'e', [ 'ю', 'Ю' ] => 'yu', [ 'я', 'Я' ] => 'ya', [ 'ы', 'Ы' ] => 'i', [ 'ь', 'Ь' ] => '', [ 'ъ', 'Ъ' ] => '', # Ukrainian lovely letters [ 'і', 'І' ] => 'i', [ 'ї', 'Ї' ] => 'yi', [ 'є', 'Є' ] => 'ye', [ 'ґ', 'Ґ' ] => 'g', } # transliterate a string (assumed to contain UTF-8 data) # into ASCII by replacing non-ascii characters to their # ASCII. # # The transliteration is, of course, lossy, and its performance is poor. # Don't abuse this method. def transliterate new = self.dup TRANSLITERATIONS.each { |from,to| from.each { |seq| new.gsub!(seq, to) } } new end def to_slug transliterate.downcase.gsub(/[^[[:word:]]~\s:;+=_."'`-]/, '').gsub(/[\s:;+=_"'`-]+/, '-').gsub(/-$/, '').gsub(/^-/, '').to_s end def to_css_class underscore.dasherize.gsub('/','_') end def fix_i18n self.sub('{fn} ', '') end end |