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



372
373
374
375
# File 'lib/jekyll-multiple-languages-plugin.rb', line 372

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

Instance Method Details

#render(context) ⇒ Object

render



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/jekyll-multiple-languages-plugin.rb', line 382

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

  TranslatedString.translate(key, lang, site)
  
  translation
end