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



477
478
479
480
# File 'lib/jekyll-multiple-languages-plugin.rb', line 477

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

Instance Method Details

#render(context) ⇒ Object

render



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# 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