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 +4 -4
- data/README.md +13 -2
- data/lib/i18n/tasks.rb +4 -1
- data/lib/i18n/tasks/base_task.rb +15 -24
- data/lib/i18n/tasks/missing.rb +3 -2
- data/lib/i18n/tasks/task_helpers.rb +17 -2
- data/lib/i18n/tasks/unused.rb +6 -3
- data/lib/i18n/tasks/version.rb +1 -1
- data/spec/i18n_tasks_spec.rb +7 -0
- data/spec/support/i18n_tasks_output_matcher.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4d271be49ca3aee877e8799c24145bc17b9d5c33
|
4
|
+
data.tar.gz: df4a3e1a654c27714df1117ab5a561ef62ab762c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
35
|
+
gem 'i18n-tasks', '~> 0.0.8'
|
34
36
|
|
35
37
|
Configuration
|
36
38
|
-------------
|
37
39
|
|
38
|
-
|
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
data/lib/i18n/tasks/base_task.rb
CHANGED
@@ -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
|
-
|
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
|
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
|
79
|
-
|
80
|
-
|
81
|
-
|
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
|
-
|
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
|
80
|
+
key.split('.').inject(hash) { |r, seg| r[seg] if r }
|
90
81
|
end
|
91
82
|
|
92
83
|
def base_locale
|
data/lib/i18n/tasks/missing.rb
CHANGED
@@ -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
|
-
|
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? &&
|
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
|
data/lib/i18n/tasks/unused.rb
CHANGED
@@ -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
|
-
|
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
|
data/lib/i18n/tasks/version.rb
CHANGED
data/spec/i18n_tasks_spec.rb
CHANGED
@@ -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
|
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.
|
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-
|
11
|
+
date: 2013-07-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|