i18n-tasks 0.0.4 → 0.0.5

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: b9ffd7cede573f8a4cee0a811d80345a6e8ebb37
4
- data.tar.gz: 813300ebfe3b65e3d624c6c67636121f8d874603
3
+ metadata.gz: a588b5fcc4c85ab519a9cf45074cbc8d5f77511e
4
+ data.tar.gz: b9d6327dfce3a34fe6c5621cc3075c121533786b
5
5
  SHA512:
6
- metadata.gz: e205fafc63991aae11c9a7506886b235fe2c5dd612402ebb12de0c9c9954887b97a426a8dbbcb8122251c56c65281516fff37e677b9def315d9975de6f39b8d4
7
- data.tar.gz: 0991e4295a36944f8545ee25259307f20c239b87d1e968515c1e55895fcf252f52666272f4a1b6e09bb9e2c1b137d24f30fe5ad83ac9480859e1c8cf0bc7ae35
6
+ metadata.gz: 345f4b4255b47573f1ab42d02414bae76223d9c7e6978106701ccc325417aa5203275863b6f8b927a572ebfd64d1d07241a79932a7b53f65c3d34a780e90b535
7
+ data.tar.gz: 8a612fad25cf93919596ffba6f1fe9d07a80f91b52267872ac6d85bd1c4f21fce36b89141725f0051cd1aa3a525e86fd30ff8e74c37a199edfc7c06e3b9cb7fc
data/README.md CHANGED
@@ -16,6 +16,12 @@ Use `rake -T i18n` to get the list of tasks with descriptions. There are 3 tasks
16
16
  t 'category.' + category.key # 'category.arts_and_crafts' considered used
17
17
  t "category.#{category.key}" # also works
18
18
 
