Class: InertiaI18n::FileConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/inertia_i18n/file_converter.rb

Class Method Summary collapse

Class Method Details

.convert_allObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/inertia_i18n/file_converter.rb', line 10

def convert_all
  config = InertiaI18n.configuration
  results = []

  config.locales.each do |locale|
    result = convert_locale(locale)
    results << result
  end

  results
end

.convert_locale(locale) ⇒ Object



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
# File 'lib/inertia_i18n/file_converter.rb', line 22

def convert_locale(locale)
  config = InertiaI18n.configuration

  # Load all YAML files for this locale
  yaml_files = config.source_paths.flat_map do |path|
    Dir.glob(File.join(path, config.source_pattern))
  end
  merged_data = {}

  yaml_files.each do |file|
    data = YAML.load_file(file, permitted_classes: [Symbol])
    deep_merge!(merged_data, data) if data
  rescue Psych::SyntaxError => e
    warn "Warning: Failed to parse #{file}: #{e.message}"
  end

  # Convert to JSON
  converter = Converter.new(merged_data, locale: locale)
  json_data = converter.convert

  # Write JSON file
  output_file = File.join(config.target_path, "#{locale}.json")
  FileUtils.mkdir_p(config.target_path)
  File.write(output_file, JSON.pretty_generate(json_data) + "\n")

  {
    locale: locale,
    output_file: output_file,
    keys_count: LocaleLoader.extract_keys(json_data).size,
    source_files: yaml_files.size
  }
end