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

Returns:

  • (Boolean)


86
87
88
# File 'lib/i18n-spec/models/locale_file.rb', line 86

def has_one_top_level_namespace?
  translations.keys.size == 1
end

#invalid_pluralization_keysObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/i18n-spec/models/locale_file.rb', line 45

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)


90
91
92
# File 'lib/i18n-spec/models/locale_file.rb', line 90

def is_named_like_top_level_namespace?
  locale_code == File.basename(@filepath, File.extname(@filepath))
end

#is_parseable?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/i18n-spec/models/locale_file.rb', line 67

def is_parseable?
  if RUBY_VERSION < "1.9.3"
    yaml_parse_exception = YAML::ParseError
  else
    yaml_parse_exception = YAML::SyntaxError
  end

  begin
    yaml_load_content
    true
  rescue yaml_parse_exception => e
    @errors[:unparseable] = e.to_s
    false
  rescue ArgumentError => e
    @errors[:unparseable] = e.to_s
    false
  end
end

#localeObject



25
26
27
# File 'lib/i18n-spec/models/locale_file.rb', line 25

def locale
  @locale ||= ISO::Tag.new(locale_code)
end

#locale_codeObject



29
30
31
# File 'lib/i18n-spec/models/locale_file.rb', line 29

def locale_code
  translations.keys.first
end

#missing_pluralization_keysObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/i18n-spec/models/locale_file.rb', line 56

def missing_pluralization_keys
  return_data = {}
  rule_names = locale.language.plural_rule_names
  pluralizations.each do |parent, pluralization|
    missing_keys = rule_names - pluralization.keys
    return_data[parent] = missing_keys if missing_keys.any?
  end
  @errors[:missing_pluralization_keys] = return_data if return_data.any?
  return_data
end

#pluralizationsObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/i18n-spec/models/locale_file.rb', line 33

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