Class: Jekyll::Locale::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/locale/handler.rb

Constant Summary collapse

DEFAULT_CONFIG =
{
  "mode"        => "manual",
  "locale"      => "en-US",
  "data_dir"    => "locales",
  "content_dir" => "_locales",
  "locales_set" => [],
  "exclude_set" => [],
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Handler

Returns a new instance of Handler.



17
18
19
20
21
22
23
24
25
26
# File 'lib/jekyll/locale/handler.rb', line 17

def initialize(site)
  @site   = site
  config  = site.config["localization"]
  @config = if config.is_a?(Hash)
              Jekyll::Utils.deep_merge_hashes(DEFAULT_CONFIG, config)
            else
              DEFAULT_CONFIG
            end
  @sanitized_locale = {}
end

Instance Attribute Details

#current_localeObject



102
103
104
# File 'lib/jekyll/locale/handler.rb', line 102

def current_locale
  @current_locale ||= default_locale
end

#locale_datesObject (readonly)

Returns the value of attribute locale_dates.



5
6
7
# File 'lib/jekyll/locale/handler.rb', line 5

def locale_dates
  @locale_dates
end

Instance Method Details

#append_document(klass, canon_doc, locale) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/jekyll/locale/handler.rb', line 82

def append_document(klass, canon_doc, locale)
  locale_doc = klass.new(canon_doc, locale)
  canon_doc.locale_pages    << locale_doc
  canon_doc.collection.docs << locale_doc
  site.docs_to_write        << locale_doc
  site.docs_to_write.uniq!
end

#append_page(klass, canon_page, locale) ⇒ Object



75
76
77
78
79
80
# File 'lib/jekyll/locale/handler.rb', line 75

def append_page(klass, canon_page, locale)
  locale_page = klass.new(canon_page, locale)
  canon_page.locale_pages << locale_page
  site.pages              << locale_page
  site.pages.uniq!
end

#available_localesObject



98
99
100
# File 'lib/jekyll/locale/handler.rb', line 98

def available_locales
  @available_locales ||= user_locales + [default_locale]
end

#content_dirnameObject



114
115
116
# File 'lib/jekyll/locale/handler.rb', line 114

def content_dirname
  @content_dirname ||= fetch("content_dir")
end

#dataObject



35
36
37
38
# File 'lib/jekyll/locale/handler.rb', line 35

def data
  locale_data[sanitized_locale(current_locale)] ||
    locale_data[sanitized_locale(default_locale)] || {}
end

#default_localeObject



106
107
108
# File 'lib/jekyll/locale/handler.rb', line 106

def default_locale
  @default_locale ||= fetch("locale")
end

#filtered_portfolioObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/jekyll/locale/handler.rb', line 44

def filtered_portfolio
  @filtered_portfolio ||= begin
    portfolio.reject do |item|
      # consider only instances of class that include `Jekyll::Locale::Support` mixin
      next true unless item.is_a?(Jekyll::Locale::Support)

      item.relative_path =~ exclusion_regex
    end
  end
end

#inspectObject



132
133
134
# File 'lib/jekyll/locale/handler.rb', line 132

def inspect
  "#<#{self.class} @site=#{site}>"
end

#modeObject



118
119
120
121
122
123
# File 'lib/jekyll/locale/handler.rb', line 118

def mode
  @mode ||= begin
    value = config["mode"]
    value == "auto" ? value : DEFAULT_CONFIG["mode"]
  end
end

#portfolioObject



40
41
42
# File 'lib/jekyll/locale/handler.rb', line 40

def portfolio
  @portfolio ||= (site.docs_to_write + html_pages)
end

#readObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/jekyll/locale/handler.rb', line 55

def read
  user_locales.each do |locale|
    portfolio.each do |canon_doc|
      # consider only instances of class that include `Jekyll::Locale::Support` mixin
      next unless canon_doc.is_a?(Jekyll::Locale::Support)

      loc_page_path = site.in_source_dir(content_dirname, locale, canon_doc.relative_path)
      next unless File.exist?(loc_page_path)
      next unless Jekyll::Utils.has_yaml_header?(loc_page_path)

      case canon_doc
      when Jekyll::Page
        append_page(Locale::Page, canon_doc, locale)
      when Jekyll::Document
        append_document(Locale::Document, canon_doc, locale)
      end
    end
  end
end

#resetObject



28
29
30
31
32
33
# File 'lib/jekyll/locale/handler.rb', line 28

def reset
  @locale_data  = {}
  @locale_dates = {}
  @portfolio = nil
  @filtered_portfolio = nil
end

#sanitized_locale(locale_key) ⇒ Object



110
111
112
# File 'lib/jekyll/locale/handler.rb', line 110

def sanitized_locale(locale_key)
  @sanitized_locale[locale_key] ||= locale_key.downcase.tr("-", "_")
end

#setupObject



125
126
127
128
129
130
# File 'lib/jekyll/locale/handler.rb', line 125

def setup
  @date_handler = Locale::DateTimeHandler
  @date_handler.bootstrap(self)
  @locale_data = setup_data if @locale_data.empty?
  nil
end

#user_localesObject



90
91
92
93
94
95
96
# File 'lib/jekyll/locale/handler.rb', line 90

def user_locales
  @user_locales ||= begin
    locales = Array(config["locales_set"]) - [default_locale]
    locales.compact!
    locales
  end
end