lib/i18n-spec/models/locale_file.rb in i18n-spec-0.1.3 vs lib/i18n-spec/models/locale_file.rb in i18n-spec-0.2.0

- old
+ new

@@ -13,21 +13,27 @@ def content @content ||= IO.read(@filepath) end def translations - @translations ||= Psych.load(content) + @translations ||= yaml_load_content end def flattened_translations @flattened_translations ||= flatten_tree(translations.values.first) end def pluralizations - flatten_tree(translations).select do |key, value| + result = flatten_tree(translations).select do |key, value| value.is_a?(Hash) end + + if result.is_a?(Array) + Hash[result] + else + result + end end def invalid_pluralization_keys invalid = [] pluralizations.each do |parent, pluralization| @@ -39,15 +45,18 @@ invalid end def is_parseable? begin - Psych.load(content) + yaml_load_content true - rescue Psych::SyntaxError => e + rescue YAML::ParseError => e @errors[:unparseable] = e.to_s false + rescue ArgumentError => e + @errors[:unparseable] = e.to_s + false end end def has_one_top_level_namespace? translations.keys.size == 1 @@ -72,9 +81,17 @@ end def pluralization_data?(data) keys = data.keys.map(&:to_s) keys.any? {|k| PLURALIZATION_KEYS.include?(k) } + end + + def yaml_load_content + if defined? Psych + Psych.load(content) + else + YAML.load(content) + end end end end