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"
unless File.exist?(path)
puts "❌ Source path not found: #{path}"
puts " Please check that the file or directory exists"
exit 1
end
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
|