RuTils::Transliteration::Simple

Реализует простейшую транслитерацию

"вот мы и здесь".translify => "vot my i zdes"
"vot my i zdes".detranslify => "вот мы и здесь"

Public Class Methods

dirify(string) click to toggle source

Транслитерирует строку, делая ее пригодной для применения как имя директории или URL

# File lib/transliteration/simple.rb, line 66
def self.dirify(string)
  st = self.translify(string)
  st.gsub!(/(\s\&\s)|(\s\&amp\;\s)/, ' and ') # convert & to "and"
  st.gsub!(/\W/, ' ')  #replace non-chars
  st.gsub!(/(_)$/, '') #trailing underscores
  st.gsub!(/^(_)/, '') #leading unders
  st.strip.translify.gsub(/(\s)/,'-').downcase.squeeze('-')
end
translify(str) click to toggle source

Заменяет кириллицу в строке на латиницу. Немного специфично потому что поддерживает комби-регистр (Щука -> Shuka)

# File lib/transliteration/simple.rb, line 35
def self.translify(str)
  chars = str.split(//)    
  
  lowers = TABLE_LOWER.map{|e| e[0] }
  uppers = TABLE_UPPER.map{|e| e[0] }
  
  hashtable = {}
  TABLE.each do | item |
    next unless item[0] && item[1]
    hashtable[item[0]] = item[1]
  end
  
  result = ''
  chars.each_with_index do | char, index |
    if uppers.include?(char) && lowers.include?(chars[index+1])
      # Combined case. Here we deal with Latin letters so there is no problem to use
      # Ruby's builtin upcase_downcase
      ch = hashtable[char].downcase.capitalize
      result << ch
    elsif uppers.include?(char)
      result << hashtable[char]        
    elsif lowers.include?(char)
      result << hashtable[char]
    else
      result << char
    end
  end
  return result
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.