lib/lhj/command/trans.rb in lhj-tools-0.1.1 vs lib/lhj/command/trans.rb in lhj-tools-0.1.2
- old
+ new
@@ -1,21 +1,62 @@
+# frozen_string_literal: true
+require 'lhj/helper/trans_helper'
module Lhj
class Command
class Trans < Command
- self.summary = '用于中英转换'
- self.description = '中英转换'
+ self.summary = '源码中的简繁体转换'
- def initialize(argv)
- super
+ def self.options
+ [
+ %w[--file-type 文件扩展名,默认为m,h,pch,xib],
+ %w[--zh-cn 转成简体中文,默认转成繁体]
+ ]
end
- def validate!
+ def initialize(argv)
+ @current_path = argv.shift_argument || Dir.pwd
+ @file_type = argv.option('file-type', 'm,h,pch,xib')
+ @zh_cn = argv.flag?('zh-cn', false)
super
end
def run
- p 'hello world'
+ handler_files
end
+
+ def handler_files
+ Dir.glob("#{@current_path}/**/*.{#{@file_type}}").each do |f|
+ handler_file f
+ end
+ end
+
+ def handler_file(file)
+ File.chmod(0o644, file) unless File.writable?(file)
+ str = format_file_string(file)
+ File.open(file, 'w+') do |f|
+ f.write(str)
+ end
+ end
+
+ def format_file_string(file)
+ str = ''
+ File.open(file, 'r+') do |f|
+ f.each_line do |line|
+ str += format_line_string(line)
+ end
+ end
+ str
+ end
+
+ def format_line_string(line)
+ result = line
+ if line =~ /[\u4e00-\u9fa5]/
+ result = Lhj::Trans::Helper.instance.trans_zh_cn_str(line) if @zh_cn
+ result = Lhj::Trans::Helper.instance.trans_zh_hk_str(line) unless @zh_cn
+ end
+ result
+ end
+
end
end
end