Class: Traductor::CLI

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

Instance Method Summary collapse

Instance Method Details

#diff ⇒ Object



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

def diff
  load_config(options[:config])

  source_path = options[:source] || Traductor.configuration.source_paths&.first
  unless source_path && File.exist?(source_path)
    say "Source file not found: #{source_path}", :red
    return
  end

  source_file = LocaleFile.new(source_path)
  source_flat = KeyFlattener.flatten(source_file.content)

  target_path = resolve_target_path(options[:target], source_path)
  unless target_path && File.exist?(target_path)
    say "Target file not found: #{target_path}", :red
    return
  end

  target_file = LocaleFile.new(target_path)
  target_flat = KeyFlattener.flatten(target_file.content)

  diff = Diff.new(source_flat: source_flat, target_flat: target_flat)

  say "Diff: #{source_path} -> #{target_path}", :blue
  say ""

  if diff.added.any?
    say "Added (#{diff.added.size} keys need translation):", :green
    diff.added.each { |key, value| say "  + #{key}: #{value}" }
    say ""
  end

  if diff.removed.any?
    say "Removed (#{diff.removed.size} keys in target but not in source):", :red
    diff.removed.each { |key, _| say "  - #{key}" }
    say ""
  end

  say "Unchanged: #{diff.unchanged.size} keys", :cyan
  say diff.summary.inspect
end

#init ⇒ Object



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

def init
  if File.exist?(".traductor.yml")
    say ".traductor.yml already exists", :yellow
    return
  end

  content = <<~YAML
    # Traductor configuration
    source_locale: #{options[:source]}
    target_locales:
      - es
      - fr

    # Source file paths (glob patterns supported)
    source_paths:
      - config/locales/#{options[:source]}.yml

    # Output directory (defaults to same directory as source file)
    # output_dir: config/locales

    # LLM settings (any model supported by RubyLLM)
    # model: gpt-4.1-mini
    # temperature: 0.3

    # Translation settings
    # batch_size: 30
    # glossary_path: .traductor-glossary.yml
  YAML

  File.write(".traductor.yml", content)
  say "Created .traductor.yml", :green
  say "Edit the file to configure your locales and source paths."
end

#translate ⇒ Object



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

def translate
  load_config(options[:config])
  apply_overrides

  source_paths = resolve_source_paths
  target_locales = resolve_target_locales

  if source_paths.empty?
    say "No source files found. Run 'traductor init' or specify --source.", :red
    return
  end

  if target_locales.empty?
    say "No target locales specified. Edit .traductor.yml or use --targets.", :red
    return
  end

  source_paths.each do |source_path|
    say "Source: #{source_path}", :blue

    translator = Translator.new(
      source_path: source_path,
      target_locales: target_locales,
      output_dir: options[:output] || Traductor.configuration.output_dir,
      incremental: !options[:full]
    )

    if options[:dry_run]
      summary = translator.dry_run
      summary.each do |locale, info|
        say "  #{locale}: #{info[:keys_to_translate]} keys to translate", :cyan
        info[:keys].first(10).each { |key| say "    - #{key}" }
        say "    ... and #{info[:keys].size - 10} more" if info[:keys].size > 10
      end
    else
      result = translator.call

      result.results.each do |locale, path|
        say "  #{locale}: #{path}", :green
      end

      result.errors.each do |error|
        say "  #{error[:locale]}: ERROR - #{error[:error]}", :red
      end
    end
  end
end

#version ⇒ Object



146
147
148
# File 'lib/traductor/cli.rb', line 146

def version
  say "traductor #{Traductor::VERSION}"
end