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
|
# File 'lib/llm_translate/cli.rb', line 22
def translate
config_path = options[:config]
unless File.exist?(config_path)
say "Configuration file not found: #{config_path}", :red
say 'Please create a configuration file or specify a valid path with --config', :yellow
exit 1
end
begin
config = Config.load(config_path, options)
logger = Logger.new(config)
logger.info 'Starting translation process...'
if config.single_file_mode?
logger.info "Input file: #{config.input_file}"
logger.info "Output file: #{config.output_file}"
else
logger.info "Input directory: #{config.input_directory}"
logger.info "Output directory: #{config.output_directory}"
end
logger.info 'DRY RUN MODE - No files will be translated' if options[:dry_run]
ai_client = AiClient.new(config, logger)
translator_engine = TranslatorEngine.new(config, logger, ai_client)
if config.single_file_mode?
unless File.exist?(config.input_file)
logger.error "Input file not found: #{config.input_file}"
return
end
files = [config.input_file]
logger.info "Single file mode: translating #{config.input_file}"
else
file_finder = FileFinder.new(config, logger)
files = file_finder.find_markdown_files
if files.empty?
logger.warn "No markdown files found in #{config.input_directory}"
return
end
logger.info "Found #{files.length} markdown files to translate"
end
if options[:dry_run]
logger.info "DRY RUN: Would translate #{files.length} files with #{config.concurrent_files} concurrent threads"
success_count = files.length
error_count = 0
else
logger.info "Starting translation with #{config.concurrent_files} concurrent files"
results = translator_engine.translate_files_concurrently(files)
success_count = results[:success].length
error_count = results[:error].length
logger.error "Stopping due to too many errors (#{error_count})" if config.should_stop_on_error?(error_count)
end
logger.info 'Translation completed!'
logger.info "Success: #{success_count}, Errors: #{error_count}"
generate_report(config, success_count, error_count, files) if config.generate_report?
rescue ConfigurationError => e
say "Configuration error: #{e.message}", :red
exit 1
rescue StandardError => e
say "Unexpected error: #{e.message}", :red
say e.backtrace.join("\n") if options[:verbose]
exit 1
end
end
|