Class: Isomorfeus::I18n::MissingKeys

Inherits:
Object
  • Object
show all
Defined in:
lib/isomorfeus/i18n/missing_keys.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMissingKeys

Returns a new instance of MissingKeys.



8
9
10
11
12
13
14
15
16
17
# File 'lib/isomorfeus/i18n/missing_keys.rb', line 8

def initialize
  Isomorfeus::I18n::Init.init
  @locale_keys = {}
  FastGettext.available_locales.each do |locale|
    @locale_keys[locale] = Set.new
  end
  @all_keys = Set.new
  @used_keys = Set.new
  @all_files = Set.new
end

Class Method Details

.findObject



4
5
6
# File 'lib/isomorfeus/i18n/missing_keys.rb', line 4

def self.find
  new.find
end

Instance Method Details

#collect_files(dir) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/isomorfeus/i18n/missing_keys.rb', line 119

def collect_files(dir)
  d = Dir.open(dir)
  d.each_child do |child|
    child = File.expand_path(File.join(dir, child))
    if Dir.exist?(child)
      collect_files(child)
    else
      @all_files.add child if child.end_with?('.rb')
    end
  end
end

#findObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/isomorfeus/i18n/missing_keys.rb', line 19

def find
  # get keys for each locale
  get_locale_keys
  print_missing_locale_keys
  get_used_keys
  puts
  print_missing_keys
  puts
  print_unused_keys
end

#get_file_keys(file) ⇒ Object



70
71
72
73
74
75
# File 'lib/isomorfeus/i18n/missing_keys.rb', line 70

def get_file_keys(file)
  contents = File.read(file)
  contents.scan(/_\(\s*["':]([\w\-.]+)["']\s*\)/).each do |hit|
    @used_keys.add(hit.first)
  end
end

#get_locale_keysObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/isomorfeus/i18n/missing_keys.rb', line 30

def get_locale_keys
  FastGettext.translation_repositories.each_key do |domain|
    FastGettext.available_locales.each do |locale|
      FastGettext.locale = locale
      FastGettext.translation_repositories[domain].each do |set|
        @locale_keys[locale].add set[0]
        @all_keys.add set[0]
      end
    end
  end
end

#get_used_keysObject



63
64
65
66
67
68
# File 'lib/isomorfeus/i18n/missing_keys.rb', line 63

def get_used_keys
  collect_files(Isomorfeus.app_root)
  @all_files.each do |file|
    get_file_keys(file)
  end
end


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/isomorfeus/i18n/missing_keys.rb', line 77

def print_missing_keys
  puts "used keys missing in locale:"
  @used_keys.sort.each do |key|
    ls = []
    @locale_keys.each_key do |locale|
      ls << locale unless @locale_keys[locale]&.include?(key)
    end
    unless ls.empty?
      print "%-30s" % key
      @locale_keys.each_key do |locale|
        if ls.include?(locale)
          print "\t#{locale}"
        else
          print "\t-"
        end
      end
      print "\n"
    end
  end
end


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/isomorfeus/i18n/missing_keys.rb', line 42

def print_missing_locale_keys
  puts "keys missing in locale:\n"
  @all_keys.sort.each do |key|
    ls = []
    @locale_keys.each_key do |locale|
      ls << locale unless @locale_keys[locale].include?(key)
    end
    unless ls.empty?
      print "%-30s" % key
      @locale_keys.each_key do |locale|
        if ls.include?(locale)
          print "\t#{locale}"
        else
          print "\t-"
        end
      end
      print "\n"
    end
  end
end


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/isomorfeus/i18n/missing_keys.rb', line 98

def print_unused_keys
  puts "unused keys (may be used in variable keys):"
  @all_keys.sort.each do |key|
    unless @used_keys.include?(key)
      ls = []
      @locale_keys.each_key do |locale|
        ls << locale if @locale_keys[locale]&.include?(key)
      end
      print "%-30s" % key
      @locale_keys.each_key do |locale|
        if ls.include?(locale)
          print "\t#{locale}"
        else
          print "\t-"
        end
      end
      print "\n"
    end
  end
end