# frozen_string_literal: true module Jekyll # 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 class Site # We don't monkey patch because the Site is already instantiated alias process_single process # Process the site once per available locale def process locales.each do |locale| Jekyll.logger.info 'Generating locale:', locale config_for locale Jekyll.logger.info 'Destination:', config['destination'] Jekyll.logger.info 'Base URL:', config['baseurl'] symlink_for locale process_single copy_redirector end default_delete default_symlink end private def config_for(locale) @dest = config['destination'] = destination_for(locale) config['baseurl'] = baseurl_for(locale) # XXX: Retrocompatibility config['locale'] = config['lang'] = locale config['default_locale'] = default_locale end # Get locales from configuration def locales @locales ||= config.fetch('locales', []) end def default_locale locales.first end # Cache original destination def root_destination @root_destination ||= config['destination'] end def orig_baseurl @orig_baseurl ||= config.dig('baseurl') || '' end # Add locale to the site destination def destination_for(locale) File.join(root_destination, locale) end # If the site has a baseurl, we add the locale to it. But we add # it anyway so relative URLs include the locale def baseurl_for(locale) [orig_baseurl, locale].join('/') end # Creates a symlink to the _posts/ directory so Jekyll believes # it's processing a single site. def symlink_for(locale) FileUtils.rm_f('_posts') FileUtils.ln_s("_#{locale}", '_posts') end # TODO: Make configurable def redirector @redirector ||= File.join(destination_for(default_locale), 'redirector.html') end def redirector_to_index(redir) File.join(root_destination, File.basename(redir .sub('redirector', 'index'))) end def copy_redirector return unless File.exist? redirector Dir.glob("#{redirector}*").each do |redir| FileUtils.cp(redir, redirector_to_index(redir)) end end def default_delete FileUtils.rm_f default_destination end def default_destination @default_destination ||= File.join(root_destination, 'default') end def default_symlink FileUtils.ln_s(default_locale, default_destination) end end end