Sha256: 95705dc875c06288ce2f4758afe1edec9d71398ef2b2d715479b6d40b573b33b

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

module I18nSpec
  class LocaleFile
    PLURALIZATION_KEYS = %w{zero one two few many other}

    def initialize(filepath)
      @filepath = filepath
    end

    def is_parseable?
      begin
        Psych.load_file(@filepath)
        true
      rescue Psych::SyntaxError => e
        false
      end
    end

    def has_valid_pluralization_keys?
      pluralizations.each do |key, pluralization|
        return false unless pluralization.keys.all? {|k| PLURALIZATION_KEYS.include?(k)}
      end
      true
    end

  protected

    def translations
      @translations ||= Psych.load_file(@filepath)
    end

    def flatten_tree(data, prefix = '', result = {})
      data.each do |key, value|
        current_prefix = prefix.empty? ? key.to_s : "#{prefix}.#{key}"
        if !value.is_a?(Hash) || pluralization_data?(value)
          result[current_prefix] = value
        else
          flatten_tree(value, current_prefix, result)
        end
      end
      result
    end

    def pluralization_data?(data)
      keys = data.keys.map(&:to_s)
      keys.any? {|k| PLURALIZATION_KEYS.include?(k) }
    end

    def pluralizations
      flatten_tree(translations).select do |key, value|
        value.is_a?(Hash)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
i18n-spec-0.0.3 lib/i18n-spec/models/locale_file.rb
i18n-spec-0.0.2 lib/i18n-spec/models/locale_file.rb
i18n-spec-0.0.1 lib/i18n-spec/models/locale_file.rb