Module: I18nliner::CallHelpers

Extended by:
CallHelpers
Included in:
CallHelpers, Extractors::TranslateCall, PreProcessors::ErbPreProcessor::TBlock
Defined in:
lib/i18nliner/call_helpers.rb

Constant Summary collapse

ALLOWED_PLURALIZATION_KEYS =
[:zero, :one, :few, :many, :other]
REQUIRED_PLURALIZATION_KEYS =
[:one, :other]

Instance Method Summary collapse

Instance Method Details

#infer_arguments(args) ⇒ Object

Raises:

  • (ArgumentError)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/i18nliner/call_helpers.rb', line 102

def infer_arguments(args)
  raise ArgumentError.new("wrong number of arguments (#{args.size} for 1..3)") if args.empty? || args.size > 3
  if args.size == 2 && args[1].is_a?(Hash) && args[1][:default]
    return args
  end

  has_key = key_provided?(*args)
  args.unshift nil unless has_key

  default = nil
  default_or_options = args[1]
  if args[2] || default_or_options.is_a?(String) || pluralization_hash?(default_or_options)
    default = args.delete_at(1)
  end
  args << {} if args.size == 1
  options = args[1]
  raise ArgumentError.new("invalid default translation. expected Hash or String, got #{default.class}") unless default.nil? || default.is_a?(String) || default.is_a?(Hash)
  raise ArgumentError.new("invalid options argument. expected Hash, got #{options.class}") unless options.is_a?(Hash)
  options[:default] = default if default
  options[:i18nliner_inferred_key] = true unless has_key
  unless has_key
    args[0] = infer_key(default, options)
  end
  args
end

#infer_key(default, translate_options = {}) ⇒ Object



51
52
53
54
55
56
# File 'lib/i18nliner/call_helpers.rb', line 51

def infer_key(default, translate_options = {})
  return unless default && (default.is_a?(String) || default.is_a?(Hash))
  default = normalize_default(default, translate_options, :remove_whitespace => true)
  default = default[:other].to_s if default.is_a?(Hash)
  keyify(default)
end

#infer_pluralization_hash(default, translate_options) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/i18nliner/call_helpers.rb', line 43

def infer_pluralization_hash(default, translate_options)
  str = (default.is_a?(Array) && default.size == 1) ? default.first : default
  return default unless str.is_a?(String) &&
                        str =~ /\A[\w\-]+\z/ &&
                        translate_options.include?(:count)
  {:one => "1 #{str}", :other => "%{count} #{str.pluralize}"}
end

#key_provided?(key_or_default = nil, default_or_options = nil, maybe_options = nil, *others) ⇒ Boolean

Possible translate signatures:

key [, options] key, default_string [, options] key, default_hash, options default_string [, options] default_hash, options

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


86
87
88
89
90
91
92
93
94
# File 'lib/i18nliner/call_helpers.rb', line 86

def key_provided?(key_or_default = nil, default_or_options = nil, maybe_options = nil, *others)
  return false if key_or_default.is_a?(Hash)
  return true if key_or_default.is_a?(Symbol) || key_or_default.nil? || key_or_default.is_a?(Array)
  raise ArgumentError.new("invalid key_or_default argument. expected String, Symbol, Array, Hash or nil, got #{key_or_default.class}") unless key_or_default.is_a?(String)
  return true if default_or_options.is_a?(String)
  return true if maybe_options
  return true if key_or_default =~ /\A\.?(\w+\.)+\w+\z/
  false
end

#keyify(string) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/i18nliner/call_helpers.rb', line 71

def keyify(string)
  case I18nliner.inferred_key_format
  when :underscored       then keyify_underscored(string)
  when :underscored_crc32 then keyify_underscored_crc32(string)
  else                         string
  end
end

#keyify_underscored(string) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/i18nliner/call_helpers.rb', line 58

def keyify_underscored(string)
  key = I18n.transliterate(string, :locale => I18n.default_locale).to_s
  key.downcase!
  key.gsub!(/[^a-z0-9_]+/, '_')
  key.gsub!(/\A_|_\z/, '')
  key[0...I18nliner.underscored_key_length]
end

#keyify_underscored_crc32(string) ⇒ Object



66
67
68
69
# File 'lib/i18nliner/call_helpers.rb', line 66

def keyify_underscored_crc32(string)
  checksum = Zlib.crc32("#{string.size.to_s}:#{string}").to_s(16)
  "#{keyify_underscored(string)}_#{checksum}"
end

#normalize_default(default, translate_options = {}, options = {}) ⇒ Object



15
16
17
18
19
# File 'lib/i18nliner/call_helpers.rb', line 15

def normalize_default(default, translate_options = {}, options = {})
  default = infer_pluralization_hash(default, translate_options)
  default = normalize_whitespace(default, options)
  default
end

#normalize_key(key, scope, inferred, i18n_scope) ⇒ Object



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

def normalize_key(key, scope, inferred, i18n_scope)
  scope.normalize_key(key, inferred, i18n_scope)
end

#normalize_whitespace(default, options) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/i18nliner/call_helpers.rb', line 21

def normalize_whitespace(default, options)
  return default unless default.is_a?(String) || default.is_a?(Hash) || default.is_a?(Array)

  default = default.dup

  if default.is_a?(Hash)
    default.each { |key, value| default[key] = normalize_whitespace(value, options) }
    return default
  elsif default.is_a?(Array)
    return default.map{|value| value.is_a?(String) ? normalize_whitespace(value, options) : value}
  end

  if options[:remove_whitespace]
    default.gsub!(/\s+/, ' ')
    default.strip!
  else
    default.sub!(/\s*\n\z/, '')
    default.lstrip!
  end
  default
end

#pluralization_hash?(hash) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
# File 'lib/i18nliner/call_helpers.rb', line 96

def pluralization_hash?(hash)
  hash.is_a?(Hash) &&
  hash.size > 0 &&
  (hash.keys - ALLOWED_PLURALIZATION_KEYS).size == 0
end