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



418
419
420
421
# File 'lib/jekyll-multiple-languages-plugin.rb', line 418

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

Instance Method Details

#render(context) ⇒ Object

render



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/jekyll-multiple-languages-plugin.rb', line 428

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
    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