Class: I18nSpec::LocaleFile
- Inherits:
-
Object
- Object
- I18nSpec::LocaleFile
- Defined in:
- lib/i18n-spec/models/locale_file.rb
Constant Summary collapse
- PLURALIZATION_KEYS =
%w{zero one two few many other}
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#filepath ⇒ Object
Returns the value of attribute filepath.
Instance Method Summary collapse
- #content ⇒ Object
- #flattened_translations ⇒ Object
- #has_one_top_level_namespace? ⇒ Boolean
-
#initialize(filepath) ⇒ LocaleFile
constructor
A new instance of LocaleFile.
- #invalid_pluralization_keys ⇒ Object
- #is_named_like_top_level_namespace? ⇒ Boolean
- #is_parseable? ⇒ Boolean
- #pluralizations ⇒ Object
- #translations ⇒ Object
Constructor Details
#initialize(filepath) ⇒ LocaleFile
Returns a new instance of LocaleFile.
8 9 10 11 |
# File 'lib/i18n-spec/models/locale_file.rb', line 8 def initialize(filepath) @filepath = filepath @errors = {} end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
6 7 8 |
# File 'lib/i18n-spec/models/locale_file.rb', line 6 def errors @errors end |
#filepath ⇒ Object
Returns the value of attribute filepath.
5 6 7 |
# File 'lib/i18n-spec/models/locale_file.rb', line 5 def filepath @filepath end |
Instance Method Details
#content ⇒ Object
13 14 15 |
# File 'lib/i18n-spec/models/locale_file.rb', line 13 def content @content ||= IO.read(@filepath) end |
#flattened_translations ⇒ Object
21 22 23 |
# File 'lib/i18n-spec/models/locale_file.rb', line 21 def flattened_translations @flattened_translations ||= flatten_tree(translations.values.first) end |
#has_one_top_level_namespace? ⇒ Boolean
61 62 63 |
# File 'lib/i18n-spec/models/locale_file.rb', line 61 def has_one_top_level_namespace? translations.keys.size == 1 end |
#invalid_pluralization_keys ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/i18n-spec/models/locale_file.rb', line 37 def invalid_pluralization_keys invalid = [] pluralizations.each do |parent, pluralization| unless pluralization.keys.all? { |key| PLURALIZATION_KEYS.include?(key) } invalid << parent end end @errors[:invalid_pluralization_keys] = invalid unless invalid.empty? invalid end |
#is_named_like_top_level_namespace? ⇒ Boolean
65 66 67 |
# File 'lib/i18n-spec/models/locale_file.rb', line 65 def is_named_like_top_level_namespace? translations.keys.first == File.basename(@filepath, File.extname(@filepath)) end |
#is_parseable? ⇒ Boolean
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/i18n-spec/models/locale_file.rb', line 48 def is_parseable? begin yaml_load_content true rescue YAML::ParseError => e @errors[:unparseable] = e.to_s false rescue ArgumentError => e @errors[:unparseable] = e.to_s false end end |
#pluralizations ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/i18n-spec/models/locale_file.rb', line 25 def pluralizations result = flatten_tree(translations).select do |key, value| value.is_a?(Hash) end if result.is_a?(Array) Hash[result] else result end end |
#translations ⇒ Object
17 18 19 |
# File 'lib/i18n-spec/models/locale_file.rb', line 17 def translations @translations ||= yaml_load_content end |