i18n-tasks 0.0.8 → 0.1.0
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/.gitignore +2 -0
- data/.travis.yml +8 -2
- data/README.md +26 -9
- data/lib/i18n/tasks.rb +12 -0
- data/lib/i18n/tasks/base_task.rb +1 -15
- data/lib/i18n/tasks/missing.rb +11 -9
- data/lib/i18n/tasks/task_helpers.rb +24 -1
- data/lib/i18n/tasks/unused.rb +1 -1
- data/lib/i18n/tasks/version.rb +1 -1
- data/lib/tasks/i18n-tasks.rake +4 -0
- data/spec/i18n_tasks_spec.rb +28 -4
- metadata +3 -5
- data/.ruby-gemset +0 -1
- data/.ruby-version +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3695a5c801e3826fc8ec81bdf35d1ce830dae4f4
|
4
|
+
data.tar.gz: 3f0dceded74c9f668ae316dfd72ba1c1154d713b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f93118329d8599262d2b2443a5631b6cd6bb4adb1395b8f2e467fb70432939ffafe577b943f04eef2be6304ff274facc0454e4229becceda3b07087f55c96a0
|
7
|
+
data.tar.gz: 75f103be804ab756261c00dc318ea7669a3f20d92e7f4a9cb5223ba6ae60cf3af6d0b1d5d5dfc4fe8f8d9ddf05e41321f1d0e05ef3c711f24ad7af057e46b379
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -18,13 +18,6 @@ Use `rake -T i18n` to get the list of tasks with descriptions. There are 3 tasks
|
|
18
18
|
|
19
19
|
Relative keys (`t '.title'`) are supported too.
|
20
20
|
|
21
|
-
`i18n:missing` may incorrectly show framework i18n keys as missing, to work around this use `.i18nignore` file in project root:
|
22
|
-
|
23
|
-
devise.errors.unauthorized # ignore this key
|
24
|
-
pagination.views. # ignore the whole pattern (note the .)
|
25
|
-
|
26
|
-
Keys listed in `.i18nignore` will also be excluded from `i18n:unused` report.
|
27
|
-
|
28
21
|
For more examples see [the tests](https://github.com/glebm/i18n-tasks/blob/master/spec/i18n_tasks_spec.rb#L43-L59).
|
29
22
|
|
30
23
|
Installation
|
@@ -32,18 +25,42 @@ Installation
|
|
32
25
|
|
33
26
|
Simply add to Gemfile:
|
34
27
|
|
35
|
-
gem 'i18n-tasks', '~> 0.0
|
28
|
+
gem 'i18n-tasks', '~> 0.1.0'
|
36
29
|
|
37
30
|
Configuration
|
38
31
|
-------------
|
39
32
|
|
33
|
+
|
34
|
+
Tasks may incorrectly report framework i18n keys as missing. You can add `config/i18n-tasks.yml` to work around this:
|
35
|
+
|
36
|
+
# do not report these keys as missing (both on blank value and no key)
|
37
|
+
ignore_missing:
|
38
|
+
- devise.errors.unauthorized # ignore this key
|
39
|
+
- pagination.views. # ignore the whole pattern (note the .)
|
40
|
+
|
41
|
+
# do not report these keys when they have the same value as the base locale version
|
42
|
+
ignore_eq_base:
|
43
|
+
all:
|
44
|
+
- common.ok
|
45
|
+
es,fr:
|
46
|
+
- common.brand
|
47
|
+
|
48
|
+
# do not report these keys as unused
|
49
|
+
ignore_unused:
|
50
|
+
- category.
|
51
|
+
|
52
|
+
# do not report these keys ever
|
53
|
+
ignore:
|
54
|
+
- kaminari.
|
55
|
+
|
56
|
+
|
40
57
|
By default reports I18n reads locale data from `config/locales/{locale_code}.yml`.
|
41
58
|
You can customize this, e.g.:
|
42
59
|
|
43
60
|
# load all config/locales/*.locale.yml and config/locales/locale.yml:
|
44
61
|
I18n::Tasks.get_locale_data = ->(locale) {
|
45
62
|
(["config/locales/#{locale}.yml"] + Dir["config/locales/*.#{locale}.yml"]).inject({}) { |hash, path|
|
46
|
-
hash.
|
63
|
+
hash.deep_merge! YAML.load_file(path)
|
47
64
|
hash
|
48
65
|
}
|
49
66
|
}
|
data/lib/i18n/tasks.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'i18n/tasks/version'
|
2
2
|
require 'i18n/tasks/railtie'
|
3
|
+
require 'active_support/hash_with_indifferent_access'
|
3
4
|
|
4
5
|
module I18n
|
5
6
|
module Tasks
|
@@ -7,5 +8,16 @@ module I18n
|
|
7
8
|
self.get_locale_data = lambda { |locale|
|
8
9
|
YAML.load_file("config/locales/#{locale}.yml")
|
9
10
|
}
|
11
|
+
|
12
|
+
CONFIG_FILE = 'config/i18n-tasks.yml'
|
13
|
+
class << self
|
14
|
+
def config
|
15
|
+
@config ||= begin
|
16
|
+
file = File.read(CONFIG_FILE) if File.exists?(CONFIG_FILE)
|
17
|
+
file = YAML.load(file) if file.present?
|
18
|
+
HashWithIndifferentAccess.new.merge(file.presence || {})
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
10
22
|
end
|
11
23
|
end
|
data/lib/i18n/tasks/base_task.rb
CHANGED
@@ -36,24 +36,10 @@ module I18n
|
|
36
36
|
key
|
37
37
|
end
|
38
38
|
}.uniq
|
39
|
-
used_keys
|
40
|
-
exclude_patterns used_keys, ignore_patterns
|
39
|
+
used_keys.reject { |k| k !~ /^[\w.\#{}]+$/ }
|
41
40
|
end
|
42
41
|
end
|
43
42
|
|
44
|
-
IGNORE_FILE = '.i18nignore'
|
45
|
-
|
46
|
-
def ignore_patterns
|
47
|
-
@ignored_patterns ||= begin
|
48
|
-
if File.exists?(IGNORE_FILE)
|
49
|
-
File.read(IGNORE_FILE).split("\n").map {|k|
|
50
|
-
k.split('#')[0].try(:strip).presence
|
51
|
-
}.compact.uniq
|
52
|
-
else
|
53
|
-
[]
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
43
|
|
58
44
|
def find_source_pattern_keys
|
59
45
|
@source_pattern_keys ||= find_source_keys.select { |k| k =~ /\#{.*?}/ || k.ends_with?('.') }
|
data/lib/i18n/tasks/missing.rb
CHANGED
@@ -33,16 +33,18 @@ module I18n
|
|
33
33
|
|
34
34
|
|
35
35
|
|
36
|
+
# get all the missing translations as list of missing keys as hashes with:
|
37
|
+
# {:locale, :key, :type, and optionally :base_value}
|
38
|
+
# :type — :blank, :missing, or :eq_base
|
39
|
+
# :base_value — translation value in base locale if one is present
|
36
40
|
def find_missing
|
37
|
-
|
38
|
-
|
39
|
-
# missing keys (in the code but not in base locale data)
|
41
|
+
# dynamically generated keys in the source, e.g t("category.#{category_key}")
|
40
42
|
pattern_re = compile_start_with_re find_source_pattern_prefixes
|
41
43
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
44
|
+
# missing keys (in the code but not in base locale data)
|
45
|
+
keys = find_source_keys
|
46
|
+
missing = keys.select { |key| t(base[base_locale], key).blank? && key !~ pattern_re && key !~ ignore_pattern(:missing) }.map do |key|
|
47
|
+
{locale: base_locale, type: :none, key: key}
|
46
48
|
end
|
47
49
|
|
48
50
|
# missing translations (present in base locale, but untranslated in another locale )
|
@@ -50,9 +52,9 @@ module I18n
|
|
50
52
|
trn = get_locale_data(locale)[locale]
|
51
53
|
traverse base[base_locale] do |key, base_value|
|
52
54
|
translated = t(trn, key)
|
53
|
-
if translated.blank?
|
55
|
+
if translated.blank? && key !~ ignore_pattern(:missing)
|
54
56
|
missing << {locale: locale, key: key, type: :blank, base_value: base_value}
|
55
|
-
elsif translated == base_value
|
57
|
+
elsif translated == base_value && key !~ ignore_pattern(:eq_base, locale)
|
56
58
|
missing << {locale: locale, key: key, type: :eq_base, base_value: base_value}
|
57
59
|
end
|
58
60
|
end
|
@@ -11,7 +11,11 @@ module I18n
|
|
11
11
|
|
12
12
|
# compile prefix matching Regexp from the list of prefixes
|
13
13
|
def compile_start_with_re(prefixes)
|
14
|
-
|
14
|
+
if prefixes.blank?
|
15
|
+
/\Z\A/ # match nothing
|
16
|
+
else
|
17
|
+
/^(?:#{prefixes.map { |p| Regexp.escape(p) }.join('|')})/
|
18
|
+
end
|
15
19
|
end
|
16
20
|
|
17
21
|
# exclude @keys with prefixes matching @patterns
|
@@ -19,6 +23,25 @@ module I18n
|
|
19
23
|
pattern_re = compile_start_with_re patterns.select { |p| p.end_with?('.') }
|
20
24
|
(keys - patterns).reject { |k| k =~ pattern_re }
|
21
25
|
end
|
26
|
+
|
27
|
+
# type: missing, eq_base, unused
|
28
|
+
def ignore_pattern(type, locale = nil)
|
29
|
+
((@ignore_patterns ||= HashWithIndifferentAccess.new)[type] ||= {})[locale] = begin
|
30
|
+
global = config[:ignore] || []
|
31
|
+
type_ignore = config["ignore_#{type}"] || []
|
32
|
+
if type_ignore.is_a?(Array)
|
33
|
+
compile_start_with_re global + type_ignore
|
34
|
+
elsif type_ignore.is_a?(Hash)
|
35
|
+
p = global + (type_ignore[:all] || [])
|
36
|
+
type_ignore.each { |key, value| p += (value || []) if key.to_s =~ /\b#{locale}\b/ } if locale
|
37
|
+
compile_start_with_re p
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def config
|
43
|
+
I18n::Tasks.config
|
44
|
+
end
|
22
45
|
end
|
23
46
|
end
|
24
47
|
end
|
data/lib/i18n/tasks/unused.rb
CHANGED
@@ -16,7 +16,7 @@ module I18n
|
|
16
16
|
def find_unused
|
17
17
|
used_keys = find_source_keys.to_set
|
18
18
|
r = []
|
19
|
-
ignore_re =
|
19
|
+
ignore_re = ignore_pattern(:unused)
|
20
20
|
pattern_re = compile_start_with_re find_source_pattern_prefixes
|
21
21
|
traverse base[base_locale] do |key, value|
|
22
22
|
unless used_keys.include?(key) || key =~ pattern_re || key =~ ignore_re
|
data/lib/i18n/tasks/version.rb
CHANGED
data/lib/tasks/i18n-tasks.rake
CHANGED
@@ -13,6 +13,10 @@ namespace :i18n do
|
|
13
13
|
|
14
14
|
desc 'show keys with translation values identical to base'
|
15
15
|
task :missing => :environment do
|
16
|
+
if File.exists?('.i18nignore')
|
17
|
+
STDERR.puts "!!!! Looks like you are using .i18ignore. It is no longer used in favour of config/i18n-tasks.yml.\n
|
18
|
+
See README.md https://github.com/glebm/i18n-tasks"
|
19
|
+
end
|
16
20
|
I18n::Tasks::Missing.new.perform
|
17
21
|
end
|
18
22
|
|
data/spec/i18n_tasks_spec.rb
CHANGED
@@ -32,8 +32,11 @@ describe 'rake i18n' do
|
|
32
32
|
'hash_pattern' => {'a' => v},
|
33
33
|
'hash_pattern2' => {'a' => v},
|
34
34
|
'unused' => {'a' => v},
|
35
|
+
'ignore_unused' => {'a' => v},
|
35
36
|
'missing_in_es' => {'a' => v},
|
36
37
|
'same_in_es' => {'a' => v},
|
38
|
+
'ignore_eq_base_all' => {'a' => v},
|
39
|
+
'ignore_eq_base_es' => {'a' => v},
|
37
40
|
'blank_in_es' => {'a' => v},
|
38
41
|
'relative' => {'index' => {'title' => v}}
|
39
42
|
|
@@ -47,22 +50,43 @@ describe 'rake i18n' do
|
|
47
50
|
es_data = gen_data.('ES_TEXT').except('missing_in_es')
|
48
51
|
es_data['same_in_es']['a'] = 'EN_TEXT'
|
49
52
|
es_data['blank_in_es']['a'] = ''
|
53
|
+
es_data['ignore_eq_base_all']['a'] = 'EN_TEXT'
|
54
|
+
es_data['ignore_eq_base_es']['a'] = 'EN_TEXT'
|
50
55
|
|
51
56
|
fs = {
|
52
57
|
'config/locales/en.yml' => {'en' => en_data}.to_yaml,
|
53
58
|
'config/locales/es.yml' => {'es' => es_data}.to_yaml,
|
54
|
-
'.
|
55
|
-
|
59
|
+
'config/i18n-tasks.yml' => <<-YML,
|
60
|
+
# do not report these keys as missing:
|
61
|
+
ignore_missing:
|
62
|
+
- ignored_missing_key.a # one key to ignore
|
63
|
+
- ignored_pattern. # ignore the whole pattern
|
56
64
|
|
57
|
-
|
58
|
-
|
65
|
+
# do not report these keys when they have the same value as the base locale version
|
66
|
+
ignore_eq_base:
|
67
|
+
all:
|
68
|
+
- ignore_eq_base_all.a
|
69
|
+
es:
|
70
|
+
- ignore_eq_base_es.a
|
71
|
+
|
72
|
+
# do not report these keys as unused
|
73
|
+
ignore_unused:
|
74
|
+
- ignore_unused.a
|
75
|
+
|
76
|
+
# do not report these keys ever
|
77
|
+
ignore:
|
78
|
+
- ignore.a
|
79
|
+
YML
|
59
80
|
'app/views/index.html.slim' => <<-SLIM,
|
60
81
|
p \#{t('ca.a')} \#{t 'ca.b'} \#{t "ca.c"}
|
61
82
|
p \#{t 'ca.d'} \#{t 'ca.f', i: 'world'} \#{t 'ca.e', i: 'world'}
|
62
83
|
p \#{t 'missing_in_es.a'} \#{t 'same_in_es.a'} \#{t 'blank_in_es.a'}
|
63
84
|
p = t 'used_but_missing.a'
|
64
85
|
p = t 'ignored_missing_key.a'
|
86
|
+
p = t 'ignore.a'
|
65
87
|
p = t 'ignored_pattern.some_key'
|
88
|
+
p = t 'ignore_eq_base_all.a'
|
89
|
+
p = t 'ignore_eq_base_es.a'
|
66
90
|
SLIM
|
67
91
|
'app/views/relative/index.html.slim' => <<-SLIM,
|
68
92
|
p = t '.title'
|
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.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- glebm
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-08-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -102,8 +102,6 @@ extensions: []
|
|
102
102
|
extra_rdoc_files: []
|
103
103
|
files:
|
104
104
|
- .gitignore
|
105
|
-
- .ruby-gemset
|
106
|
-
- .ruby-version
|
107
105
|
- .travis.yml
|
108
106
|
- Gemfile
|
109
107
|
- LICENSE.txt
|
@@ -146,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
146
144
|
version: '0'
|
147
145
|
requirements: []
|
148
146
|
rubyforge_project:
|
149
|
-
rubygems_version: 2.0.
|
147
|
+
rubygems_version: 2.0.6
|
150
148
|
signing_key:
|
151
149
|
specification_version: 4
|
152
150
|
summary: Rails I18n tasks to find missing / unused translations and more
|
data/.ruby-gemset
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
i18n-tasks
|
data/.ruby-version
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
2.0.0-p195
|