Class: BetterTranslate::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/better_translate/cli.rb

Overview

Command-line interface for BetterTranslate

Provides standalone CLI commands for translation without Rails.

Examples:

Run translation from config file

cli = CLI.new(["translate", "--config", "config.yml"])
cli.run

Generate config file

cli = CLI.new(["generate", "config.yml"])
cli.run

Direct translation

cli = CLI.new(["direct", "Hello", "--to", "it", "--provider", "chatgpt"])
cli.run

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Initialize CLI

Examples:

cli = CLI.new(ARGV)

Parameters:

  • args (Array<String>)

    Command-line arguments



34
35
36
# File 'lib/better_translate/cli.rb', line 34

def initialize(args)
  @args = args
end

Instance Attribute Details

#argsArray<String> (readonly)

Returns Command-line arguments.

Returns:

  • (Array<String>)

    Command-line arguments



25
26
27
# File 'lib/better_translate/cli.rb', line 25

def args
  @args
end

Instance Method Details

#runvoid

This method returns an undefined value.

Run CLI command

Examples:

cli = CLI.new(ARGV)
cli.run


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/better_translate/cli.rb', line 46

def run
  command = args.first

  case command
  when "translate"
    run_translate
  when "generate"
    run_generate
  when "direct"
    run_direct
  when "analyze"
    run_analyze
  when "--version", "-v"
    puts "BetterTranslate version #{VERSION}"
  when "--help", "-h", nil
    show_help
  else
    puts "Unknown command: #{command}"
    puts
    show_help
  end
end

#run_analyzevoid (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Run analyze command



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/better_translate/cli.rb', line 313

def run_analyze
  # @type var options: Hash[Symbol, String]
  options = {}
  OptionParser.new do |opts|
    opts.on("--source FILE", "Source YAML file path") { |v| options[:source] = v }
    opts.on("--scan-path PATH", "Path to scan for code files") { |v| options[:scan_path] = v }
    opts.on("--format FORMAT", "Output format (text, json, csv)") { |v| options[:format] = v }
    opts.on("--output FILE", "Output file path") { |v| options[:output] = v }
  end.parse!(args[1..])

  # Validate required options
  unless options[:source]
    puts "Error: --source is required"
    return
  end

  unless options[:scan_path]
    puts "Error: --scan-path is required"
    return
  end

  # Validate paths exist
  unless File.exist?(options[:source])
    puts "Error: Source file not found: #{options[:source]}"
    return
  end

  unless File.exist?(options[:scan_path])
    puts "Error: Scan path not found: #{options[:scan_path]}"
    return
  end

  # Default format
  format = (options[:format] || "text").to_sym

  # Scan keys from YAML
  key_scanner = Analyzer::KeyScanner.new(options[:source])
  all_keys = key_scanner.scan

  # Scan code for used keys
  code_scanner = Analyzer::CodeScanner.new(options[:scan_path])
  used_keys = code_scanner.scan

  # Detect orphans
  detector = Analyzer::OrphanDetector.new(all_keys, used_keys)
  orphans = detector.detect

  # Generate report
  reporter = Analyzer::Reporter.new(
    orphans: orphans,
    orphan_details: detector.orphan_details,
    total_keys: all_keys.size,
    used_keys: used_keys.size,
    usage_percentage: detector.usage_percentage,
    format: format
  )

  report = reporter.generate

  # Output or save
  if options[:output]
    reporter.save_to_file(options[:output])
    puts "Report saved to #{options[:output]}"
  else
    puts report
  end
rescue StandardError => e
  puts "Error: #{e.message}"
end

#run_directvoid (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Run direct translation command



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/better_translate/cli.rb', line 248

def run_direct
  text = args[1]
  # @type var options: Hash[Symbol, String]
  options = {}

  remaining_args = args[2..] || []
  OptionParser.new do |opts|
    opts.on("--to LANG", "Target language code") { |v| options[:to] = v }
    opts.on("--language-name NAME", "Target language name") { |v| options[:language_name] = v }
    opts.on("--provider PROVIDER", "Provider (chatgpt, gemini, anthropic)") { |v| options[:provider] = v }
    opts.on("--api-key KEY", "API key") { |v| options[:api_key] = v }
  end.parse!(remaining_args)

  unless text
    puts "Error: Text is required"
    puts "Usage: better_translate direct TEXT --to LANG --provider PROVIDER --api-key KEY"
    return
  end

  unless options[:to]
    puts "Error: --to is required"
    return
  end

  unless options[:provider]
    puts "Error: --provider is required"
    return
  end

  # Default language name
  options[:language_name] ||= options[:to].upcase

  # Configure
  BetterTranslate.configure do |config|
    config.provider = options[:provider].to_sym
    config.source_language = "en"

    case options[:provider].to_sym
    when :chatgpt
      config.openai_key = options[:api_key] || ENV["OPENAI_API_KEY"]
    when :gemini
      config.gemini_key = options[:api_key] || ENV["GEMINI_API_KEY"]
    when :anthropic
      config.anthropic_key = options[:api_key] || ENV["ANTHROPIC_API_KEY"]
    end
  end

  # Translate
  translator = DirectTranslator.new(BetterTranslate.configuration)
  result = translator.translate(
    text,
    to: options[:to],
    language_name: options[:language_name]
  )

  puts result
rescue StandardError => e
  puts "Error: #{e.message}"
end

#run_generatevoid (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Run generate command



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/better_translate/cli.rb', line 195

