Module: Translit

Defined in:
lib/translit.rb

Class Method Summary collapse

Class Method Details

.convert(text, enforce_language = nil) ⇒ Object



19
20
21
# File 'lib/translit.rb', line 19

def self.convert(text, enforce_language = nil)
  convert!(text.dup, enforce_language)
end

.convert!(text, enforce_language = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/translit.rb', line 4

def self.convert!(text, enforce_language = nil)
  language = if enforce_language
    enforce_input_language(enforce_language)
  else
    detect_input_language(text.split(/\s+/).first)
  end

  map = self.send(language.to_s).sort_by {|k,v| v.length <=>  k.length}
  map.each do |translit_key, translit_value|
    text.gsub!(translit_key.capitalize, translit_value.first)
    text.gsub!(translit_key, translit_value.last)
  end
  text
end