Module: I18nliner::CallHelpers
- Extended by:
- CallHelpers
- 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
- #infer_arguments(args) ⇒ Object
- #infer_key(default, translate_options = {}) ⇒ Object
- #infer_pluralization_hash(default, translate_options) ⇒ Object
-
#key_provided?(key_or_default = nil, default_or_options = nil, maybe_options = nil, *others) ⇒ Boolean
Possible translate signatures:.
- #keyify(string) ⇒ Object
- #keyify_underscored(string) ⇒ Object
- #keyify_underscored_crc32(string) ⇒ Object
- #normalize_default(default, translate_options = {}, options = {}) ⇒ Object
- #normalize_key(key, scope, inferred, i18n_scope) ⇒ Object
- #normalize_whitespace(default, options) ⇒ Object
- #pluralization_hash?(hash) ⇒ Boolean
Instance Method Details
#infer_arguments(args) ⇒ Object
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 = args[1] if args[2] || .is_a?(String) || pluralization_hash?() default = args.delete_at(1) end args << {} if args.size == 1 = 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 #{.class}") unless .is_a?(Hash) [:default] = default if default [:i18nliner_inferred_key] = true unless has_key unless has_key args[0] = infer_key(default, ) 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, = {}) return unless default && (default.is_a?(String) || default.is_a?(Hash)) default = normalize_default(default, , :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, ) str = (default.is_a?(Array) && default.size == 1) ? default.first : default return default unless str.is_a?(String) && str =~ /\A[\w\-]+\z/ && .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
86 87 88 89 90 91 92 93 94 |
# File 'lib/i18nliner/call_helpers.rb', line 86 def key_provided?(key_or_default = nil, = nil, = 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 .is_a?(String) return true if 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, = {}, = {}) default = infer_pluralization_hash(default, ) default = normalize_whitespace(default, ) 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, ) 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, ) } return default elsif default.is_a?(Array) return default.map{|value| value.is_a?(String) ? normalize_whitespace(value, ) : value} end if [:remove_whitespace] default.gsub!(/\s+/, ' ') default.strip! else default.sub!(/\s*\n\z/, '') default.lstrip! end default end |
#pluralization_hash?(hash) ⇒ 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 |