class Jekyll::LocalizeLink

class LocalizeLink

Creates links or permalinks for translated pages. User must use the “tl” or “translate_link” liquid tags.

Public Class Methods

new(tag_name, key, tokens) click to toggle source
initialize
Calls superclass method
# File lib/jekyll-multiple-languages-plugin.rb, line 477
def initialize(tag_name, key, tokens)
  super
  @key = key
end

Public Instance Methods

render(context) click to toggle source
render
# File lib/jekyll-multiple-languages-plugin.rb, line 487
def render(context)
  if      "#{context[@key]}" != "" # Check for page variable
    key = "#{context[@key]}"
  else
    key = @key
  end
  
  key = Liquid::Template.parse(key).render(context)  # Parses and renders some Liquid syntax on arguments (allows expansions)
  
  site = context.registers[:site] # Jekyll site object
  
  key          = key.split
  namespace    = key[0]
  lang         = key[1] || site.config[        'lang']
  default_lang =           site.config['default_lang']
  baseurl      =           site.baseurl
  pages        =           site.pages
  url          = "";
  
  if default_lang != lang || site.config['default_locale_in_subfolder']
    baseurl = baseurl + "/" + lang
  end
  
  collections = site.collections.values.collect{|x| x.docs}.flatten
  pages = site.pages + collections
  
  for p in pages
    unless             p['namespace'].nil?
      page_namespace = p['namespace']
      
      if namespace == page_namespace
        permalink = p['permalink_'+lang] || p['permalink']
        url       = baseurl + permalink
      end
    end
  end
  
  url
end