Class: Octopress::Multilingual::Tags::SetLang

Inherits:
Liquid::Block
  • Object
show all
Defined in:
lib/octopress-multilingual/set_lang_tag.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ SetLang

Returns a new instance of SetLang.



5
6
7
8
# File 'lib/octopress-multilingual/set_lang_tag.rb', line 5

def initialize(tag_name, markup, tokens)
  super
  @input = markup.strip
end

Instance Method Details

#langObject



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/octopress-multilingual/set_lang_tag.rb', line 59

def lang

  # Read tag arguments as a string first, if that fails,
  # Look at the local context, to see if it is a variable
  #
  if lang = [@input, @context[@input]].select do |l| 
      @languages.include?(l)
    end.first

    lang.downcase
  end
end

#render(context) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/octopress-multilingual/set_lang_tag.rb', line 10

def render(context)
  @context         = context
  @languages       = @context['site.languages']

  # If a language is defined
  if lang && lang != @context['page.lang']

    store_state                    # Store current language and post arrays
    set_lang lang                  # Set to specified language
    content = super(context)       # Render
    restore_state                  # Restore language and post arrays

    content
  else
    # If the language argument resovles to nil
    # this will render contents normally
    # 
    super(context)
  end
end

#restore_stateObject



53
54
55
56
57
# File 'lib/octopress-multilingual/set_lang_tag.rb', line 53

def restore_state
  @context.environments.first['page']['lang'] = @current_lang
  @context.environments.first['site'] = @site
  @context.environments.first['lang'] = @lang
end

#set_lang(lang) ⇒ Object

Swap out site.posts, site.linkposts, and site.articles with arrays filtered by the selected language



34
35
36
37
38
39
# File 'lib/octopress-multilingual/set_lang_tag.rb', line 34

def set_lang(lang)
  @context.environments.first['page']['lang'] = lang
  payload = Multilingual.page_payload(lang)
  set_payload('site', payload)
  set_payload('lang', payload)
end

#set_payload(payload_key, payload) ⇒ Object



41
42
43
44
45
# File 'lib/octopress-multilingual/set_lang_tag.rb', line 41

def set_payload(payload_key, payload)
  payload[payload_key].each do |key,value| 
    @context.environments.first[payload_key][key] = value
  end
end

#store_stateObject



47
48
49
50
51
# File 'lib/octopress-multilingual/set_lang_tag.rb', line 47

def store_state
  @current_lang       = @context['page.lang']
  @site               = @context['site'].clone
  @lang               = @context['lang'].clone
end