Method: I18n::Backend::Base#localize

Defined in:
lib/i18n/backend/base.rb

#localize(locale, object, format = :default, options = EMPTY_HASH) ⇒ Object

Acts the same as strftime, but uses a localized version of the format string. Takes a key from the date/time formats translations as a format argument (e.g., :short in :'date.formats').

Raises:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/i18n/backend/base.rb', line 77

def localize(locale, object, format = :default, options = EMPTY_HASH)
  if object.nil? && options.include?(:default)
    return options[:default]
  end
  raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime)

  if Symbol === format
    key  = format
    type = object.respond_to?(:sec) ? 'time' : 'date'
    options = options.merge(:raise => true, :object => object, :locale => locale)
    format  = I18n.t(:"#{type}.formats.#{key}", **options)
  end

  format = translate_localization_format(locale, object, format, options)
  object.strftime(format)
end