lib/zh.rb in zh-0.1.0 vs lib/zh.rb in zh-1.0.0

- old
+ new

@@ -1,72 +1,62 @@ -# encoding: utf-8 -require "zh/version" +# frozen_string_literal: true -path = File.expand_path(File.dirname(__FILE__)) -$:.unshift(path) unless $:.include?(path) - +require 'zh/version' require 'open-uri' -require 'json' +require 'yajl/json_gem' class Zh - class << self - LOCALE = [:cn, :sg, :tw, :hk] + class Error < StandardError; end - # sg, cn 是簡體,tw, hk 是繁體 + class << self + 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 + + # sg, cn are Simplified Chinese, tw, hk are Traditional Chinese LOCALE.each do |method| define_method "to_#{method}" do |message| converter(method, message) end end def converter(method, message) - if method == :tw || method == :hk - localize_json = file_parser(method) - localize_json.each_key { |k| message.gsub!(k, localize_json[k]) } - traditionalize_json = file_parser("hant") - traditionalize_json.each_key { |k| message.gsub!(k, traditionalize_json[k]) } - elsif method == :cn || method == :sg - localize_json = file_parser(method) - localize_json.each_key { |k| message.gsub!(k, localize_json[k]) } - simplify_json = file_parser("hans") - simplify_json.each_key { |k| message.gsub!(k, simplify_json[k]) } - end + 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 def update_data - variants = [:cn, :sg, :tw, :hk, :hans, :hant] - variants.each do |l| - json = file_parser(l) - url = "http://zh.wikipedia.org/w/index.php?title=MediaWiki:Conversiontable/zh-#{l}&action=raw&templates=expand" - table = open(url).read + %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(/^[\*\"]([^\"=]+)\"?\s*=>\s*\"?([^\s\/\"]+)\s?.*\"?[;,]$/) - if matches - json[matches[1].strip] = matches[2].strip - end + matches = line.match(%r{^[*"]([^"=]+)"?\s*=>\s*"?([^\s/"]+)\s?.*"?[;,]$}) + data[matches[1].strip] = matches[2].strip if matches end - write_to_json(l, json) + write_to_json(locale, data) end true end private - def file_parser(locale) - file = File.read(File.expand_path(File.dirname(__FILE__)) + "/../data/zh-#{locale}.json") - json = JSON.parse(file) + def locale_data(locale) + json = File.new(__dir__ + "/data/zh-#{locale}.json", 'r') + parser = Yajl::Parser.new + parser.parse(json) end - def write_to_json(l, j) - json = j - begin - file = File.open(File.expand_path(File.dirname(__FILE__)) + "/../data/zh-#{l}.json", "w") - file.write(json.to_json) - rescue IOError => e - puts "An error occurred: #{$!}" - ensure - file.close unless file == nil - end + 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 end end