Module: GostTranslit

Defined in:
lib/gost_translit.rb

Constant Summary collapse

UPPER_REGEXP =
/[[:upper:]]/
LOWER_REGEXP =
/[[:lower:]]/
RU_MAPPING =
{
  'а': 'a',
  'б': 'b',
  'в': 'v',
  'г': 'g',
  'д': 'd',
  'е': 'e',
  'ё': 'yo',
  'ж': 'zh',
  'з': 'z',
  'и': 'i',
  'й': 'j',
  'к': 'k',
  'л': 'l',
  'м': 'm',
  'н': 'n',
  'о': 'o',
  'п': 'p',
  'р': 'r',
  'с': 's',
  'т': 't',
  'у': 'u',
  'ф': 'f',
  'х': 'x',
  'ц': 'cz',
  'ч': 'ch',
  'ш': 'sh',
  'щ': 'shh',
  'ъ': '``',
  'ы': 'y`',
  'ь': '`',
  'э': 'e`',
  'ю': 'yu',
  'я': 'ya'
}
LATIN_REPLACING_MAPPING =
{
  'shh' => 'щ',
  'sh'  => 'ш',
  'yu'  => 'ю',
  'ya'  => 'я',
  '``'  => 'ъ',
  'y`'  => 'ы',
  'e`'  => 'э',
  'ch'  => 'ч',
  'cz'  => 'ц',
  'zh'  => 'ж',
  'yo'  => 'ё'
}
LATIN_MAPPING =
Hash[
  GostTranslit::RU_MAPPING.invert.collect { |k, v| [ k.to_s, v.to_s ] }
].merge!('c' => 'ц')

Class Method Summary collapse

Class Method Details

.convert(string) ⇒ Object



91
92
93
# File 'lib/gost_translit.rb', line 91

def convert(string)
  language(string) == :rus ? to_latin(string) : to_cyrillic(string)
end

.to_cyrillic(string) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gost_translit.rb', line 76

def to_cyrillic(string)
  words = string.split(' ')

  words.map! do |word|
    translit_word = word.downcase
    LATIN_REPLACING_MAPPING.each { |k,v| translit_word.gsub!(k, v) }

    translit_word = translit_word.split('')
                                 .map! { |l| LATIN_MAPPING[l] || l }
                                 .join

    apply_capitalize_rules(word, translit_word)
  end.join(' ')
end

.to_latin(string) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/gost_translit.rb', line 62

def to_latin(string)
  words = string.split(' ')

  words.map! do |word|
    translit_word = word.downcase
                        .split('')
                        .map { |l| RU_MAPPING[l.to_sym] || l}
                        .join

    translit_word.gsub!(/(cz)(?=[i|e|j|y])/, 'c')
    apply_capitalize_rules(word, translit_word)
  end.join(' ')
end