module RailsI18n::Transliteration::Ukrainian

Public Class Methods

rule() click to toggle source
# File lib/rails_i18n/transliteration.rb, line 7
def rule
  lambda do |string|
    next '' unless string

    string.gsub(/./) do |char|
      # Regexp.last_match is local to the thread and method scope
      # of the method that did the pattern match.
      @pre_match, @post_match = $`, $'

      case char
      when 'Ж'
        lookahead_upcase 'ZH'
      when 'Х'
        lookahead_upcase 'KH'
      when 'Ц'
        lookahead_upcase 'TS'
      when 'Ч'
        lookahead_upcase 'CH'
      when 'Ш'
        lookahead_upcase 'SH'
      when 'Щ'
        lookahead_upcase 'SHCH'
      when 'г'
        behind =~ /[зЗ]/ ? 'gh' : 'h'
      when 'Г'
        behind =~ /[зЗ]/ ? lookahead_upcase('GH') : 'H'
      when 'є'
        letter?(behind) ? 'ie' : 'ye'
      when 'Є'
        letter?(behind) ? lookahead_upcase('IE') : lookahead_upcase('YE')
      when 'ї'
        letter?(behind) ? 'i' : 'yi'
      when 'Ї'
        letter?(behind) ? 'I' : lookahead_upcase('YI')
      when 'й'
        letter?(behind) ? 'i' : 'y'
      when 'Й'
        letter?(behind) ? 'I' : 'Y'
      when 'ю'
        letter?(behind) ? 'iu' : 'yu'
      when 'Ю'
        letter?(behind) ? lookahead_upcase('IU') : lookahead_upcase('YU')
      when 'я'
        letter?(behind) ? 'ia' : 'ya'
      when 'Я'
        letter?(behind) ? lookahead_upcase('IA') : lookahead_upcase('YA')
      when "'"
        # remove apostrophe inside a word
        letter?(behind) && letter?(ahead) ? '' : "'"
      else
        straight_lookup[char] || char
      end
    end
  end
end

Private Class Methods

ahead() click to toggle source
# File lib/rails_i18n/transliteration.rb, line 69
def ahead
  @post_match && @post_match[0]
end
behind() click to toggle source
# File lib/rails_i18n/transliteration.rb, line 65
def behind
  @pre_match && @pre_match[-1]
end
downcased?(symbol) click to toggle source
# File lib/rails_i18n/transliteration.rb, line 73
def downcased?(symbol)
  symbol =~ downcased_regexp
end
downcased_regexp() click to toggle source
# File lib/rails_i18n/transliteration.rb, line 77
def downcased_regexp
  @downcased_regexp ||= /[а-яґєії]/
end
letter?(symbol) click to toggle source

apostrophe can be inside a word

# File lib/rails_i18n/transliteration.rb, line 82
def letter?(symbol)
  symbol =~ letter_regexp
end
letter_regexp() click to toggle source
# File lib/rails_i18n/transliteration.rb, line 86
def letter_regexp
  @letter_regexp ||= /[а-яґєіїА-ЯҐЄІЇ'’]/
end
lookahead_upcase(word) click to toggle source
# File lib/rails_i18n/transliteration.rb, line 90
def lookahead_upcase(word)
  downcased?(ahead) ? word.capitalize : word.upcase
end
straight_lookup() click to toggle source
# File lib/rails_i18n/transliteration.rb, line 94
def straight_lookup
  @straight_lookup ||= {
    'а'=>'a','б'=>'b','в'=>'v','ґ'=>'g','д'=>'d','е'=>'e','ж'=>'zh',
    'з'=>'z','и'=>'y','і'=>'i','к'=>'k','л'=>'l','м'=>'m','н'=>'n','о'=>'o',
    'п'=>'p','р'=>'r','с'=>'s','т'=>'t','у'=>'u','ф'=>'f','х'=>'kh','ц'=>'ts',
    'ч'=>'ch','ш'=>'sh','щ'=>'shch','ь'=>'','’'=>'',
    'А'=>'A','Б'=>'B','В'=>'V','Ґ'=>'G','Д'=>'D','Е'=>'E',
    'З'=>'Z','И'=>'Y','І'=>'I','К'=>'K','Л'=>'L','М'=>'M','Н'=>'N','О'=>'O',
    'П'=>'P','Р'=>'R','С'=>'S','Т'=>'T','У'=>'U','Ф'=>'F','Ь'=>''
  }
end