Class: I18nliner::Extractors::TranslateCall

Inherits:
Object
  • Object
show all
Includes:
CallHelpers
Defined in:
lib/i18nliner/extractors/translate_call.rb

Constant Summary

Constants included from CallHelpers

CallHelpers::ALLOWED_PLURALIZATION_KEYS, CallHelpers::REQUIRED_PLURALIZATION_KEYS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CallHelpers

#infer_arguments, #infer_key, #infer_pluralization_hash, #key_provided?, #keyify, #keyify_underscored, #keyify_underscored_crc32, #normalize_default, #normalize_key, #normalize_whitespace, #pluralization_hash?

Constructor Details

#initialize(scope, line, method, args, meta = {}) ⇒ TranslateCall

Returns a new instance of TranslateCall.


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/i18nliner/extractors/translate_call.rb', line 13

def initialize(scope, line, method, args, meta = {})
  @scope = scope
  @line = line
  @method = method
  @meta = meta

  normalize_arguments(args)

  validate
  normalize
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default


11
12
13
# File 'lib/i18nliner/extractors/translate_call.rb', line 11

def default
  @default
end

#keyObject (readonly)

Returns the value of attribute key


11
12
13
# File 'lib/i18nliner/extractors/translate_call.rb', line 11

def key
  @key
end

Instance Method Details

#normalizeObject


31
32
33
34
# File 'lib/i18nliner/extractors/translate_call.rb', line 31

def normalize
  @key = normalize_key(@key, @scope, @options[:i18nliner_inferred_key], @options[:scope])
  @default = normalize_default(@default, @options || {}, {:remove_whitespace => @scope.remove_whitespace?})
end

#translationsObject


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/i18nliner/extractors/translate_call.rb', line 36

def translations
  return [] unless default
  keys = Array(key)
  keys.inject([]) do |result, key|
    if default.is_a?(String)
      result << [key, default]
    else
      result.concat default.map { |dk, dv| ["#{key}.#{dk}", dv] }
    end
  end
end

#validateObject


25
26
27
28
29
# File 'lib/i18nliner/extractors/translate_call.rb', line 25

def validate
  validate_key
  validate_default
  validate_options
end

#validate_defaultObject


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/i18nliner/extractors/translate_call.rb', line 51

def validate_default
  return unless @default.is_a?(Hash)
  if (keys = @default.keys - ALLOWED_PLURALIZATION_KEYS).size > 0
    raise InvalidPluralizationKeyError.new(@line, keys)
  elsif REQUIRED_PLURALIZATION_KEYS & (keys = @default.keys) != REQUIRED_PLURALIZATION_KEYS
    raise MissingPluralizationKeyError.new(@line, keys)
  else
    @default.values.each do |value|
      raise InvalidPluralizationDefaultError.new(@line, value) unless value.is_a?(String)
    end
  end

  unless I18nliner.infer_interpolation_values
    if @default.is_a?(String)
      validate_interpolation_values(@key, @default)
    else
      @default.each_pair do |sub_key, default|
        validate_interpolation_values("#{@key}.#{sub_key}", default)
      end
    end
  end
end

#validate_keyObject


48
49
# File 'lib/i18nliner/extractors/translate_call.rb', line 48

def validate_key
end