Class: Jekyll::LanguagePlugin::DateLocalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/language-plugin/date_localizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(translation) ⇒ DateLocalizer

Returns a new instance of DateLocalizer.



9
10
11
# File 'lib/jekyll/language-plugin/date_localizer.rb', line 9

def initialize(translation)
  @translation = translation
end

Instance Attribute Details

#translationObject (readonly)

Returns the value of attribute translation.



7
8
9
# File 'lib/jekyll/language-plugin/date_localizer.rb', line 7

def translation
  @translation
end

Instance Method Details

#localize_date(date, format) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jekyll/language-plugin/date_localizer.rb', line 13

def localize_date(date, format)
  # validate language translation
  if !['abbr_daynames', 'daynames', 'abbr_monthnames', 'monthnames'].all? {|s| translation.key?(s) && translation[s].is_a?(Array) } ||
     translation['abbr_daynames'].size < 7 || translation['daynames'].size < 7 ||
     translation['abbr_monthnames'].size < 12 || translation['monthnames'].size < 12
    raise Jekyll::LanguagePlugin::PluginError.new('Invalid date translation.')
  end

  date.strftime(
    format.gsub(/%([aAbB])/){ |m|
        case $1
          when 'a'; translation['abbr_daynames'][date.wday]
          when 'A'; translation['daynames'][date.wday]
          when 'b'; translation['abbr_monthnames'][date.mon-1]
          when 'B'; translation['monthnames'][date.mon-1]
          else
            raise Jekyll::LanguagePlugin::PluginError.new('Internal error.')
        end
      })
end