Sha256: 64e7f32961615660b8761e5bb6d6f30d693c6788e61fcc6634c40a9ed06e31e1

Contents?: true

Size: 830 Bytes

Versions: 18

Compression:

Stored size: 830 Bytes

Contents

module Bisu
  class Dictionary
    def initialize(keys)
      unless keys.is_a?(Hash)
        raise ArgumentError.new("keys: expected Hash, got #{keys.class}")
      end

      keys.each do |lang,v|
        unless v.is_a?(Hash)
          raise ArgumentError.new("keys['#{lang}']: expected Hash, got #{v.class}")
        end

        v.each do |key,v|
          unless v.is_a?(String) || v.nil?
            raise ArgumentError.new("keys['#{lang}']['#{key}']: expected String, got #{v.class}")
          end
        end
      end

      @keys = Hash[keys.map { |k,v| [k.downcase, v] }]
    end

    def has_language?(language)
      @keys.include?(language&.downcase)
    end

    def localize(language, key)
      if has_language?(language)
        @keys[language&.downcase][key]
      else
        nil
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
bisu-2.3.0 lib/bisu/dictionary.rb
bisu-2.2.0 lib/bisu/dictionary.rb
bisu-2.1.0 lib/bisu/dictionary.rb
bisu-2.0.0 lib/bisu/dictionary.rb
bisu-1.10.2 lib/bisu/dictionary.rb
bisu-1.10.1 lib/bisu/dictionary.rb
bisu-1.10.0 lib/bisu/dictionary.rb
bisu-1.9.0 lib/bisu/dictionary.rb
bisu-1.8.0 lib/bisu/dictionary.rb
bisu-1.7.3 lib/bisu/dictionary.rb
bisu-1.7.2 lib/bisu/dictionary.rb
bisu-1.7.1 lib/bisu/dictionary.rb
bisu-1.7.0 lib/bisu/dictionary.rb
bisu-1.6.0 lib/bisu/dictionary.rb
bisu-1.5.0 lib/bisu/dictionary.rb
bisu-1.4.7 lib/bisu/dictionary.rb
bisu-1.4.6 lib/bisu/dictionary.rb
bisu-1.4.5 lib/bisu/dictionary.rb