Class: I18nContextGenerator::Parsers::YamlParser
- Defined in:
- lib/i18n_context_generator/parsers/yaml_parser.rb
Overview
Parses YAML translation files (including Rails i18n style) into TranslationEntry objects.
Instance Method Summary collapse
-
#initialize(locale: nil) ⇒ YamlParser
constructor
A new instance of YamlParser.
- #parse(path) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(locale: nil) ⇒ YamlParser
Returns a new instance of YamlParser.
7 8 9 10 |
# File 'lib/i18n_context_generator/parsers/yaml_parser.rb', line 7 def initialize(locale: nil) super() @locale = locale end |
Instance Method Details
#parse(path) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/i18n_context_generator/parsers/yaml_parser.rb', line 12 def parse(path) data = YAML.safe_load_file(path, permitted_classes: []) raise Error, "Invalid YAML translation file #{path}: root must be a mapping" unless data.is_a?(Hash) data = locale_root(data, path) if @locale 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 Psych::SyntaxError => e raise Error, "Failed to parse YAML translation file #{path}: #{e.problem} at line #{e.line}, column #{e.column}" rescue Psych::Exception => e raise Error, "Failed to parse YAML translation file #{path}: #{e.}" end |