i18n-tasks 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 249e36d0665a2fda7758aede212244f485083b54
4
- data.tar.gz: 149073894264064e8972f43833fdffb4df4d2e18
3
+ metadata.gz: 4d271be49ca3aee877e8799c24145bc17b9d5c33
4
+ data.tar.gz: df4a3e1a654c27714df1117ab5a561ef62ab762c
5
5
  SHA512:
6
- metadata.gz: 772c68ce93f166f9c41886a3de694088031efb5433dd58cb8f3da032c91a3a5be3b55027d460a29f63ee2a41c7f9ad7d033a5d1abe4bb8693ea3cb175b409c3b
7
- data.tar.gz: 354dd75548fc81da5eb335be15472dcf96fcd269e8c6ff077ec1c44833a10011e2aea69aecd79a8e1b93cde6a9b478ba56e6a8ca38eca89f6f431dcd202a44f9
6
+ metadata.gz: 50a1d85e61b5a39d649c321648596268c70123193c913918bb6ac2eeedbe11bc2bba45ce0a6106622883541e944d248f3c9b2588d977ab87f900fef70c035ea4
7
+ data.tar.gz: 29f950b42403e129faf1473b4d8888fbdb61569569689adbc0fee72f237f017b80ba10d28562d213e22497d95f991ad7efe060734cd36e9ad6cf83c71f5e017a
data/README.md CHANGED
@@ -23,6 +23,8 @@ Relative keys (`t '.title'`) are supported too.
23
23
  devise.errors.unauthorized # ignore this key
24
24
  pagination.views. # ignore the whole pattern (note the .)
25
25
 
