Class: Jekyll::LocalizeTag

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

Overview

class LocalizeTag

Localization by getting localized text from YAML files. User must use the “t” or “translate” liquid tags.

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, key, tokens) ⇒ LocalizeTag

initialize



337
338
339
340
# File 'lib/jekyll-multiple-languages-plugin.rb', line 337

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

Instance Method Details

#render(context) ⇒ Object

render



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/jekyll-multiple-languages-plugin.rb', line 347

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
  
  lang = site.config['lang']
  
  translation = site.parsed_translations[lang].access(key) if key.is_a?(String)
  
  if translation.nil? or translation.empty?
     translation = site.parsed_translations[site.config['default_lang']].access(key)
    
    if site.config["verbose"]
      puts "Missing i18n key: #{lang}:#{key}"
      puts "Using translation '%s' from default language: %s" %[translation, site.config['default_lang']]
    end
  end
  
  translation
end