Sha256: e683fe2578121247bd24c77de0c0d345dc5b304034fb46cfcb358e9d6616d826

Contents?: true

Size: 1.53 KB

Versions: 10

Compression:

Stored size: 1.53 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, aliases: true)).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}"] = h_v
            end
          else
            h[k] = v
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
coverband-6.1.4 lib/coverband/collectors/translation_tracker.rb
coverband-6.1.3 lib/coverband/collectors/translation_tracker.rb
coverband-6.1.2 lib/coverband/collectors/translation_tracker.rb
coverband-6.1.2.rc.1 lib/coverband/collectors/translation_tracker.rb
coverband-6.1.1 lib/coverband/collectors/translation_tracker.rb
coverband-6.1.0 lib/coverband/collectors/translation_tracker.rb
coverband-6.0.3.rc.4 lib/coverband/collectors/translation_tracker.rb
coverband-6.0.3.rc.3 lib/coverband/collectors/translation_tracker.rb
coverband-6.0.3.rc.2 lib/coverband/collectors/translation_tracker.rb
coverband-6.0.3.rc.1 lib/coverband/collectors/translation_tracker.rb