Class: Locomotive::Liquid::Tags::LocaleSwitcher

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/locomotive/liquid/tags/locale_switcher.rb

Overview

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

Constant Summary collapse

Syntax =
/(#{::Liquid::Expression}+)?/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens, context) ⇒ LocaleSwitcher

Returns a new instance of LocaleSwitcher.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/locomotive/liquid/tags/locale_switcher.rb', line 24

def initialize(tag_name, markup, tokens, context)
  @options = { :label => 'iso', :sep => ' | ' }

  if markup =~ Syntax
    markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') }

    @options[:exclude] = Regexp.new(@options[:exclude]) if @options[:exclude]
  else
    raise ::Liquid::SyntaxError.new("Syntax Error in 'locale_switcher' - Valid syntax: locale_switcher <options>")
  end

  super
end

Instance Method Details

#render(context) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/locomotive/liquid/tags/locale_switcher.rb', line 38

def render(context)
  @site, @page = context.registers[:site], context.registers[:page]

  output = %(<div id="locale-switcher">)

  output += @site.locales.collect do |locale|
    ::Mongoid::Fields::I18n.with_locale(locale) do
      fullpath = @site.localized_page_fullpath(@page, locale)

      if @page.templatized?
        fullpath.gsub!('content_type_template', context['entry']._permalink)
      end

      css = link_class(locale, context['locale'])

      %(<a href="/#{fullpath}" class="#{css}">#{link_label(locale)}</a>)
    end
  end.join(@options[:sep])

  output += %(</div>)
end