19
+ `i18n:missing` may incorrectly show framework i18n keys as missing, to work around this use `.i18nignore` file in project root:
20
+
21
+ devise.errors.unauthorized # ignore this key
22
+ pagination.views. # ignore the whole pattern (note the .)
23
+
24
+
19
25
  For more examples see [the tests](https://github.com/glebm/i18n-tasks/blob/master/spec/i18n_tasks_spec.rb#L43-L59).
20
26
 
21
27
  Installation
@@ -23,7 +29,7 @@ Installation
23
29
 
24
30
  Simply add to Gemfile:
25
31
 
26
- gem 'i18n-tasks', '~> 0.0.3'
32
+ gem 'i18n-tasks', '~> 0.0.5'
27
33
 
28
34
  Configuration
29
35
  -------------
Binary file
@@ -1,9 +1,87 @@
1
- require 'i18n/tasks/task_helpers'
1
+ require 'open3'
2
+ require 'term/ansicolor'
2
3
 
3
4
  module I18n
4
5
  module Tasks
5
6
  class BaseTask
6
- include TaskHelpers
7
+ include Term::ANSIColor
8
+
9
+ def run_command(*args)
10
+ _in, out, _err = Open3.popen3(*args)
11
+ out.gets nil
12
+ end
13
+
14
+ # locale data hash, with locale name as root
15
+ def get_locale_data(locale)
16
+ # todo multiple files, configuration option
17
+ YAML.load_file "config/locales/#{locale}.yml"
18
+ end
19
+
20
+ # main locale file path (for writing to)
21
+ def locale_file_path(locale)
22
+ "config/locales/#{locale}.yml"
23
+ end
24
+
25
+ # find all keys in the source
26
+ def find_source_keys
27
+ @source_keys ||= begin
28
+ grep_out = run_command 'grep', '-horI', %q{\\bt(\\?\\s*['"]\\([^'"]*\\)['"]}, 'app/'
29
+ used_keys = grep_out.split("\n").map { |r| r.match(/['"](.*?)['"]/)[1] }.uniq.to_set
30
+ used_keys = used_keys.reject { |k| k !~ /^[\w.\#{}]+$/ }
31
+ exclude_patterns used_keys, ignore_patterns
32
+ end
33
+ end
34
+
35
+ def exclude_patterns(keys, patterns)
36
+ keys.reject do |k|
37
+ patterns.any? { |pattern| k == pattern || pattern.end_with?('.') && k.start_with?(pattern) }
38
+ end
39
+ end
40
+
41
+ IGNORE_FILE = '.i18nignore'
42
+
43
+ def ignore_patterns
44
+ @ignored_patterns ||= begin
45
+ if File.exists?(IGNORE_FILE)
46
+ File.read(IGNORE_FILE).split("\n").map {|k|
47
+ k.split('#')[0].strip.presence
48
+ }.compact.uniq
49
+ else
50
+ []
51
+ end
52
+ end
53
+ end
54
+
55
+ def find_source_pattern_keys
56
+ find_source_keys.select { |k| k =~ /\#{.*?}/ || k.ends_with?('.') }
57
+ end
58
+
59
+ def find_source_pattern_prefixes
60
+ find_source_pattern_keys.map { |k| k.split(/\.?#/)[0] }
61
+ end
62
+
63
+ # traverse hash, yielding with full key and value
64
+ def traverse(path = '', hash, &block)
65
+ hash.each do |k, v|
66
+ if v.is_a?(Hash)
67
+ traverse("#{path}.#{k}", v, &block)
68
+ else
69
+ block.call("#{path}.#{k}"[1..-1], v)
70
+ end
71
+ end
72
+ end
73
+
74
+ def t(hash, key)
75
+ key.split('.').inject(hash) { |r, seg| r.try(:[], seg) }
76
+ end
77
+
78
+ def base_locale
79
+ I18n.default_locale.to_s
80
+ end
81
+
82
+ def base
83
+ @base ||= get_locale_data(base_locale)
84
+ end
7
85
  end
8
86
  end
9
87
  end
@@ -9,10 +9,12 @@ module I18n
9
9
  def perform
10
10
  missing = find_missing
11
11
  STDERR.puts bold cyan("= #{DESC} (#{missing.length}) =")
12
+ STDERR.puts cyan(" This task may report framework i18n keys as missing (use .i18nignore if that happens)")
13
+ STDERR.puts cyan " Legend:\t#{red '✗'} - key is missing\t#{yellow bold '∅'} - translation is blank\t#{yellow bold '='} - value same as base locale"
12
14
  status_texts = {
13
- none: red("✗ #{bold 'none'}".ljust(19)),
14
- blank: yellow(bold '∅ blank'.ljust(11)),
15
- eq_base: yellow(bold "= #{base_locale.ljust(9)}")
15
+ none: red("✗".ljust(6)),
16
+ blank: yellow(bold '∅'.ljust(6)),
17
+ eq_base: yellow(bold "=".ljust(6))
16
18
  }
17
19
 
18
20
  missing.sort_by { |m| m[:type] }.reverse_each do |m|
@@ -1,65 +1,9 @@
1
1
  # coding: utf-8
2
- require 'open3'
3
- require 'term/ansicolor'
2
+
4
3
 
5
4
  module I18n
6
5
  module Tasks
7
6
  module TaskHelpers
8
- include Term::ANSIColor
9
- def run_command(*args)
10
- _in, out, _err = Open3.popen3(*args)
11
- out.gets nil
12
- end
13
-
14
- # locale data hash, with locale name as root
15
- def get_locale_data(locale)
16
- # todo multiple files, configuration option
17
- YAML.load_file "config/locales/#{locale}.yml"
18
- end
19
-
20
- # main locale file path (for writing to)
21
- def locale_file_path(locale)
22
- "config/locales/#{locale}.yml"
23
- end
24
-
25
- # find all keys in the source
26
- def find_source_keys
27
- @source_keys ||= begin
28
- grep_out = run_command 'grep', '-horI', %q{\\bt(\\?\\s*['"]\\([^'"]*\\)['"]}, 'app/'
29
- used_keys = grep_out.split("\n").map { |r| r.match(/['"](.*?)['"]/)[1] }.uniq.to_set
30
- end
31
- end
32
-
33
- def find_source_pattern_keys
34
- find_source_keys.select { |k| k =~ /\#{.*?}/ || k.ends_with?('.') }
35
- end
36
-
37
- def find_source_pattern_prefixes
38
- find_source_pattern_keys.map { |k| k.split(/\.?#/)[0] }
39
- end
40
-
41
- # traverse hash, yielding with full key and value
42
- def traverse(path = '', hash, &block)
43
- hash.each do |k, v|
44
- if v.is_a?(Hash)
45
- traverse("#{path}.#{k}", v, &block)
46
- else
47
- block.call("#{path}.#{k}"[1..-1], v)
48
- end
49
- end
50
- end
51
-
52
- def t(hash, key)
53
- key.split('.').inject(hash) { |r, seg| r.try(:[], seg) }
54
- end
55
-
56
- def base_locale
57
- I18n.default_locale.to_s
58
- end
59
7
 
60
- def base
61
- @base ||= get_locale_data(base_locale)
62
- end
63
- end
64
8
  end
65
9
  end
@@ -1,5 +1,5 @@
1
1
  module I18n
2
2
  module Tasks
3
- VERSION = '0.0.4'
3
+ VERSION = '0.0.5'
4
4
  end
5
5
  end
@@ -45,11 +45,17 @@ describe 'rake i18n' do
45
45
  fs = {
46
46
  'config/locales/en.yml' => {'en' => en_data}.to_yaml,
47
47
  'config/locales/es.yml' => {'es' => es_data}.to_yaml,
48
+ '.i18nignore' => <<-TEXT,
49
+ ignored_missing_key.a # one key to ignore
50
+ ignored_pattern. # ignore the whole pattern
51
+ TEXT
48
52
  'app/views/index.html.slim' => <<-SLIM,
49
53
  p \#{t('ca.a')} \#{t 'ca.b'} \#{t "ca.c"}
50
54
  p \#{t 'ca.d'} \#{t 'ca.f', i: 'world'} \#{t 'ca.e', i: 'world'}
51
55
  p \#{t 'missing_in_es.a'} \#{t 'same_in_es.a'} \#{t 'blank_in_es.a'}
52
56
  p = t 'used_but_missing.a'
57
+ p = t 'ignored_missing_key.a'
58
+ p = t 'ignored_pattern.some_key'
53
59
  SLIM
54
60
  'app/controllers/events_controller.slim' => <<-RUBY,
55
61
  class EventsController < ApplicationController
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
4
+ version: 0.0.5
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-01 00:00:00.000000000 Z
11
+ date: 2013-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails