i18n_missing_keys 0.1.21 → 0.1.22

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/tasks/i18n_missing_keys.rake +116 -0
  2. metadata +3 -2
@@ -0,0 +1,116 @@
1
+ namespace :i18n do
2
+ desc "Find and list translation keys that do not exist in all locales"
3
+ task :missing_keys => :environment do
4
+ finder = MissingKeysFinder.new(I18n.backend)
5
+ finder.find_missing_keys
6
+ end
7
+ end
8
+
9
+
10
+ class MissingKeysFinder
11
+
12
+ def initialize(backend)
13
+ @backend = backend
14
+ self.load_config
15
+ self.load_translations
16
+ end
17
+
18
+ # Returns an array with all keys from all locales
19
+ def all_keys
20
+ I18n.backend.send(:translations).collect do |check_locale, translations|
21
+ collect_keys([], translations).sort
22
+ end.flatten.uniq
23
+ end
24
+
25
+ def find_missing_keys
26
+ output_available_locales
27
+ output_unique_key_stats(all_keys)
28
+
29
+ missing_keys = {}
30
+ all_keys.each do |key|
31
+
32
+ I18n.available_locales.each do |locale|
33
+
34
+ skip = false
35
+ ls = locale.to_s
36
+ if !@yaml[ls].nil?
37
+ @yaml[ls].each do |re|
38
+ if key.match(re)
39
+ skip = true
40
+ break
41
+ end
42
+ end
43
+ end
44
+
45
+ if !key_exists?(key, locale) && skip == false
46
+ if missing_keys[key]
47
+ missing_keys[key] << locale
48
+ else
49
+ missing_keys[key] = [locale]
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ output_missing_keys(missing_keys)
56
+ return missing_keys
57
+ end
58
+
59
+ def output_available_locales
60
+ puts "#{I18n.available_locales.size} #{I18n.available_locales.size == 1 ? 'locale' : 'locales'} available: #{I18n.available_locales.join(', ')}"
61
+ end
62
+
63
+ def output_missing_keys(missing_keys)
64
+ puts "#{missing_keys.size} #{missing_keys.size == 1 ? 'key is missing' : 'keys are missing'} from one or more locales:"
65
+ missing_keys.keys.sort.each do |key|
66
+ puts "'#{key}': Missing from #{missing_keys[key].collect(&:inspect).join(', ')}"
67
+ end
68
+ end
69
+
70
+ def output_unique_key_stats(keys)
71
+ number_of_keys = keys.size
72
+ puts "#{number_of_keys} #{number_of_keys == 1 ? 'unique key' : 'unique keys'} found."
73
+ end
74
+
75
+ def collect_keys(scope, translations)
76
+ full_keys = []
77
+ translations.to_a.each do |key, translations|
78
+ next if translations.nil?
79
+
80
+ new_scope = scope.dup << key
81
+ if translations.is_a?(Hash)
82
+ full_keys += collect_keys(new_scope, translations)
83
+ else
84
+ full_keys << new_scope.join('.')
85
+ end
86
+ end
87
+ return full_keys
88
+ end
89
+
90
+ # Returns true if key exists in the given locale
91
+ def key_exists?(key, locale)
92
+ I18n.locale = locale
93
+ I18n.translate(key, :raise => true)
94
+ return true
95
+ rescue I18n::MissingInterpolationArgument
96
+ return true
97
+ rescue I18n::MissingTranslationData
98
+ return false
99
+ end
100
+
101
+ def load_translations
102
+ # Make sure we’ve loaded the translations
103
+ I18n.backend.send(:init_translations)
104
+ end
105
+
106
+ def load_config
107
+ @yaml = {}
108
+ begin
109
+ @yaml = YAML.load_file(File.join(Rails.root, 'config', 'ignore_missing_keys.yml'))
110
+ rescue => e
111
+ STDERR.puts "No ignore_missing_keys.yml config file."
112
+ end
113
+
114
+ end
115
+
116
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n_missing_keys
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.21
4
+ version: 0.1.22
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -118,6 +118,7 @@ extra_rdoc_files:
118
118
  files:
119
119
  - lib/i18n_missing_keys.rb
120
120
  - lib/i18n_missing_keys/railtie.rb
121
+ - lib/tasks/i18n_missing_keys.rake
121
122
  - LICENSE.txt
122
123
  - README.rdoc
123
124
  homepage: http://github.com/renuo/i18n_missing_keys
@@ -135,7 +136,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
135
136
  version: '0'
136
137
  segments:
137
138
  - 0
138
- hash: 2622860916469597881
139
+ hash: 2890609892691551958
139
140
  required_rubygems_version: !ruby/object:Gem::Requirement
140
141
  none: false
141
142
  requirements: