Class: I18nDocs::MissingKeysFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n_docs/missing_keys_finder.rb

Instance Method Summary collapse

Constructor Details

#initialize(backend) ⇒ MissingKeysFinder

Returns a new instance of MissingKeysFinder.



3
4
5
6
7
# File 'lib/i18n_docs/missing_keys_finder.rb', line 3

def initialize(backend)
  @backend = backend
  load_config
  load_translations
end

Instance Method Details

#all_keysObject

Returns an array with all keys from all locales



10
11
12
13
14
# File 'lib/i18n_docs/missing_keys_finder.rb', line 10

def all_keys
  I18n.backend.send(:translations).collect do |_check_locale, translations|
    collect_keys([], translations).sort
  end.flatten.uniq
end

#collect_keys(scope, translations) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/i18n_docs/missing_keys_finder.rb', line 71

def collect_keys(scope, translations)
  full_keys = []
  translations.to_a.each do |key, translation|
    new_scope = scope.dup << key
    if translation.is_a?(Hash)
      full_keys += collect_keys(new_scope, translation)
    else
      full_keys << new_scope.join('.')
    end
  end
  full_keys
end

#find_missing_keysObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/i18n_docs/missing_keys_finder.rb', line 16

def find_missing_keys
  output_available_locales
  output_unique_key_stats(all_keys)

  missing_keys = {}
  all_keys.each do |key|
    I18n.available_locales.each do |locale|
      skip = false
      ls = locale.to_s
      unless @yaml[ls].nil?
        @yaml[ls].each do |re|
          if key.match(re)
            skip = true
            break
          end
        end
      end

      if !key_exists?(key, locale) && skip == false
        if missing_keys[key]
          missing_keys[key] << locale
        else
          missing_keys[key] = [locale]
        end
      end
    end
  end

  output_missing_keys(missing_keys)
  missing_keys
end

#key_exists?(key, locale) ⇒ Boolean

Returns true if key exists in the given locale

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
93
# File 'lib/i18n_docs/missing_keys_finder.rb', line 85

def key_exists?(key, locale)
  I18n.locale = locale
  I18n.translate(key, raise: true)
  return true
rescue I18n::MissingInterpolationArgument
  return true
rescue I18n::MissingTranslationData
  return false
end

#load_configObject



100
101
102
103
104
105
106
107
# File 'lib/i18n_docs/missing_keys_finder.rb', line 100

def load_config
  @yaml = {}
  begin
    @yaml = YAML.load_file(File.join(Rails.root, 'config', 'ignore_missing_i18n_keys.yml'))
  rescue
    STDERR.puts 'No ignore_missing_keys.yml config file.'
  end
end

#load_translationsObject



95
96
97
98
# File 'lib/i18n_docs/missing_keys_finder.rb', line 95

def load_translations
  # Make sure we’ve loaded the translations
  I18n.backend.send(:init_translations)
end

#output_available_localesObject

rubocop:disable Metrics/LineLength



49
50
51
# File 'lib/i18n_docs/missing_keys_finder.rb', line 49

def output_available_locales
  puts "#{I18n.available_locales.size} #{I18n.available_locales.size == 1 ? 'locale' : 'locales'} available: #{I18n.available_locales.join(', ')}"
end

#output_missing_keys(missing_keys) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/i18n_docs/missing_keys_finder.rb', line 53

def output_missing_keys(missing_keys)
  if missing_keys.any?
    puts "#{missing_keys.size} #{missing_keys.size == 1 ? 'key is missing' : 'keys are missing'} from one or more locales:"
    missing_keys.keys.sort.each do |key|
      puts "'#{key}': Missing from #{missing_keys[key].collect(&:inspect).join(', ')}"
    end
    puts "\nERROR: #{missing_keys.size} #{missing_keys.size == 1 ? 'key is missing' : 'keys are missing'} from one or more locales."
  else
    puts 'No keys are missing'
  end
end

#output_unique_key_stats(keys) ⇒ Object

rubocop:enable Metrics/LineLength



66
67
68
69
# File 'lib/i18n_docs/missing_keys_finder.rb', line 66

def output_unique_key_stats(keys)
  number_of_keys = keys.size
  puts "#{number_of_keys} #{number_of_keys == 1 ? 'unique key' : 'unique keys'} found."
end