Module: FormInput::Parameter::R18nMethods

Included in:
FormInput::Parameter
Defined in:
lib/form_input/r18n.rb

Overview

R18n specific methods.

Constant Summary collapse

UNLOCALIZED_OPTIONS =

Parameter options which are known to be not localized.

[
  :required, :disabled, :array, :hash, :type, :data, :tag, :tags, :filter, :transform, :format, :class, :check, :test, :reject, :match,
  :min_key, :max_key, :match_key, :min_count, :max_count, :min, :max, :inf, :sup, :min_size, :max_size, :min_bytesize, :max_bytesize,
]

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object

Automatically attempt to translate available parameter options.



27
28
29
30
31
32
33
34
35
36
# File 'lib/form_input/r18n.rb', line 27

def []( name )
  if form and r18n
    value = opts[ name ]
    if value.is_a?( String ) or ( value.nil? and not UNLOCALIZED_OPTIONS.include?( name ) )
      text = pt( name )
      return text.to_s if text.translated?
    end
  end
  super
end

#format_error_message(msg, count = nil, singular = nil, *rest) ⇒ Object

Localized version of error message formatting. See original implementation for details.



39
40
41
42
43
44
45
46
# File 'lib/form_input/r18n.rb', line 39

def format_error_message( msg, count = nil, singular = nil, *rest )
  return super unless msg.is_a?( Symbol ) and r18n
  if limit = count and singular
    limit = t.form_input.units[ singular, count ].to_s
  end
  text = t.form_input.errors[ msg, *limit, self ].to_s
  super( text )
end

#ft(*args) ⇒ Object

Like t helper, except that the translation is looked up in the forms. scope. Supports both ft.name( args ) and ft( :name, args ) forms.



50
51
52
# File 'lib/form_input/r18n.rb', line 50

def ft( *args )
  form.ft( *args )
end

#genderObject

Get the gender string used for inflecting the parameter messages.



77
78
79
# File 'lib/form_input/r18n.rb', line 77

def gender
  ( self[ :gender ] || ( t.form_input.default_gender | 'n' ) ).to_s
end

#inflectionObject

Get the inflection string used for correctly inflecting the parameter messages. Note that it ignores the noun case as the parameter names are supposed to use the nominative case anyway.



63
64
65
# File 'lib/form_input/r18n.rb', line 63

def inflection
  ( self[ :inflect ] || "#{pluralize}#{gender}" ).to_s
end

#pluralizeObject

Get the string corresponding to the grammatical number of the parameter name used for inflecting the parameter messages.



68
69
70
71
72
73
74
# File 'lib/form_input/r18n.rb', line 68

def pluralize
  p = self[ :plural ]
  p = ! scalar? if p.nil?
  p = 's' if p == false
  p = 'p' if p == true
  p.to_s
end

#pt(*args) ⇒ Object

Like t helper, except that the translation is looked up in the forms.. scope. Supports both pt.name( args ) and pt( :name, args ) forms. The latter automatically adds self as last argument to support inflection.



56
57
58
59
# File 'lib/form_input/r18n.rb', line 56

def pt( *args )
  translation = ft[ name ]
  args.empty? ? translation : translation[ *args, self ]
end