26
+ Keys listed in `.i18nignore` will also be excluded from `i18n:unused` report.
27
+
26
28
  For more examples see [the tests](https://github.com/glebm/i18n-tasks/blob/master/spec/i18n_tasks_spec.rb#L43-L59).
27
29
 
28
30
  Installation
@@ -30,11 +32,20 @@ Installation
30
32
 
31
33
  Simply add to Gemfile:
32
34
 
33
- gem 'i18n-tasks', '~> 0.0.7'
35
+ gem 'i18n-tasks', '~> 0.0.8'
34
36
 
35
37
  Configuration
36
38
  -------------
37
39
 
38
- Currently i18n-tasks only reports / writes to locale data in `config/locales/{locale_code}.yml`. *PRs making this configurable welcome!*
40
+ By default reports I18n reads locale data from `config/locales/{locale_code}.yml`.
41
+ You can customize this, e.g.:
42
+
43
+ # load all config/locales/*.locale.yml and config/locales/locale.yml:
44
+ I18n::Tasks.get_locale_data = ->(locale) {
45
+ (["config/locales/#{locale}.yml"] + Dir["config/locales/*.#{locale}.yml"]).inject({}) { |hash, path|
46
+ hash.merge! YAML.load_file(path)
47
+ hash
48
+ }
49
+ }
39
50
 
40
51
  [i18n-missing-screenshot]: https://raw.github.com/glebm/i18n-tasks/master/doc/img/i18n-missing.png "rake i18n:missing output screenshot"
data/lib/i18n/tasks.rb CHANGED
@@ -3,6 +3,9 @@ require 'i18n/tasks/railtie'
3
3
 
4
4
  module I18n
5
5
  module Tasks
6
-
6
+ mattr_accessor :get_locale_data
7
+ self.get_locale_data = lambda { |locale|
8
+ YAML.load_file("config/locales/#{locale}.yml")
9
+ }
7
10
  end
8
11
  end
@@ -1,21 +1,16 @@
1
1
  # coding: utf-8
2
- require 'open3'
3
2
  require 'term/ansicolor'
3
+ require 'i18n/tasks/task_helpers'
4
4
 
5
5
  module I18n
6
6
  module Tasks
7
7
  class BaseTask
8
8
  include Term::ANSIColor
9
-
10
- def run_command(*args)
11
- _in, out, _err = Open3.popen3(*args)
12
- out.gets nil
13
- end
9
+ include TaskHelpers
14
10
 
15
11
  # locale data hash, with locale name as root
16
12
  def get_locale_data(locale)
17
- # todo multiple files, configuration option
18
- YAML.load_file "config/locales/#{locale}.yml"
13
+ (@locale_data ||= {})[locale] ||= I18n::Tasks.get_locale_data.call(locale)
19
14
  end
20
15
 
21
16
  # main locale file path (for writing to)
@@ -30,7 +25,7 @@ module I18n
30
25
  used_keys = grep_out.split("\n").map { |r|
31
26
  key = r.match(/['"](.*?)['"]/)[1]
32
27
  # absolutize relative key:
33
- if key.start_with?('.')
28
+ if key.start_with? '.'
34
29
  path = r.split(':')[0]
35
30
  # normalized path
36
31
  path = Pathname.new(File.expand_path path).relative_path_from(Pathname.new(Dir.pwd)).to_s
@@ -40,18 +35,12 @@ module I18n
40
35
  else
41
36
  key
42
37
  end
43
- }.uniq.to_set
38
+ }.uniq
44
39
  used_keys = used_keys.reject { |k| k !~ /^[\w.\#{}]+$/ }
45
40
  exclude_patterns used_keys, ignore_patterns
46
41
  end
47
42
  end
48
43
 
49
- def exclude_patterns(keys, patterns)
50
- keys.reject do |k|
51
- patterns.any? { |pattern| k == pattern || pattern.end_with?('.') && k.start_with?(pattern) }
52
- end
53
- end
54
-
55
44
  IGNORE_FILE = '.i18nignore'
56
45
 
57
46
  def ignore_patterns
@@ -67,26 +56,28 @@ module I18n
67
56
  end
68
57
 
69
58
  def find_source_pattern_keys
70
- find_source_keys.select { |k| k =~ /\#{.*?}/ || k.ends_with?('.') }
59
+ @source_pattern_keys ||= find_source_keys.select { |k| k =~ /\#{.*?}/ || k.ends_with?('.') }
71
60
  end
72
61
 
73
62
  def find_source_pattern_prefixes
74
- find_source_pattern_keys.map { |k| k.split(/\.?#/)[0] }
63
+ @source_pattern_prefixes ||= find_source_pattern_keys.map { |k| k.split(/\.?#/)[0] }
75
64
  end
76
65
 
77
66
  # traverse hash, yielding with full key and value
78
- def traverse(path = '', hash, &block)
79
- hash.each do |k, v|
80
- if v.is_a?(Hash)
81
- traverse("#{path}.#{k}", v, &block)
67
+ def traverse(path = '', hash)
68
+ q = [ [path, hash] ]
69
+ until q.empty?
70
+ path, value = q.pop
71
+ if value.is_a?(Hash)
72
+ value.each { |k, v| q << ["#{path}.#{k}", v] }
82
73
  else
83
- block.call("#{path}.#{k}"[1..-1], v)
74
+ yield path[1..-1], value
84
75
  end
85
76
  end
86
77
  end
87
78
 
88
79
  def t(hash, key)
89
- key.split('.').inject(hash) { |r, seg| r.try(:[], seg) }
80
+ key.split('.').inject(hash) { |r, seg| r[seg] if r }
90
81
  end
91
82
 
92
83
  def base_locale
@@ -37,9 +37,10 @@ module I18n
37
37
  missing = []
38
38
 
39
39
  # missing keys (in the code but not in base locale data)
40
- pattern_prefixes = find_source_pattern_prefixes
40
+ pattern_re = compile_start_with_re find_source_pattern_prefixes
41
+
41
42
  find_source_keys.each do |key|
42
- if t(base[base_locale], key).blank? && !pattern_prefixes.any? { |pp| key.start_with?(pp) }
43
+ if t(base[base_locale], key).blank? && key !~ pattern_re
43
44
  missing << {locale: :en, type: :none, key: key}
44
45
  end
45
46
  end
@@ -1,9 +1,24 @@
1
1
  # coding: utf-8
2
-
3
-
2
+ require 'open3'
4
3
  module I18n
5
4
  module Tasks
6
5
  module TaskHelpers
6
+ # Run command and get only stdout output
7
+ def run_command(*args)
8
+ _in, out, _err = Open3.popen3(*args)
9
+ out.gets nil
10
+ end
11
+
12
+ # compile prefix matching Regexp from the list of prefixes
13
+ def compile_start_with_re(prefixes)
14
+ /^(?:#{prefixes.map{|p| Regexp.escape(p) }.join('|')})/
15
+ end
7
16
 
17
+ # exclude @keys with prefixes matching @patterns
18
+ def exclude_patterns(keys, patterns)
19
+ pattern_re = compile_start_with_re patterns.select { |p| p.end_with?('.') }
20
+ (keys - patterns).reject { |k| k =~ pattern_re }
21
+ end
22
+ end
8
23
  end
9
24
  end
@@ -14,11 +14,14 @@ module I18n
14
14
  end
15
15
 
16
16
  def find_unused
17
- used_keys = find_source_keys
18
- pattern_prefixes = find_source_pattern_prefixes
17
+ used_keys = find_source_keys.to_set
19
18
  r = []
19
+ ignore_re = compile_start_with_re ignore_patterns
20
+ pattern_re = compile_start_with_re find_source_pattern_prefixes
20
21
  traverse base[base_locale] do |key, value|
21
- r << [key, value] if !used_keys.include?(key) && !pattern_prefixes.any? { |pp| key.start_with?(pp) }
22
+ unless used_keys.include?(key) || key =~ pattern_re || key =~ ignore_re
23
+ r << [key, value]
24
+ end
22
25
  end
23
26
  r
24
27
  end
@@ -1,5 +1,5 @@
1
1
  module I18n
2
2
  module Tasks
3
- VERSION = '0.0.7'
3
+ VERSION = '0.0.8'
4
4
  end
5
5
  end
@@ -23,6 +23,7 @@ describe 'rake i18n' do
23
23
  end
24
24
 
25
25
  # --- setup ---
26
+ BENCH_KEYS = 3000
26
27
  before do
27
28
  gen_data = ->(v) {
28
29
  {
@@ -35,6 +36,10 @@ describe 'rake i18n' do
35
36
  'same_in_es' => {'a' => v},
36
37
  'blank_in_es' => {'a' => v},
37
38
  'relative' => {'index' => {'title' => v}}
39
+
40
+ }.tap {|r|
41
+ gen = r["bench"] = {}
42
+ BENCH_KEYS.times.map { |i| gen["key#{i}"] = v }
38
43
  }
39
44
  }
40
45
 
@@ -72,6 +77,8 @@ describe 'rake i18n' do
72
77
  end
73
78
  end
74
79
  RUBY
80
+ # test that our algorithms can scale to the order of {BENCH_KEYS} keys.
81
+ 'app/heavy.file' => BENCH_KEYS.times.map { |i| "t('bench.key#{i}') " }.join
75
82
  }
76
83
  TestCodebase.setup fs
77
84
  end
@@ -1,3 +1,4 @@
1
+ # coding: utf-8
1
2
  RSpec::Matchers.define :be_i18n_keys do |expected|
2
3
  locale_re = /^\w{2}\b/
3
4
  extract_keys = ->(actual){
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n-tasks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - glebm
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-15 00:00:00.000000000 Z
11
+ date: 2013-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails