Sha256: fccb0503328ce7fb39ebf2e09889d62e6dc586b8af61efa2a7729862d3fce6d8

Contents?: true

Size: 981 Bytes

Versions: 9

Compression:

Stored size: 981 Bytes

Contents

# frozen_string_literal: true

require 'yaml'

module Liquid
  class I18n
    DEFAULT_LOCALE = File.join(File.expand_path(__dir__), "locales", "en.yml")

    TranslationError = Class.new(StandardError)

    attr_reader :path

    def initialize(path = DEFAULT_LOCALE)
      @path = path
    end

    def translate(name, vars = {})
      interpolate(deep_fetch_translation(name), vars)
    end
    alias_method :t, :translate

    def locale
      @locale ||= YAML.load_file(@path)
    end

    private

    def interpolate(name, vars)
      name.gsub(/%\{(\w+)\}/) do
        # raise TranslationError, "Undefined key #{$1} for interpolation in translation #{name}"  unless vars[$1.to_sym]
        (vars[Regexp.last_match(1).to_sym]).to_s
      end
    end

    def deep_fetch_translation(name)
      name.split('.').reduce(locale) do |level, cur|
        level[cur] || raise(TranslationError, "Translation for #{name} does not exist in locale #{path}")
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
liquid-5.6.0.rc1 lib/liquid/i18n.rb
liquid-5.5.1 lib/liquid/i18n.rb
liquid-5.5.0 lib/liquid/i18n.rb
liquid-5.4.0 lib/liquid/i18n.rb
liquid-5.3.0 lib/liquid/i18n.rb
liquid-5.2.0 lib/liquid/i18n.rb
liquid-5.1.0 lib/liquid/i18n.rb
liquid-5.0.1 lib/liquid/i18n.rb
liquid-5.0.0 lib/liquid/i18n.rb