class Jekyll::Site

Jekyll multilingual support

Attributes

locale[R]

Public Instance Methods

default_locale() click to toggle source
# File lib/jekyll/locales/site.rb, line 57
def default_locale
  @default_locale ||= locales.first
end
default_locale?() click to toggle source
# File lib/jekyll/locales/site.rb, line 61
def default_locale?
  default_locale == locale
end
locales() click to toggle source

Get locales from configuration

# File lib/jekyll/locales/site.rb, line 53
def locales
  @locales ||= config.fetch('locales', [])
end
locales?() click to toggle source
# File lib/jekyll/locales/site.rb, line 48
def locales?
  locales.length > 1
end
process() click to toggle source

Process the site once per available locale

# File lib/jekyll/locales/site.rb, line 18
def process
  locales.each do |locale|
    @locale = locale

    Jekyll.logger.info 'Generating locale:', locale

    locale_config

    Jekyll.logger.info 'Destination:', config['destination']
    Jekyll.logger.info 'Base URL:', config['baseurl']

    symlink
    reset
    setup
    process_single
    # The default locale is placed at the root of the
    # site and everything else is under a subdirectory, but we symlink
    # root to default_locale for compatibility
    self_symlink if default_locale? && !redirect?
  end

  # XXX: Default is expected by Sutty so we always create it
  default_delete
  default_symlink

  # If we enable the redirector, the root of the site will consist
  # of an index.html that allows visitors to select their locale.
  copy_redirector if redirect?
end
Also aliased as: process_single
process_single()

We don't monkey patch because the Site is already instantiated

Alias for: process

Private Instance Methods

copy_redirector() click to toggle source
# File lib/jekyll/locales/site.rb, line 157
def copy_redirector
  return unless File.exist? redirector

  Dir.glob("#{redirector}*").each do |redir|
    FileUtils.cp(redir, redirector_to_index(redir))
  end
end
default_delete() click to toggle source
# File lib/jekyll/locales/site.rb, line 165
def default_delete
  FileUtils.rm_f default_destination
end
default_destination() click to toggle source
# File lib/jekyll/locales/site.rb, line 169
def default_destination
  @default_destination ||= File.join(root_destination, 'default')
end
locale_baseurl() click to toggle source

If the site has a baseurl, we add the locale to it. But we add it anyway so relative URLs include the locale

# File lib/jekyll/locales/site.rb, line 131
def locale_baseurl
  [orig_baseurl, locale].join('/')
end
locale_config() click to toggle source
# File lib/jekyll/locales/site.rb, line 82
def locale_config
  # XXX: Retrocompatibility
  config['locale'] = config['lang'] = locale
  config['default_locale'] = default_locale

  # Don't touch the config unless there's more than one locale or we
  # aren't using a redirector
  return unless locales?

  other_locales_as_collections
  return if default_locale? && !redirect?

  @dest = config['destination'] = locale_destination
  config['baseurl'] = locale_baseurl
end
locale_destination() click to toggle source

Add locale to the site destination

# File lib/jekyll/locales/site.rb, line 125
def locale_destination
  File.join(root_destination, locale)
end
orig_baseurl() click to toggle source
# File lib/jekyll/locales/site.rb, line 120
def orig_baseurl
  @orig_baseurl ||= config.dig('baseurl') || ''
end
other_locales_as_collections() click to toggle source

Read the other locales as collections but don't render them, only make them available to templates.

The URL will be the permalink with the language prepended.

{rubygems.org/gems/jekyll-linked-posts}

# File lib/jekyll/locales/site.rb, line 104
def other_locales_as_collections
  locales.each do |l|
    next if l == locale

    config['collections'][l] = {
      'output' => false,
      'permalink' => [nil, l, config['permalink']].join('/')
    }
  end
end
redirect?() click to toggle source
# File lib/jekyll/locales/site.rb, line 142
def redirect?
  config.fetch('redirect', false)
end
redirector() click to toggle source

TODO: Make configurable

# File lib/jekyll/locales/site.rb, line 147
def redirector
  @redirector ||= File.join(locale_destination, 'redirector.html')
end
redirector_to_index(redir) click to toggle source
# File lib/jekyll/locales/site.rb, line 151
def redirector_to_index(redir)
  File.join(root_destination,
            File.basename(redir
                .sub('redirector', 'index')))
end
render_docs(payload) click to toggle source

Redefine render_docs so it doesn't render the locale collections for each independent locale.

XXX: Jekyll should render on demand instead of rendering and writing separately.

# File lib/jekyll/locales/site.rb, line 72
def render_docs(payload)
  collections.each do |name, collection|
    next if locales.include? name

    collection.docs.each do |document|
      render_regenerated(document, payload)
    end
  end
end
root_destination() click to toggle source

Cache original destination

# File lib/jekyll/locales/site.rb, line 116
def root_destination
  @root_destination ||= config['destination']
end