Class: Woods::Extractors::I18nExtractor
- Inherits:
-
Object
- Object
- Woods::Extractors::I18nExtractor
- 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.
Constant Summary collapse
- I18N_DIRECTORIES =
Directories to scan for locale files
%w[ config/locales ].freeze
Instance Method Summary collapse
-
#extract_all ⇒ Array<ExtractedUnit>
Extract all locale files.
-
#extract_i18n_file(file_path) ⇒ ExtractedUnit?
Extract a single locale file.
-
#initialize ⇒ I18nExtractor
constructor
A new instance of I18nExtractor.
Constructor Details
#initialize ⇒ I18nExtractor
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_all ⇒ Array<ExtractedUnit>
Extract all locale files
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
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.}") nil end |