Sha256: d16fc6155f93961ad75611f809985e6de170e8a3964c886db0690466e97d9e7c

Contents?: true

Size: 937 Bytes

Versions: 18

Compression:

Stored size: 937 Bytes

Contents

require 'yaml'

module Datacaster
  module SubstituteI18n
    @load_path = []

    def self.exists?(key)
      !fetch(key).nil?
    end

    def self.fetch(key)
      keys = [locale] + key.split('.')

      @translations.each do |hash|
        result = hash.dig(*keys)
        return result unless result.nil?
      end
      nil
    end

    def self.load_path
      @load_path
    end

    def self.load_path=(array)
      @load_path = array
      @translations = array.map { |x| YAML.load_file(x) }
    end

    def self.locale
      'en'
    end

    def self.locale=(*)
      raise NotImplementedError.new("Setting locale is not supported, use ruby-i18n instead of datacaster's built-in")
    end

    def self.t(key, **args)
      string = fetch(key)
      return "Translation missing #{key}" unless string

      args.each do |from, to|
        string = string.gsub("%{#{from}}", to.to_s)
      end
      string
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
datacaster-4.1.0 lib/datacaster/substitute_i18n.rb
datacaster-4.0.1 lib/datacaster/substitute_i18n.rb
datacaster-3.3.1 lib/datacaster/substitute_i18n.rb
datacaster-3.3.0 lib/datacaster/substitute_i18n.rb
datacaster-3.2.7 lib/datacaster/substitute_i18n.rb
datacaster-3.2.6 lib/datacaster/substitute_i18n.rb
datacaster-3.2.5 lib/datacaster/substitute_i18n.rb
datacaster-3.2.4 lib/datacaster/substitute_i18n.rb
datacaster-3.2.3 lib/datacaster/substitute_i18n.rb
datacaster-3.2.2 lib/datacaster/substitute_i18n.rb
datacaster-3.2.1 lib/datacaster/substitute_i18n.rb
datacaster-3.2.0 lib/datacaster/substitute_i18n.rb
datacaster-3.1.5 lib/datacaster/substitute_i18n.rb
datacaster-3.1.3 lib/datacaster/substitute_i18n.rb
datacaster-3.1.2 lib/datacaster/substitute_i18n.rb
datacaster-3.1.1 lib/datacaster/substitute_i18n.rb
datacaster-3.1.0 lib/datacaster/substitute_i18n.rb
datacaster-3.0.0 lib/datacaster/substitute_i18n.rb