Class: Lhj::Command::Trans

Inherits:
Lhj::Command show all
Defined in:
lib/lhj/command/trans.rb,
lib/lhj/command/template.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Trans

Returns a new instance of Trans.



17
18
19
20
21
22
# File 'lib/lhj/command/trans.rb', line 17

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

Class Method Details

.optionsObject



10
11
12
13
14
15
# File 'lib/lhj/command/trans.rb', line 10

def self.options
  [
    %w[--file-type 文件扩展名,默认为m,h,pch,xib],
    %w[--zh-cn 转成简体中文,默认转成繁体]
  ]
end

Instance Method Details

#format_file_string(file) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/lhj/command/trans.rb', line 42

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

#format_line_string(line) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/lhj/command/trans.rb', line 52

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

#handler_file(file) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/lhj/command/trans.rb', line 34

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

#handler_filesObject



28
29
30
31
32
# File 'lib/lhj/command/trans.rb', line 28

def handler_files
  Dir.glob("#{@current_path}/**/*.{#{@file_type}}").each do |f|
    handler_file f
  end
end

#runObject



24
25
26
# File 'lib/lhj/command/trans.rb', line 24

def run
  handler_files
end

#validate!Object



12
13
14
# File 'lib/lhj/command/template.rb', line 12

def validate!
  super
end