Class: Han

Inherits:
Object
  • Object
show all
Defined in:
lib/lib/han.rb

Class Method Summary collapse

Class Method Details

.t(text, opt = nil) ⇒ Object



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

def self.t(text, opt = nil)
  latin = []
  chars = text.split("")
  chars.each_with_index do |char, index|
    if char =~ /\p{Han}/
      converted_char = Pinyin.t(char, opt == :ascii ? {} : {tonemarks: true})
      latin << ' '
      latin << converted_char
    else
      latin << char
    end
  end
  latin.join('').gsub('  ', ' ')
end