class Jekyll::Site
Jekyll
multilingual support
-
Posts are put under _LANGUAGE/ directories
-
The first language on the config is default
-
Translation strings are store in _data/LANGUAGE.yml
-
The plugin generates a copy of the site for every language at _site/LANGUAGE
Attributes
Public Instance Methods
# File lib/jekyll/locales/site.rb, line 57 def default_locale @default_locale ||= locales.first end
# File lib/jekyll/locales/site.rb, line 61 def default_locale? default_locale == locale end
Get locales from configuration
# File lib/jekyll/locales/site.rb, line 53 def locales @locales ||= config.fetch('locales', []) end
# File lib/jekyll/locales/site.rb, line 48 def locales? locales.length > 1 end
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
Private Instance Methods
# 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
# File lib/jekyll/locales/site.rb, line 165 def default_delete FileUtils.rm_f default_destination end
# File lib/jekyll/locales/site.rb, line 169 def default_destination @default_destination ||= File.join(root_destination, 'default') end
# File lib/jekyll/locales/site.rb, line 173 def default_symlink FileUtils.ln_s(default_locale, default_destination) end
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
# 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
Add locale to the site destination
# File lib/jekyll/locales/site.rb, line 125 def locale_destination File.join(root_destination, locale) end
# File lib/jekyll/locales/site.rb, line 120 def orig_baseurl @orig_baseurl ||= config.dig('baseurl') || '' end
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
# File lib/jekyll/locales/site.rb, line 142 def redirect? config.fetch('redirect', false) end
TODO: Make configurable
# File lib/jekyll/locales/site.rb, line 147 def redirector @redirector ||= File.join(locale_destination, 'redirector.html') end
# File lib/jekyll/locales/site.rb, line 151 def redirector_to_index(redir) File.join(root_destination, File.basename(redir .sub('redirector', 'index'))) end
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
Cache original destination
# File lib/jekyll/locales/site.rb, line 116 def root_destination @root_destination ||= config['destination'] end
# File lib/jekyll/locales/site.rb, line 177 def self_symlink FileUtils.rm_f(locale_destination) FileUtils.ln_s('.', locale_destination) end
Creates a symlink to the _posts/ directory so Jekyll
believes it's processing a single site.
# File lib/jekyll/locales/site.rb, line 137 def symlink FileUtils.rm_f('_posts') FileUtils.ln_s("_#{locale}", '_posts') end