Class: BetterTranslate::Analyzer::OrphanDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/better_translate/analyzer/orphan_detector.rb

Overview

Detects orphan i18n keys (keys defined but never used in code)

Examples:

Basic usage

all_keys = { "users.greeting" => "Hello", "orphan" => "Unused" }
used_keys = Set.new(["users.greeting"])

detector = OrphanDetector.new(all_keys, used_keys)
orphans = detector.detect
#=> ["orphan"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(all_keys, used_keys) ⇒ OrphanDetector

Initialize detector

Parameters:

  • all_keys (Hash)

    All translation keys from YAML files

  • used_keys (Set)

    Keys found in code



30
31
32
33
34
# File 'lib/better_translate/analyzer/orphan_detector.rb', line 30

def initialize(all_keys, used_keys)
  @all_keys = all_keys
  @used_keys = used_keys
  @orphans = []
end

Instance Attribute Details

#all_keysHash (readonly)

Returns All translation keys with their values.

Returns:

  • (Hash)

    All translation keys with their values



17
18
19
# File 'lib/better_translate/analyzer/orphan_detector.rb', line 17

def all_keys
  @all_keys
end

#orphansArray<String> (readonly)

Returns Orphan keys (not used in code).

Returns:

  • (Array<String>)

    Orphan keys (not used in code)



23
24
25
# File 'lib/better_translate/analyzer/orphan_detector.rb', line 23

def orphans
  @orphans
end

#used_keysSet (readonly)

Returns Keys that are used in code.

Returns:

  • (Set)

    Keys that are used in code



20
21
22
# File 'lib/better_translate/analyzer/orphan_detector.rb', line 20

def used_keys
  @used_keys
end

Instance Method Details

#detectArray<String>

Detect orphan keys

Compares all keys with used keys and identifies those that are never referenced.

Examples:

detector.detect
#=> ["orphan_key", "another.orphan"]

Returns:

  • (Array<String>)

    List of orphan key names



46
47
48
# File 'lib/better_translate/analyzer/orphan_detector.rb', line 46

def detect
  @orphans = all_keys.keys.reject { |key| used_keys.include?(key) }
end

#orphan_countInteger

Get count of orphan keys

Returns:

  • (Integer)

    Number of orphan keys



54
55
56
# File 'lib/better_translate/analyzer/orphan_detector.rb', line 54

def orphan_count
  @orphans.size
end

#orphan_detailsHash

Get details of orphan keys with their values

Examples:

detector.orphan_details
#=> { "orphan_key" => "This is never used" }

Returns:

  • (Hash)

    Hash of orphan keys and their translation values



66
67
68
69
70
71
72
73
# File 'lib/better_translate/analyzer/orphan_detector.rb', line 66

def orphan_details
  # @type var result: Hash[String, untyped]
  result = {}
  @orphans.each do |key|
    result[key] = all_keys[key]
  end
  result
end

#usage_percentageFloat

Calculate usage percentage

Examples:

detector.usage_percentage
#=> 75.0  # 6 out of 8 keys are used

Returns:

  • (Float)

    Percentage of keys that are used (0.0 to 100.0)



83
84
85
86
87
88
# File 'lib/better_translate/analyzer/orphan_detector.rb', line 83

def usage_percentage
  return 0.0 if all_keys.empty?

  used_count = all_keys.size - @orphans.size
  (used_count.to_f / all_keys.size * 100).round(1).to_f
end