Class: I18nContextGenerator::Parsers::JsonParser

Inherits:
Base
  • Object
show all
Defined in:
lib/i18n_context_generator/parsers/json_parser.rb

Overview

Parses flat or nested JSON translation files into TranslationEntry objects.

Instance Method Summary collapse

Methods inherited from Base

for

Instance Method Details

#parse(path) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/i18n_context_generator/parsers/json_parser.rb', line 7

def parse(path)
  data = Oj.load_file(path)
  raise Error, "Invalid JSON translation file #{path}: root must be an object" unless data.is_a?(Hash)

  flatten_keys(data).filter_map do |key, text|
    next if text.nil? || text.to_s.strip.empty?

    TranslationEntry.new(
      key: key,
      text: text.to_s,
      source_file: path
    )
  end
rescue Oj::ParseError => e
  raise Error, "Failed to parse JSON translation file #{path}: #{e.message}"
end