module Jekyll::Filters::DateLocal

Public Instance Methods

date_local(input, format) click to toggle source

Translates month and day names. This requires certain values on your _data/LANG.yml

Example usage:

{{ page.date | date_local: '%e de %B de %Y' }}

@see {_data/es.yml} @param [String|Date|Time|DateTime] @param [String] @return [String]

   # File lib/jekyll/filters/date_local.rb
17 def date_local(input, format)
18   require 'date'
19 
20   input = ::Jekyll::Utils.parse_date(input) unless input.respond_to? :mon
21 
22   # Return early if we don't need to translate
23   return input.strftime(format) unless /%(|\^)[aAbBpP]/ =~ format
24 
25   input.strftime translate_localization_format(input, format)
26 rescue ArgumentError, TypeError, ::Jekyll::Errors::InvalidDateError => e
27   Jekyll.logger.warn "#{input} is not a valid date: #{e.message}"
28   input
29 end

Private Instance Methods

i18n() click to toggle source
   # File lib/jekyll/filters/date_local.rb
56 def i18n
57   @i18n ||= site.data.dig site.config['lang']
58 end
site() click to toggle source
   # File lib/jekyll/filters/date_local.rb
52 def site
53   @site ||= @context.registers[:site]
54 end
translate_localization_format(object, format) click to toggle source

Adapted from the i18n gem @see {github.com/ruby-i18n/i18n/blob/a8f4fdcb197e56b5a698d1bc68007dd0871c03bf/lib/i18n/backend/base.rb}

   # File lib/jekyll/filters/date_local.rb
35 def translate_localization_format(object, format)
36   format.to_s.gsub(/%(|\^)[aAbBpP]/) do |match|
37     case match
38     when '%a'  then i18n.dig('date', 'abbr_day_names', object.wday - 1)
39     when '%^a' then i18n.dig('date', 'abbr_day_names', object.wday - 1)&.upcase
40     when '%A'  then i18n.dig('date', 'day_names', object.wday - 1)
41     when '%^A' then i18n.dig('date', 'day_names', object.wday - 1)&.upcase
42     when '%b'  then i18n.dig('date', 'abbr_month_names', object.mon - 1)
43     when '%^b' then i18n.dig('date', 'abbr_month_names', object.mon - 1)&.upcase
44     when '%B'  then i18n.dig('date', 'month_names', object.mon - 1)
45     when '%^B' then i18n.dig('date', 'month_names', object.mon - 1)&.upcase
46     when '%p'  then i18n.dig('time', object.hour < 12 ? 'am' : 'pm')&.upcase if object.respond_to? :hour
47     when '%P'  then i18n.dig('time', object.hour < 12 ? 'am' : 'pm')&.downcase if object.respond_to? :hour
48     end
49   end
50 end