class Locomotive::Steam::Liquid::Tags::LocaleSwitcher
Display the links to change the locale of the current page
Usage:
{% locale_switcher %} => <div id=“locale-switcher”><a href=“/features” class=“current en”>Features</a><a href=“/fr/fonctionnalites” class=“fr”>Fonctionnalités</a></div>
{% locale_switcher label: locale, sep: ' - ' }
options:
- label: iso (de, fr, en, ...etc), locale (Deutsch, Français, English, ...etc), title (page title) - sep: piece of html code separating 2 locales
notes:
- "iso" is the default choice for label - " | " is the default separating code
Attributes
attributes[R]
current_locale[R]
page[R]
site[R]
url_builder[R]
Public Class Methods
new(tag_name, markup, options)
click to toggle source
Calls superclass method
# File lib/locomotive/steam/liquid/tags/locale_switcher.rb, line 30 def initialize(tag_name, markup, options) super parse_attributes(markup, label: 'iso', sep: ' | ') end
Public Instance Methods
render(context)
click to toggle source
# File lib/locomotive/steam/liquid/tags/locale_switcher.rb, line 36 def render(context) evaluate_attributes(context) set_vars_from_context(context) %{<div id="locale-switcher">#{build_site_locales}</div>} end
Private Instance Methods
build_site_locales()
click to toggle source
# File lib/locomotive/steam/liquid/tags/locale_switcher.rb, line 46 def build_site_locales site.locales.map do |locale| change_page_locale(locale, page) do css = link_class(locale) path = link_path(locale) %(<a href="#{path}" class="#{css}">#{link_label(locale)}</a>) end end.join(attributes[:sep]) end
link_class(locale)
click to toggle source
# File lib/locomotive/steam/liquid/tags/locale_switcher.rb, line 57 def link_class(locale) css = [locale] css << 'current' if locale.to_sym == current_locale css.join(' ') end
link_label(locale)
click to toggle source
# File lib/locomotive/steam/liquid/tags/locale_switcher.rb, line 67 def link_label(locale) case attributes[:label] when 'locale' then I18n.t("locomotive.locales.#{locale}") when 'title' then page_title else locale end end
link_path(locale)
click to toggle source
# File lib/locomotive/steam/liquid/tags/locale_switcher.rb, line 63 def link_path(locale) url_builder.url_for(page.send(:_source), locale) end
page_title()
click to toggle source
# File lib/locomotive/steam/liquid/tags/locale_switcher.rb, line 76 def page_title if page.templatized? page.send(:_source).content_entry._label else page.title end end
set_vars_from_context(context)
click to toggle source
# File lib/locomotive/steam/liquid/tags/locale_switcher.rb, line 84 def set_vars_from_context(context) @context = context @site = context.registers[:site] @page = context['page'] @current_locale = context.registers[:locale].to_sym @url_builder = context.registers[:services].url_builder end