Skip to content

Custom Data Loaders

Vincent Wochnik edited this page Dec 15, 2015 · 1 revision

This plugin offers the possibility of adding a custom data loader which loads translations used by the plugin. To create a custom loader, create a new Ruby file within your site's _plugins directory, extend Jekyll::LanguagePlugin::Loaders::BaseLoader and register it so it can be instantiated by the plugin.

require 'jekyll-language-plugin'

module Jekyll
  module LanguagePlugin
    module Loaders
      class CustomDataLoader < BaseLoader
        attr_reader :data

        def initialize(site)
          super
          @data = Hash.new
        end

        def loaded?(language)
          loaded = @data.has_key?(language)
        end

        def load(language)
          return true if loaded?(language)

          [email protected]!({
            language => {
              'test' => "This is a test string in #{language}."
            }
          })
        end

        def get(key, language)
          return nil unless loaded?(language)

          traverse_hash(@data, resolve_dot_notation([language, key]))
        end
      end
    end
  end
end

Jekyll::LanguagePlugin.register_loader(Jekyll::LanguagePlugin::Loaders::CustomDataLoader)

This custom loader makes a test translation available to each language it is being used with.