class Jekyll::Locale::Handler

Constants

DEFAULT_CONFIG

Attributes

config[R]
current_locale[W]
locale_data[R]
locale_dates[R]
site[R]

Public Class Methods

new(site) click to toggle source
# 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

Public Instance Methods

append_document(klass, canon_doc, locale) click to toggle source
# 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) click to toggle source
# 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_locales() click to toggle source
# File lib/jekyll/locale/handler.rb, line 98
def available_locales
  @available_locales ||= user_locales + [default_locale]
end
content_dirname() click to toggle source
# File lib/jekyll/locale/handler.rb, line 114
def content_dirname
  @content_dirname ||= fetch("content_dir")
end
current_locale() click to toggle source
# File lib/jekyll/locale/handler.rb, line 102
def current_locale
  @current_locale ||= default_locale
end
data() click to toggle source
# File lib/jekyll/locale/handler.rb, line 35
def data
  locale_data[sanitized_locale(current_locale)] ||
    locale_data[sanitized_locale(default_locale)] || {}
end
default_locale() click to toggle source
# File lib/jekyll/locale/handler.rb, line 106
def default_locale
  @default_locale ||= fetch("locale")
end
filtered_portfolio() click to toggle source
# 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
inspect() click to toggle source
# File lib/jekyll/locale/handler.rb, line 132
def inspect
  "#<#{self.class} @site=#{site}>"
end
mode() click to toggle source
# File lib/jekyll/locale/handler.rb, line 118
def mode
  @mode ||= begin
    value = config["mode"]
    value == "auto" ? value : DEFAULT_CONFIG["mode"]
  end
end
portfolio() click to toggle source
# File lib/jekyll/locale/handler.rb, line 40
def portfolio
  @portfolio ||= (site.docs_to_write + html_pages)
end
read() click to toggle source
# 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
reset() click to toggle source
# File lib/jekyll/locale/handler.rb, line 28
def reset
  @locale_data  = {}
  @locale_dates = {}
  @portfolio = nil
  @filtered_portfolio = nil
end
sanitized_locale(locale_key) click to toggle source
# File lib/jekyll/locale/handler.rb, line 110
def sanitized_locale(locale_key)
  @sanitized_locale[locale_key] ||= locale_key.downcase.tr("-", "_")
end
setup() click to toggle source
# 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_locales() click to toggle source
# 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

Private Instance Methods

exclusion_regex() click to toggle source
# File lib/jekyll/locale/handler.rb, line 182
def exclusion_regex
  @exclusion_regex ||= Regexp.new("\\A(?:#{Regexp.union(Array(config["exclude_set"]))})")
end
fetch(key) click to toggle source
# File lib/jekyll/locale/handler.rb, line 173
def fetch(key)
  value   = config[key]
  default = DEFAULT_CONFIG[key]
  return default unless value.class == default.class
  return default if value.to_s.empty?

  value
end
html_pages() click to toggle source
# File lib/jekyll/locale/handler.rb, line 140
def html_pages
  pages = site.site_payload["site"]["html_pages"] || []
  pages.reject { |page| page.name == "404.html" }
end
locales_dir() click to toggle source
# File lib/jekyll/locale/handler.rb, line 145
def locales_dir
  @locales_dir ||= fetch("data_dir")
end
setup_data() click to toggle source
# File lib/jekyll/locale/handler.rb, line 149
def setup_data
  ldata  = site.site_data[locales_dir]
  result = {}
  return result unless ldata.is_a?(Hash)

  ldata.each do |loc, loc_data|
    locale = Utils.snakeify(loc)
    result[locale] = {}
    next unless loc_data.is_a?(Hash)

    date_data = @date_handler::DATETIME_DEFAULTS
    loc_data.each do |key, value|
      if key == "locale_date"
        date_data = Utils.recursive_symbolize_hash_keys(value) if value.is_a?(Hash)
      elsif value.is_a?(String)
        result[locale][Utils.snakeify(key)] = value
      end
    end
    @locale_dates[loc] = date_data
  end

  result
end