Sha256: 83aec38a6db8207c04460c30b362ac96a4cea6058d84ae41b1799d362160ab07

Contents?: true

Size: 1.52 KB

Versions: 9

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

require "set"
require "singleton"

module Coverband
  module Collectors
    module I18n
      module KeyRegistry
        def lookup(locale, key, scope = [], options = {})
          separator = options[:separator] || ::I18n.default_separator
          flat_key = ::I18n.normalize_keys(locale, key, scope, separator).join(separator)
          Coverband.configuration.translations_tracker.track_key(flat_key)

          super
        end
      end
    end

    ###
    # This class tracks translation usage via I18n::Backend
    ###
    class TranslationTracker < AbstractTracker
      REPORT_ROUTE = "translations_tracker"
      TITLE = "Translations"

      def railtie!
        # plugin to i18n
        ::I18n::Backend::Simple.send :include, ::Coverband::Collectors::I18n::KeyRegistry
      end

      private

      def concrete_target
        if defined?(Rails.application)
          app_translation_keys = []
          app_translation_files = ::I18n.load_path.select { |f| f.match(/config\/locales/) }
          app_translation_files.each do |file|
            app_translation_keys += flatten_hash(YAML.load_file(file)).keys
          end
          app_translation_keys.uniq
        else
          []
        end
      end

      def flatten_hash(hash)
        hash.each_with_object({}) do |(k, v), h|
          if v.is_a? Hash
            flatten_hash(v).map do |h_k, h_v|
              h["#{k}.#{h_k}".to_sym] = h_v
            end
          else
            h[k] = v
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
coverband-6.0.2 lib/coverband/collectors/translation_tracker.rb
coverband-6.0.1 lib/coverband/collectors/translation_tracker.rb
coverband-6.0.1.rc.1 lib/coverband/collectors/translation_tracker.rb
coverband-6.0.0 lib/coverband/collectors/translation_tracker.rb
coverband-5.2.6.rc.6 lib/coverband/collectors/translation_tracker.rb
coverband-5.2.6.rc.5 lib/coverband/collectors/translation_tracker.rb
coverband-5.2.6.rc.4 lib/coverband/collectors/translation_tracker.rb
coverband-5.2.6.rc.3 lib/coverband/collectors/translation_tracker.rb
coverband-5.2.6.rc.2 lib/coverband/collectors/translation_tracker.rb