Skip to content

Instantly share code, notes, and snippets.

@simonc
Created August 31, 2021 13:53
Show Gist options
  • Save simonc/f393bba83a992635fb238e62dd84bd0d to your computer and use it in GitHub Desktop.
Save simonc/f393bba83a992635fb238e62dd84bd0d to your computer and use it in GitHub Desktop.
module Jekyll
module LocalizedSite
def process
baseurl_root = config['baseurl']
output_dir = dest
available_locales = config['available_locales']
default_locale = config['default_locale']
config['baseurl_root'] = baseurl_root
config['translations'] = {}
available_locales.each do |locale|
Jekyll.logger.info "Loading:", "_i18n/#{locale}.yml"
config['translations'][locale] = YAML.load_file("#{source}/_i18n/#{locale}.yml")
end
available_locales.each do |locale|
config['locale'] = locale
if locale == default_locale
@dest = output_dir
config['baseurl_root'] = baseurl_root
config['exclude'].delete('assets')
else
@dest = "#{output_dir}/#{locale}"
config['baseurl_root'] = "#{baseurl_root}/#{locale}"
config['exclude'] << 'assets'
end
Jekyll.logger.info "Building #{locale.upcase} site:", dest
super
end
config['baseurl'] = baseurl_root
@dest = output_dir
Jekyll.logger.info 'Build complete'
end
def each_site_file(&block)
(pages + docs_to_write)
.select { |page| page_for_current_locale?(page) }
.each(&block)
static_files.each(&block)
end
private def render_docs(payload)
collections.each_value do |collection|
collection
.docs
.select { |page| page_for_current_locale?(page) }
.each { |page| render_regenerated(page, payload) }
end
end
private def render_pages(payload)
pages
.flatten
.select { |page| page_for_current_locale?(page) }
.each { |page| render_regenerated(page, payload) }
end
private def page_for_current_locale?(page)
page.data['locale'] == config['locale'] || !page.data.key?('locale')
end
end
class Site
prepend LocalizedSite
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment