Sha256: f4d42924a0d78c9bc9c8c44b2400f2a259e8867ae08aeacd07d50a1999428fea

Contents?: true

Size: 1010 Bytes

Versions: 6

Compression:

Stored size: 1010 Bytes

Contents

require 'liquid'

module Jekyll
  module Language
    
    @@lang = nil
    
    # Supplies translated text
    #
    # Usage: {{ 'author' | t: 'recent_articles' }}
    def t(section, item)
      site = @context.registers[:site]

      if @@lang == nil
        # Access this fewer times by keeping it as a module variable
        @@lang = Jekyll.configuration({})['language']
      end
      
      # Find text in the site language (for example 'fr-be')
      text = site.data['language'][section][item][@@lang]

      # Fall back to a more general version of the language (for example 'fr')
      if text == nil and @@lang.include? '-'
        fallback_lang = @@lang.split('-')[0];
        text = site.data['language'][section][item][fallback_lang]
      end

      if text == nil
        # Fallback to English text
        text = site.data['language'][section][item]['en']
      end

      return text
    end
  end
end

Liquid::Template.register_filter(Jekyll::Language)

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
fenton-jekyll-plugin-0.0.2 lib/fenton-jekyll-plugin/jb_liquid_language.rb
fenton-jekyll-plugin-0.0.1 lib/fenton-jekyll-plugin/jb_liquid_language.rb
fenton-jekyll-boilerplate-0.0.4 _plugins/liquid_language.rb
fenton-jekyll-boilerplate-0.0.3 _plugins/liquid_language.rb
fenton-jekyll-boilerplate-0.0.2 _plugins/liquid_language.rb
fenton-jekyll-boilerplate-0.0.1 _plugins/liquid_language.rb