Class: I18nContextGenerator::Writers::JsonWriter
- Inherits:
-
Object
- Object
- I18nContextGenerator::Writers::JsonWriter
- Includes:
- ResultSerialization
- Defined in:
- lib/i18n_context_generator/writers/json_writer.rb
Overview
Writes extraction results to a JSON file.
Constant Summary
Constants included from ResultSerialization
ResultSerialization::OUTPUT_SCHEMA_VERSION
Instance Method Summary collapse
Instance Method Details
#write(results, path, metrics: nil, output: $stdout) ⇒ Object
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/i18n_context_generator/writers/json_writer.rb', line 11 def write(results, path, metrics: nil, output: $stdout) document = { schema_version: OUTPUT_SCHEMA_VERSION, generated_at: Time.now.iso8601, version: I18nContextGenerator::VERSION, total: results.size, metrics: metrics&.to_h, entries: results.sort_by { |result| result_sort_key(result) }.map do |result| { key: result.key, source_file: result.source_file, translation_key: result.translation_key, text: result.text, context: { description: result.description, ui_element: result.ui_element, tone: result.tone, max_length: result.max_length, confidence: result.confidence, ambiguity_reason: result.ambiguity_reason }, locations: serialize_locations(result.locations), changed_locations: serialize_locations(result.changed_locations), changed_location_groups: serialize_location_groups(result.changed_location_groups), changed_translation_locations: serialize_locations(result.changed_translation_locations), status: result.status, telemetry: { cache_hit: result.cache_hit, request_count: result.request_count, input_tokens: result.input_tokens, output_tokens: result.output_tokens, retries: result.retries }, error: result.error } end } rendered = Oj.dump(document, indent: 2, mode: :compat) if path == '-' output.write(rendered) output.write("\n") unless rendered.end_with?("\n") else File.write(path, rendered) end end |