Class: Zh

Inherits:
Object
  • Object
show all
Defined in:
lib/zh.rb,
lib/zh/version.rb

Defined Under Namespace

Modules: VERSION Classes: Error

Constant Summary collapse

RB_LT_25 =
Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.5.0')
TRADITIONAL_LOCALE =
i[tw hk].freeze
SIMPLIFIED_LOCALE =
i[cn sg].freeze
LOCALE =
TRADITIONAL_LOCALE + SIMPLIFIED_LOCALE

Class Method Summary collapse

Class Method Details

.converter(method, message) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/zh.rb', line 23

def converter(method, message)
  default_locale_data = locale_data(method)
  default_locale_data.each_key { |k| message.gsub!(k, default_locale_data[k]) }
  hanx_data = locale_data(TRADITIONAL_LOCALE.include?(method) ? 'hant' : 'hans')
  hanx_data.each_key { |k| message.gsub!(k, hanx_data[k]) }
  message
end

.update_dataObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/zh.rb', line 31

def update_data
  i[cn sg tw hk hans hant].each do |locale|
    data = locale_data(locale)
    url = "https://zh.wikipedia.org/w/index.php?title=MediaWiki:Conversiontable/zh-#{locale}&action=raw&templates=expand"
    table = RB_LT_25 ? open(url).read : URI.open(url).read
    table.lines do |line|
      matches = line.match(%r{^[*"]([^"=]+)"?\s*=>\s*"?([^\s/"]+)\s?.*"?[;,]$})
      data[matches[1].strip] = matches[2].strip if matches
    end
    write_to_json(locale, data)
  end
  true
end

.versionObject



2
3
4
# File 'lib/zh/version.rb', line 2

def self.version
  Gem::Version.new VERSION::STRING
end