class Zh

Constants

LOCALE
RB_LT_25
SIMPLIFIED_LOCALE
TRADITIONAL_LOCALE

Public Class Methods

converter(method, message) click to toggle source
# 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_data() click to toggle source
# 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
version() click to toggle source
# File lib/zh/version.rb, line 2
def self.version
  Gem::Version.new VERSION::STRING
end

Private Class Methods

locale_data(locale) click to toggle source
# File lib/zh.rb, line 47
def locale_data(locale)
  json = File.new(__dir__ + "/data/zh-#{locale}.json", 'r')
  parser = Yajl::Parser.new
  parser.parse(json)
end
write_to_json(locale, data) click to toggle source
# File lib/zh.rb, line 53
def write_to_json(locale, data)
  file = File.open(__dir__ + "/data/zh-#{locale}.json", 'w')
  file.write(data.to_json)
rescue IOError => e
  puts "An error occurred: #{$ERROR_INFO}"
ensure
  file&.close
end