Class: Woods::Extractors::I18nExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/woods/extractors/i18n_extractor.rb

Overview

I18nExtractor handles internationalization locale file extraction.

Parses YAML files from ‘config/locales/` to extract translation keys, locale information, and key structure. Each locale file becomes one ExtractedUnit.

Examples:

extractor = I18nExtractor.new
units = extractor.extract_all
en = units.find { |u| u.identifier == "en.yml" }

Constant Summary collapse

I18N_DIRECTORIES =

Directories to scan for locale files

%w[
  config/locales
].freeze

Instance Method Summary collapse

Constructor Details

#initializeI18nExtractor

Returns a new instance of I18nExtractor.



24
25
26
27
# File 'lib/woods/extractors/i18n_extractor.rb', line 24

def initialize
  @directories = I18N_DIRECTORIES.map { |d| Rails.root.join(d) }
                                 .select(&:directory?)
end

Instance Method Details

#extract_allArray<ExtractedUnit>

Extract all locale files

Returns:



32
33
34
35
36
37
38
# File 'lib/woods/extractors/i18n_extractor.rb', line 32

def extract_all
  @directories.flat_map do |dir|
    Dir[dir.join('**/*.yml')].filter_map do |file|
      extract_i18n_file(file)
    end
  end
end

#extract_i18n_file(file_path) ⇒ ExtractedUnit?

Extract a single locale file

Parameters:

  • file_path (String)

    Path to the YAML locale file

Returns:



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
# File 'lib/woods/extractors/i18n_extractor.rb', line 44

def extract_i18n_file(file_path)
  source = File.read(file_path)
  data = YAML.safe_load(source, permitted_classes: [Symbol, Date, Time, Regexp])

  return nil unless data.is_a?(Hash) && data.any?

  identifier = build_identifier(file_path)
  locale = data.keys.first

  unit = ExtractedUnit.new(
    type: :i18n,
    identifier: identifier,
    file_path: file_path
  )

  unit.namespace = locale
  unit.source_code = source
  unit. = (data, locale)
  unit.dependencies = []

  unit
rescue StandardError => e
  Rails.logger.error("Failed to extract i18n #{file_path}: #{e.message}")
  nil
end