-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdocx_translate.rb
executable file
·192 lines (140 loc) · 4 KB
/
docx_translate.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env ruby
##########################################################
###
## File: docx_translate.rb
## Desc: Machine translation of *.docx files
## By: Dewayne VanHoozer ([email protected])
#
require 'debug_me' # A tool to print the labeled value of variables.
require 'pathname' # STDLIB
require 'pathname_helpers'
require 'docx' # a ruby library/gem for interacting with .docx files
require 'docx_helpers'
include DocxHelpers
require 'google_translate' # Screen scrapper 'google-translate'
$glate = GoogleTranslate.new
me = Pathname.new(__FILE__).realpath
my_dir = me.parent
my_name = me.basename.to_s
$options = {
verbose: false,
from_language: :en,
to_language: :es,
ignore_pstyle: [],
paths: []
}
def verbose?
$options[:verbose]
end
usage = <<EOS
Test out some of the common translation gems
Usage: #{my_name} options english_file
Where:
options Do This
-h or --help Display this message
-v or --verbose Display progress
-f or --from Source language
-t pr --to Target language
source_file File to translate
EOS
# Check command line for Problems with Parameters
errors = []
if ARGV.empty? ||
ARGV.include?('-h') ||
ARGV.include?('--help')
puts usage
exit
end
%w[ -v --verbose ].each do |param|
if ARGV.include? param
$options[:verbose] = true
ARGV[ ARGV.index(param) ] = nil
end
end
%w[ -f --from ].each do |param|
if ARGV.include? param
param_index = ARGV.index(param)
if param_index+1 >= ARGV.size
# FIXME: errors is not global
$errors << "#{ARGV[param_index]} specified without parameter"
else
$options[:from_language] = ARGV[param_index+1].to_sym
ARGV[param_index+1] = nil
end
ARGV[param_index] = nil
end
end
%w[ -t --to ].each do |param|
if ARGV.include? param
param_index = ARGV.index(param)
if param_index+1 >= ARGV.size
# FIXME: errors is not global
$errors << "#{ARGV[param_index]} specified without parameter"
else
$options[:to_language] = ARGV[param_index+1].to_sym
ARGV[param_index+1] = nil
end
ARGV[param_index] = nil
end
end
ARGV.compact!
$options[:paths] = ARGV.map { |f| Pathname.new(f) } unless ARGV.empty?
$options[:paths].select! { |f| f.exist? && '.docx' == f.extname.downcase } unless ARGV.empty?
if $options[:paths].empty?
errors << "No valid *.docx files were specified."
end
unless errors.empty?
STDERR.puts
STDERR.puts "Correct the following errors and try again:"
STDERR.puts
errors.each do |e|
STDERR.puts "\t#{e}"
end
STDERR.puts
exit(1)
end
######################################################
# Local methods
def translate_paragraph( text,
from_language = $options[:from_language],
to_language = $options[:to_language]
)
result = $glate.translate( from_language, to_language, text )[0]
result_str = result.map { |t| t[0] }.join(' ')
end
######################################################
# Main
at_exit do
puts
puts "Done."
puts
end
$options[:paths].each do | from_filepath |
if verbose?
print "Translating (#{$options[:from_language]}-=>#{$options[:to_language]}) #{from_filepath.basename} ... "
else
print '.'
end
docx = Docx::Document.open( from_filepath )
there_was_a_problem = false
docx.paragraphs.each do |para|
next if $options[:ignore_pstyle].include?( get_paragraph_style_name(para) )
begin
para.text = translate_paragraph( para.text )
rescue Exception => e
there_was_a_problem = true
#STDERR.puts "#{e}"
end
end
new_file = from_filepath.parent + from_filepath.basename.to_s.
gsub( "_#{$options[:from_language]}",
"_#{$options[:to_language]}")
docx.save(new_file)
if verbose?
if there_was_a_problem
puts "** PROBLEM **"
else
puts 'done.'
end
end
end # $options[:paths].each do | en_filepath |