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



394
395
396
397
# File 'lib/jekyll-multiple-languages-plugin.rb', line 394

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

Instance Method Details

#render(context) ⇒ Object

render



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/jekyll-multiple-languages-plugin.rb', line 404

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