Sha256: b7f44ab1dd943f5d6d65ad188aa22998cb45734a5b99742e14a780e2d52ac1df

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

require 'yaml'

module I18n
  module Coverage
    class KeyLister
      def self.list_keys(locale: 'en', locale_dir_path: 'config/locales')
        KeyLister.new(locale, locale_dir_path).list_keys
      end
  
      def initialize(locale, locale_dir_path)
        @locale = locale
        @source = YAML.load(File.open(File.expand_path("#{locale_dir_path}/#{locale}.yml")))
        @keys = Set[]
      end
  
      def list_keys
        visit_childs(path: [])
        @keys
      end
  
      private
  
      def visit_childs(path: )
        node = @source.dig(*[@locale, path].flatten.compact)
        if node.respond_to? :keys
          keys = node.keys

          if pluralization_keys?(keys)
            @keys.add(path.join('.'))
          else
            keys.map {|key| visit_childs(path: [path, key].flatten.compact)}
          end
        else
          @keys.add(path.join('.'))
        end
      end

      def pluralization_keys?(keys)
        return (keys - ['zero', 'one', 'other']).empty?
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
i18n-coverage-0.1.1 lib/i18n/coverage/key_lister.rb
i18n-coverage-0.1.0 lib/i18n/coverage/key_lister.rb