i18n-tasks 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +5 -0
- data/README.md +5 -5
- data/lib/i18n/tasks/base_task.rb +16 -0
- data/lib/i18n/tasks/output/terminal.rb +2 -2
- data/lib/i18n/tasks/unused.rb +8 -5
- data/lib/i18n/tasks/version.rb +1 -1
- data/spec/fixtures/app/views/index.html.slim +2 -0
- data/spec/i18n_tasks_spec.rb +7 -4
- 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: cc1e95e2717382abb299d60e4b207b2b0152bc66
|
4
|
+
data.tar.gz: 4bf82fe58f91e8513f7f8b37aa2ebb4cee2c8d87
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b37c93c586cdd40a0222d91d8b0d92c93f8a21ce3738a54f4c90f4d22c36e63f7782e8e3107dbe395c1e0277f9a4e957470bf89f6000f6b1cef9e22a9da2d04
|
7
|
+
data.tar.gz: 1a92543a5ace12423673f658bd9b951d520abe22209325feafd9eafced5fc349a6810d3a69d5ca04dbcddb0dd53b0a5a5cccfb1da1a915cb7943eccb983803ce
|
data/CHANGES.md
CHANGED
data/README.md
CHANGED
@@ -7,9 +7,9 @@ Rails I18n tasks to find missing / unused translations and more. Works with slim
|
|
7
7
|
|
8
8
|
Use `rake -T i18n` to get the list of tasks with descriptions. There are 3 tasks available at the moment:
|
9
9
|
|
10
|
-
* `i18n:missing` task shows all the keys that have not been translated yet *([source](
|
11
|
-
* `i18n:prefill` task normalizes locale files, and adds missing keys from base locale to others *([source](
|
12
|
-
* `i18n:unused` task shows potentially unused translations *([source](
|
10
|
+
* `i18n:missing` task shows all the keys that have not been translated yet *([source](/lib/i18n/tasks/missing.rb))*
|
11
|
+
* `i18n:prefill` task normalizes locale files, and adds missing keys from base locale to others *([source](/lib/i18n/tasks/prefill.rb))*
|
12
|
+
* `i18n:unused` task shows potentially unused translations *([source](/lib/i18n/tasks/unused.rb))*
|
13
13
|
|
14
14
|
The `i18n:unused` task will detect pattern translations and not report them, e.g.:
|
15
15
|
|
@@ -18,9 +18,9 @@ t 'category.' + category.key # 'category.arts_and_crafts' considered used
|
|
18
18
|
t "category.#{category.key}" # also works
|
19
19
|
```
|
20
20
|
|
21
|
-
Relative keys (`t '.title'`) are supported
|
21
|
+
Relative keys (`t '.title'`) are supported. Plural keys (key.one/many/other/etc) are supported.
|
22
22
|
|
23
|
-
For more examples see [the tests](
|
23
|
+
For more examples see [the tests](/spec/i18n_tasks_spec.rb).
|
24
24
|
|
25
25
|
|
26
26
|
## Installation
|
data/lib/i18n/tasks/base_task.rb
CHANGED
@@ -100,6 +100,22 @@ module I18n
|
|
100
100
|
"#{prefix}#{key}"
|
101
101
|
end
|
102
102
|
|
103
|
+
PLURAL_KEY_RE = /\.(?:zero|one|two|few|many|other)$/
|
104
|
+
|
105
|
+
# @param key [String] i18n key
|
106
|
+
# @param data [Hash{String => String,Hash}] locale data
|
107
|
+
# @return the base form if the key is a specific plural form (e.g. apple for apple.many), and the key as passed otherwise
|
108
|
+
def depluralize_key(key, data)
|
109
|
+
return key if key !~ PLURAL_KEY_RE || t(data, key).is_a?(Hash)
|
110
|
+
parent_key = key.split('.')[0..-2] * '.'
|
111
|
+
plural_versions = t(data, parent_key)
|
112
|
+
if plural_versions.is_a?(Hash) && plural_versions.all? { |k, v| ".#{k}" =~ PLURAL_KEY_RE && !v.is_a?(Hash) }
|
113
|
+
parent_key
|
114
|
+
else
|
115
|
+
key
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
103
119
|
|
104
120
|
# @return [String] default i18n locale
|
105
121
|
def base_locale
|
@@ -15,7 +15,7 @@ module I18n
|
|
15
15
|
def unused(unused)
|
16
16
|
$stderr.puts bold cyan("Unused i18n keys (#{unused.length})")
|
17
17
|
key_col_width = unused.max_by { |x| x[0].length }[0].length + 2
|
18
|
-
unused.each { |(key, value)| puts "#{magenta key.ljust(key_col_width)}#{cyan value.strip}" }
|
18
|
+
unused.each { |(key, value)| puts "#{magenta key.ljust(key_col_width)}#{cyan value.to_s.strip}" }
|
19
19
|
end
|
20
20
|
|
21
21
|
private
|
@@ -28,7 +28,7 @@ module I18n
|
|
28
28
|
}
|
29
29
|
|
30
30
|
def print_missing_translation(m, opts)
|
31
|
-
locale, key, base_value, status_text = m[:locale], m[:key], m[:base_value].try(:strip), " #{STATUS_TEXTS[m[:type]]}"
|
31
|
+
locale, key, base_value, status_text = m[:locale], m[:key], m[:base_value].to_s.try(:strip), " #{STATUS_TEXTS[m[:type]]}"
|
32
32
|
|
33
33
|
key = magenta key.ljust(opts[:key_col_width])
|
34
34
|
s = if m[:type] == :none
|
data/lib/i18n/tasks/unused.rb
CHANGED
@@ -6,12 +6,15 @@ module I18n
|
|
6
6
|
class Unused < BaseTask
|
7
7
|
# @return [Array<[String, String]>] all the unused translations as an array of [key, value] pairs
|
8
8
|
def find_keys
|
9
|
-
r
|
10
|
-
|
11
|
-
|
9
|
+
r = []
|
10
|
+
data = base_locale_data
|
11
|
+
traverse data do |key, value|
|
12
|
+
next if pattern_key?(key) || ignore_key?(key, :unused)
|
13
|
+
key = depluralize_key(key, data)
|
14
|
+
r << [key, value] unless used_key?(key)
|
12
15
|
end
|
13
|
-
r
|
16
|
+
r.uniq
|
14
17
|
end
|
15
18
|
end
|
16
19
|
end
|
17
|
-
end
|
20
|
+
end
|
data/lib/i18n/tasks/version.rb
CHANGED
data/spec/i18n_tasks_spec.rb
CHANGED
@@ -13,8 +13,8 @@ describe 'rake i18n' do
|
|
13
13
|
describe 'unused' do
|
14
14
|
it 'detects unused' do
|
15
15
|
TestCodebase.capture_stderr do
|
16
|
-
TestCodebase.rake_result('i18n:unused').should be_i18n_keys %w(unused.a)
|
17
|
-
end.should =~ /Unused i18n keys \(
|
16
|
+
TestCodebase.rake_result('i18n:unused').should be_i18n_keys %w(unused.a unused.numeric unused.plural)
|
17
|
+
end.should =~ /Unused i18n keys \(3\)/
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
@@ -30,19 +30,22 @@ describe 'rake i18n' do
|
|
30
30
|
BENCH_KEYS = 30
|
31
31
|
before do
|
32
32
|
gen_data = ->(v) {
|
33
|
+
v_num = v.chars.map(&:ord).join('').to_i
|
33
34
|
{
|
34
35
|
'ca' => {'a' => v, 'b' => v, 'c' => v, 'd' => v, 'e' => "#{v}%{i}", 'f' => "#{v}%{i}"},
|
35
36
|
'cb' => {'a' => v, 'b' => "#{v}%{i}"},
|
36
37
|
'hash_pattern' => {'a' => v},
|
37
38
|
'hash_pattern2' => {'a' => v},
|
38
|
-
'unused' => {'a' => v},
|
39
|
+
'unused' => {'a' => v, 'numeric' => v_num, 'plural' => {'one' => v, 'other' => v}},
|
39
40
|
'ignore_unused' => {'a' => v},
|
40
41
|
'missing_in_es' => {'a' => v},
|
41
42
|
'same_in_es' => {'a' => v},
|
42
43
|
'ignore_eq_base_all' => {'a' => v},
|
43
44
|
'ignore_eq_base_es' => {'a' => v},
|
44
45
|
'blank_in_es' => {'a' => v},
|
45
|
-
'relative' => {'index' => {'title' => v}}
|
46
|
+
'relative' => {'index' => {'title' => v}},
|
47
|
+
'numeric' => {'a' => v_num},
|
48
|
+
'plural' => {'a' => {'one' => v, 'other' => "%{count} #{v}s"}}
|
46
49
|
}.tap { |r|
|
47
50
|
gen = r["bench"] = {}
|
48
51
|
BENCH_KEYS.times { |i| gen["key#{i}"] = v }
|
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.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- glebm
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|