Sha256: 41bdf7ed884734c1966a0665f4e85fe8d8c8d5e4b91af1370664ff5a329673f0

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

require 'yaml'

module I18n
  module Coverage
    class KeyLister
      def self.list_keys
        KeyLister.new.list_keys
      end

      def initialize
        locale_dir_path = I18n::Coverage.config.locale_dir_path
        @locale_files = Dir.glob("#{locale_dir_path}/**/*.yml")
        @keys = Set[]
      end

      def list_keys
        @locale_files
          .map(&YAML.method(:load_file))
          .flat_map { |hash| visit_childs(source: hash, path: []) }

        @keys
      end

      private

      def visit_childs(source:, path:)
        node = source.dig(locale, *path)

        if node.respond_to? :keys
          keys = node.keys

          if pluralization_keys?(keys)
            @keys.add(path.join('.'))
          else
            keys.map { |key| visit_childs(source: source, path: path + [key]) }
          end
        elsif path.count.positive?
          @keys.add(path.join('.'))
        end
      end

      def pluralization_keys?(keys)
        (keys - %w[zero one other]).empty?
      end

      def locale
        I18n::Coverage.config.locale
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
i18n-coverage-0.2.0 lib/i18n/coverage/key_lister.rb