def run_generate
  output_file = args[1]
  force = args.include?("--force")

  unless output_file
    puts "Error: Output file path is required"
    puts "Usage: better_translate generate OUTPUT_FILE [--force]"
    return
  end

  if File.exist?(output_file) && !force
    puts "Config file already exists at #{output_file}"
    puts "Use --force to overwrite"
    return
  end

  sample_config = {
    "provider" => "chatgpt",
    "openai_key" => "YOUR_OPENAI_API_KEY",
    "gemini_key" => "YOUR_GEMINI_API_KEY",
    "anthropic_key" => "YOUR_ANTHROPIC_API_KEY",
    "source_language" => "en",
    "target_languages" => [
      { "short_name" => "it", "name" => "Italian" },
      { "short_name" => "es", "name" => "Spanish" },
      { "short_name" => "fr", "name" => "French" }
    ],
    "input_file" => "locales/en.yml",
    "output_folder" => "locales",
    "verbose" => true,
    "dry_run" => false,
    "translation_mode" => "override",
    "preserve_variables" => true,
    "global_exclusions" => [], # : Array[String]
    "exclusions_per_language" => {}, # : Hash[String, Array[String]]
    "model" => nil,
    "temperature" => 0.3,
    "max_tokens" => 2000,
    "timeout" => 30,
    "max_retries" => 3,
    "rate_limit" => 10
  }

  File.write(output_file, sample_config.to_yaml)
  puts "Generated configuration file at #{output_file}"
  puts "Please edit it with your API keys and preferences."
end

#run_translatevoid (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Run translate command



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
# File 'lib/better_translate/cli.rb', line 103

def run_translate
  # @type var options: Hash[Symbol, String]
  options = {}
  OptionParser.new do |opts|
    opts.on("--config FILE", "Config file path") { |v| options[:config] = v }
  end.parse!(args[1..])

  unless options[:config]
    puts "Error: --config is required"
    return
  end

  unless File.exist?(options[:config])
    puts "Error: Config file not found: #{options[:config]}"
    return
  end

  # Load configuration from YAML
  yaml_config = YAML.load_file(options[:config])

  # Configure BetterTranslate
  BetterTranslate.configure do |config|
    config.provider = yaml_config["provider"]&.to_sym
    config.openai_key = yaml_config["openai_key"] || ENV["OPENAI_API_KEY"]
    config.gemini_key = yaml_config["gemini_key"] || ENV["GEMINI_API_KEY"]
    config.anthropic_key = yaml_config["anthropic_key"] || ENV["ANTHROPIC_API_KEY"]

    config.source_language = yaml_config["source_language"]
    config.target_languages = yaml_config["target_languages"]&.map do |lang|
      if lang.is_a?(Hash)
        { short_name: lang["short_name"], name: lang["name"] }
      else
        lang
      end
    end

    config.input_file = yaml_config["input_file"]
    config.output_folder = yaml_config["output_folder"]
    config.verbose = yaml_config.fetch("verbose", true)
    config.dry_run = yaml_config.fetch("dry_run", false)

    # Map "full" to :override for backward compatibility
    translation_mode = yaml_config.fetch("translation_mode", "override")
    translation_mode = "override" if translation_mode == "full"
    config.translation_mode = translation_mode.to_sym

    config.preserve_variables = yaml_config.fetch("preserve_variables", true)

    # Exclusions
    config.global_exclusions = yaml_config["global_exclusions"] || []
    config.exclusions_per_language = yaml_config["exclusions_per_language"] || {}

    # Provider options
    config.model = yaml_config["model"] if yaml_config["model"]
    config.temperature = yaml_config["temperature"] if yaml_config["temperature"]
    config.max_tokens = yaml_config["max_tokens"] if yaml_config["max_tokens"]
    config.timeout = yaml_config["timeout"] if yaml_config["timeout"]
    config.max_retries = yaml_config["max_retries"] if yaml_config["max_retries"]
    config.rate_limit = yaml_config["rate_limit"] if yaml_config["rate_limit"]
  end

  # Perform translation
  puts "Starting translation..."
  puts "Provider: #{BetterTranslate.configuration.provider}"
  puts "Source: #{BetterTranslate.configuration.source_language}"
  puts "Targets: #{BetterTranslate.configuration.target_languages.map { |l| l[:name] }.join(", ")}"
  puts

  results = BetterTranslate.translate_files

  # Report results
  puts
  puts "=" * 60
  puts "Translation Complete"
  puts "=" * 60
  puts "Success: #{results[:success_count]}"
  puts "Failure: #{results[:failure_count]}"

  return unless results[:errors].any?

  puts
  puts "Errors:"
  results[:errors].each do |error|
    puts "  - #{error[:language]}: #{error[:error]}"
  end
end

#show_helpvoid (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Show help message



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/better_translate/cli.rb', line 76

def show_help
  puts "    Usage: better_translate COMMAND [OPTIONS]\n\n    Commands:\n      translate              Translate YAML files using config file\n      generate OUTPUT_FILE   Generate sample config file\n      direct TEXT            Translate text directly\n      analyze                Analyze YAML files for orphan keys\n\n    Options:\n      --help, -h             Show this help message\n      --version, -v          Show version\n\n    Examples:\n      better_translate translate --config config.yml\n      better_translate generate config.yml\n      better_translate direct \"Hello\" --to it --provider chatgpt --api-key KEY\n      better_translate analyze --source config/locales/en.yml --scan-path app/\n  HELP\nend\n"