i18n-tasks 0.1.2 → 0.1.3

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: 9c0488a6ce907e9327da7ed07ffea723e8f8010f
4
- data.tar.gz: 8f5042c6c785f81d5c75bc953fcd7d2cce496870
3
+ metadata.gz: cc1e95e2717382abb299d60e4b207b2b0152bc66
4
+ data.tar.gz: 4bf82fe58f91e8513f7f8b37aa2ebb4cee2c8d87
5
5
  SHA512:
6
- metadata.gz: 9297eb48c4d50a91e4825b2a3867360f0296f780bfecb7fd9e6ae39b98501ca916557f3ab12501be96ab45eccfbad46628d315b297edf761887d6c9471a98579
7
- data.tar.gz: 6eebe2d12de95e7e8563409007dd2df906768e7860047370d3e5cd9952b6382ac8abcb3096b14e404431c9477c5a54793e4a2068df956346d57536c4338008f2
6
+ metadata.gz: 0b37c93c586cdd40a0222d91d8b0d92c93f8a21ce3738a54f4c90f4d22c36e63f7782e8e3107dbe395c1e0277f9a4e957470bf89f6000f6b1cef9e22a9da2d04
7
+ data.tar.gz: 1a92543a5ace12423673f658bd9b951d520abe22209325feafd9eafced5fc349a6810d3a69d5ca04dbcddb0dd53b0a5a5cccfb1da1a915cb7943eccb983803ce
data/CHANGES.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## v0.1.3
2
+
3
+ * detect countable keys as used for unused task
4
+ * account for non-string keys coming from yaml (thanks @lichtamberg)
5
+
1
6
  ## v0.1.2
2
7
 
3
8
  * added grep config options (thanks @dmke)
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](./blob/master/lib/i18n/tasks/missing.rb))*
11
- * `i18n:prefill` task normalizes locale files, and adds missing keys from base locale to others *([source](./blob/master/lib/i18n/tasks/prefill.rb))*
12
- * `i18n:unused` task shows potentially unused translations *([source](./blob/master/lib/i18n/tasks/unused.rb))*
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 too.
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](./blob/master/spec/i18n_tasks_spec.rb#L43-L59).
23
+ For more examples see [the tests](/spec/i18n_tasks_spec.rb).
24
24
 
25
25
 
26
26
  ## Installation
@@ -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
@@ -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
- traverse base_locale_data do |key, value|
11
- r << [key, value] unless used_key?(key) || pattern_key?(key) || ignore_key?(key, :unused)
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
@@ -1,5 +1,5 @@
1
1
  module I18n
2
2
  module Tasks
3
- VERSION = '0.1.2'
3
+ VERSION = '0.1.3'
4
4
  end
5
5
  end
@@ -7,3 +7,5 @@ p = t 'ignore.a'
7
7
  p = t 'ignored_pattern.some_key'
8
8
  p = t 'ignore_eq_base_all.a'
9
9
  p = t 'ignore_eq_base_es.a'
10
+ p = t 'numeric.a'
11
+ p = t 'plural.a', count: 2
@@ -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 \(1\)/
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.2
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-08 00:00:00.000000000 Z
11
+ date: 2013-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails