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
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