Class: Lhj::Command::Trans
Class Method Summary
collapse
Instance Method Summary
collapse
#auto_spin, #begin_title, #stop
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
.options ⇒ Object
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
43
44
45
46
47
48
49
50
51
|
# File 'lib/lhj/command/trans.rb', line 43
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
|
53
54
55
56
57
58
59
60
|
# File 'lib/lhj/command/trans.rb', line 53
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
|
#handle ⇒ Object
24
25
26
|
# File 'lib/lhj/command/trans.rb', line 24
def handle
handler_files
end
|
#handler_file(file) ⇒ Object
35
36
37
38
39
40
41
|
# File 'lib/lhj/command/trans.rb', line 35
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_files ⇒ Object
28
29
30
31
32
33
|
# File 'lib/lhj/command/trans.rb', line 28
def handler_files
search_type = @file_type.gsub('.', '')
Dir.glob("#{@current_path}/**/*.{#{search_type}}").each do |f|
handler_file f
end
end
|
#run ⇒ Object
16
17
18
|
# File 'lib/lhj/command/template.rb', line 16
def run
p 'hello world'
end
|
#validate! ⇒ Object
12
13
14
|
# File 'lib/lhj/command/template.rb', line 12
def validate!
super
end
|