Class: Woods::Extractors::I18nExtractor

Inherits:
Object
  • Object
show all
Includes:
SharedUtilityMethods
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

Methods included from SharedUtilityMethods

#app_source?, #condition_label, #count_loc, #detect_entry_points, #extract_action_filter_actions, #extract_callback_conditions, #extract_class_methods, #extract_class_name, #extract_custom_errors, #extract_initialize_params, #extract_namespace, #extract_parent_class, #extract_public_methods, #find_files_in_directories, #resolve_source_location, #skip_file?

Constructor Details

#initializeI18nExtractor

Returns a new instance of I18nExtractor.



28
29
30
31
# File 'lib/woods/extractors/i18n_extractor.rb', line 28

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:



36
37
38
39
40
# File 'lib/woods/extractors/i18n_extractor.rb', line 36

def extract_all
  find_files_in_directories(@directories, '**/*.yml').filter_map do |file|
    extract_i18n_file(file)
  end
end

#extract_i18n_file(file_path) ⇒ ExtractedUnit?

Extract a single locale file

Parameters:

  • file_path (String)

    Path to the YAML locale file

Returns:



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

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