Class: Honyaku::CLI

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

Instance Method Summary collapse

Instance Method Details

#fix(path) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/honyaku/cli.rb', line 96

def fix(path)
  api_key = ENV["HONYAKU_OPENAI_API_KEY"] || ENV["OPENAI_API_KEY"]
  unless api_key
    puts "❌ Please set either HONYAKU_OPENAI_API_KEY or OPENAI_API_KEY environment variable"
    exit 1
  end

  model = options[:model] || "gpt-3.5-turbo"
  
  puts "🔧 Fixing YAML formatting issues..."
  puts "📂 Processing files in #{path}..."

  fixer = Translator.new(model: model)
  
  if File.file?(path)
    fix_file(path, fixer)
  else
    Dir.glob("#{path}/**/*.yml").each do |file|
      fix_file(file, fixer)
    end
  end

  puts "✅ Fixes complete!"
end

#translate(locale) ⇒ Object



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

def translate(locale)
  api_key = ENV["HONYAKU_OPENAI_API_KEY"] || ENV["OPENAI_API_KEY"]
  unless api_key
    puts "❌ Please set either HONYAKU_OPENAI_API_KEY or OPENAI_API_KEY environment variable"
    exit 1
  end

  source_locale = options[:from] || "en"
  path = options[:path] || "config/locales"
  model = options[:model] || "gpt-4"

  # Check if the source path exists
  unless File.exist?(path)
    puts "❌ Source path not found: #{path}"
    puts "   Please check that the file or directory exists"
    exit 1
  end

  # Find all .honyakurules files from root to current path
  rules = find_translation_rules(path, locale)
  if rules.any?
    puts "📋 Found #{rules.length} translation rule file(s):"
    rules.each do |rule|
      prefix = rule[:locale_specific] ? "🌐" : "📝"
      puts "   #{prefix} #{rule[:path]}"
    end
  end

  puts "🌏 Translating from #{source_locale} to #{locale}..."
  puts "📂 Processing files in #{path}..."

  translator = Translator.new(model: model, translation_rules: rules)
  
  if File.file?(path)
    process_file(path, translator, source_locale, locale)
  else
    files = Dir.glob("#{path}/**/*.yml")
    if files.empty?
      puts "❌ No YAML files found in: #{path}"
      puts "   Make sure your path contains .yml files"
      exit 1
    end
    files.each do |file|
      process_file(file, translator, source_locale, locale)
    end
  end

  puts "✅ Translation complete!"
end

#versionObject



122
123
124
# File 'lib/honyaku/cli.rb', line 122

def version
  puts "Honyaku v#{Honyaku::VERSION}"
end