Method: I18n::Backend::Base#localize

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

- (Object) localize(locale, object, format = :default, options = {})

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:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/i18n/backend/base.rb', line 52

def localize(locale, object, format = :default, options = {})
  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 = resolve(locale, object, format, options)
  format = format.to_s.gsub(/%[aAbBpP]/) do |match|
    case match
    when '%a' then I18n.t(:date.abbr_day_names",                  :locale => locale, :format => format)[object.wday]
    when '%A' then I18n.t(:date.day_names",                       :locale => locale, :format => format)[object.wday]
    when '%b' then I18n.t(:date.abbr_month_names",                :locale => locale, :format => format)[object.mon]
    when '%B' then I18n.t(:date.month_names",                     :locale => locale, :format => format)[object.mon]
    when '%p' then I18n.t(:time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format).upcase if object.respond_to? :hour
    when '%P' then I18n.t(:time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format).downcase if object.respond_to? :hour
    end
  end

  object.strftime(format)
end

Comments