Class: I18nSpec::LocaleFile

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

Instance Method Summary collapse

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

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'lib/i18n-spec/models/locale_file.rb', line 6

def errors
  @errors
end

#filepathObject

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

#contentObject



13
14
15
# File 'lib/i18n-spec/models/locale_file.rb', line 13

def content
  @content ||= IO.read(@filepath)
end

#flattened_translationsObject



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_a_valid_locale?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/i18n-spec/models/locale_file.rb', line 69

def has_a_valid_locale?
  ISO::Tag.new(translations.keys.first).valid?
end

#has_one_top_level_namespace?Boolean

Returns:

  • (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_keysObject



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

Returns:

  • (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

Returns:

  • (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

#pluralizationsObject



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

#translationsObject



17
18
19
# File 'lib/i18n-spec/models/locale_file.rb', line 17

def translations
  @translations ||= yaml_load_content
end