Class: Jekyll::LanguageSwitcherTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll-contentful/language_switcher_tag.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, text, tokens) ⇒ LanguageSwitcherTag

Returns a new instance of LanguageSwitcherTag.



4
5
6
7
# File 'lib/jekyll-contentful/language_switcher_tag.rb', line 4

def initialize(tag_name, text, tokens)
  super
  @text = text
end

Instance Method Details

#anchor(page) ⇒ Object



28
29
30
31
# File 'lib/jekyll-contentful/language_switcher_tag.rb', line 28

def anchor(page)
  lang = @site.config['contentful']['localization'].detect{ |loc| loc["locale"] == page['contentful_locale']}["lang"]
  "<a class='translation-link lang-#{page['contentful_locale']}' href='#{ page['url']}'>#{ lang }</a>"
end

#render(context) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jekyll-contentful/language_switcher_tag.rb', line 9

def render(context)
  @site = context.registers[:site]
  this_page = context.registers[:page]
  
  return "" if @site.config['contentful']['localization'].nil? || this_page["contentful_id"].nil?

  translated_pages = @site.pages.select do |that_page|
    that_page["contentful_id"] == this_page["contentful_id"] and that_page["contentful_locale"] != this_page["contentful_locale"]
  end
  if translated_pages.length > 1
    list = translated_pages.dup.map do |tp|
      "<li translation-item>#{anchor(tp)}</li>"
    end.join()
    return "<ul class='translation-list'>#{list}</uL>"
  elsif translated_pages.length == 1
    return anchor(translated_pages[0])
  end
end