instance method localize

Ruby on Rails 2.2.3

Since v2.2.3 Last seen in v2.2.3

Signature

localize(locale, object, format = :default)

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

Parameters

locale req
object req
format opt = :default
Source
# File activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb, line 48
      def localize(locale, object, format = :default)
        raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime)
        
        type = object.respond_to?(:sec) ? 'time' : 'date'
        # TODO only translate these if format is a String?
        formats = translate(locale, :"#{type}.formats")
        format = formats[format.to_sym] if formats && formats[format.to_sym]
        # TODO raise exception unless format found?
        format = format.to_s.dup

        # TODO only translate these if the format string is actually present
        # TODO check which format strings are present, then bulk translate then, then replace them
        format.gsub!(/%a/, translate(locale, :"date.abbr_day_names")[object.wday]) 
        format.gsub!(/%A/, translate(locale, :"date.day_names")[object.wday])
        format.gsub!(/%b/, translate(locale, :"date.abbr_month_names")[object.mon])
        format.gsub!(/%B/, translate(locale, :"date.month_names")[object.mon])
        format.gsub!(/%p/, translate(locale, :"time.#{object.hour < 12 ? :am : :pm}")) if object.respond_to? :hour
        object.strftime(format)
      end

Defined in activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb line 48 · View on GitHub · Improve this page · Find usages on GitHub

Defined in I18n::Backend::Simple

Type at least 2 characters to search.

↑↓ navigate · open · esc close