Class: Honyaku::Translator
- Inherits:
-
Object
- Object
- Honyaku::Translator
- Defined in:
- lib/honyaku/translator.rb
Constant Summary collapse
- LINES_PER_CHUNK =
250
Instance Method Summary collapse
- #fix_yaml(file_path) ⇒ Object
-
#initialize(api_key: nil, model: "gpt-4", translation_rules: []) ⇒ Translator
constructor
A new instance of Translator.
- #translate_hash(file_path, from_locale, to_locale) ⇒ Object
Constructor Details
#initialize(api_key: nil, model: "gpt-4", translation_rules: []) ⇒ Translator
Returns a new instance of Translator.
8 9 10 11 12 13 |
# File 'lib/honyaku/translator.rb', line 8 def initialize(api_key: nil, model: "gpt-4", translation_rules: []) api_key ||= ENV["HONYAKU_OPENAI_API_KEY"] || ENV["OPENAI_API_KEY"] @client = OpenAI::Client.new(access_token: api_key) @model = model @translation_rules = translation_rules end |
Instance Method Details
#fix_yaml(file_path) ⇒ Object
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 102 103 |
# File 'lib/honyaku/translator.rb', line 44 def fix_yaml(file_path) content = File.read(file_path) fixed_any = false loop do begin YAML.safe_load(content, aliases: true) puts "✅ No more YAML errors found" return content rescue Psych::SyntaxError => e # If OpenAI returned invalid YAML structure, signal that we need to retranslate if e..include?("did not find expected key while parsing a block mapping") raise "Translation resulted in invalid YAML structure - needs retranslation" end lines = content.lines line_number = e.line - 1 # YAML errors are 1-based problematic_line = lines[line_number] puts "🔧 Found YAML error on line #{e.line}: #{e.message}" puts " #{problematic_line.strip}" # Only try to fix common syntax issues if e..include?("cannot start any token") fixed = false # Fix case 1: Values starting with %{var} need quotes if problematic_line.include?("%{") && problematic_line =~ /^(\s*[^:]+:\s*)(?:(&\w+)\s+)?(%\{.+)$/ prefix, reference, value = $1, $2, $3 fixed_line = if reference "#{prefix}#{reference} \"#{value}\"" else "#{prefix}\"#{value}\"" end fixed = true # Fix case 2: Fix incorrect spacing in %{ var } elsif problematic_line.include?("% {") fixed_line = problematic_line.gsub("% {", "%{") fixed = true end if fixed # Update the line lines[line_number] = "#{fixed_line}\n" content = lines.join fixed_any = true next # Continue to the next iteration to find more errors end end # If we get here, we couldn't fix this error if fixed_any puts "❌ Unable to fix remaining YAML errors" else puts "❌ Unable to fix any YAML errors" end return content end end end |
#translate_hash(file_path, from_locale, to_locale) ⇒ Object
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 42 |
# File 'lib/honyaku/translator.rb', line 15 def translate_hash(file_path, from_locale, to_locale) yaml_content = File.read(file_path) lines = yaml_content.lines # If the file is small enough, translate it all at once if lines.size <= LINES_PER_CHUNK result = translate_chunk(yaml_content, from_locale, to_locale) raise "Translation failed" unless result return result end # Otherwise, split into chunks and translate each chunks = split_into_chunks(lines) puts "📦 Splitting file into #{chunks.size} chunks..." translated_chunks = [] chunks.each_with_index do |chunk, i| puts "🔄 Translating chunk #{i + 1} of #{chunks.size}..." result = translate_chunk(chunk, from_locale, to_locale) # If any chunk fails, abort the whole translation raise "Translation failed for chunk #{i + 1}" unless result translated_chunks << result end translated_chunks.join("\n") end |