Class: Jekyll::LocalizeLink

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll-multiple-languages-plugin.rb

Overview

class LocalizeLink

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

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, key, tokens) ⇒ LocalizeLink

initialize



514
515
516
517
# File 'lib/jekyll-multiple-languages-plugin.rb', line 514

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

Instance Method Details

#render(context) ⇒ Object

render



524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/jekyll-multiple-languages-plugin.rb', line 524

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