i18n_tools 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/i18n_tools/missing_scanner.rb +42 -11
  2. metadata +2 -2
@@ -21,21 +21,25 @@ module I18nTools
21
21
  current_hash = current_hash[part]
22
22
  end
23
23
 
24
- if params.blank?
25
- content = ""
26
- else
27
- keys = []
24
+ content = suggestion_for_key(parts.last) || ""
25
+
26
+ if content.blank?
27
+ if params.blank?
28
+ content = ""
29
+ else
30
+ keys = []
28
31
 
29
- parsed = params.split(",").map {|x|x.scan(/(:[a-zA-Z0-9_]+)\s*=>\s*(.*)/)}
30
- parsed.each do |param|
31
- next if param.blank?
32
+ parsed = params.split(",").map {|x|x.scan(/(:[a-zA-Z0-9_]+)\s*=>\s*(.*)/)}
33
+ parsed.each do |param|
34
+ next if param.blank?
32
35
 
33
- key, value = *param.flatten
36
+ key, value = *param.flatten
34
37
 
35
- keys << "%{#{key.sub(/^:/, '')}} (#{value})"
36
- end
38
+ keys << "%{#{key.sub(/^:/, '')}} (#{value})"
39
+ end
37
40
 
38
- content = keys.join(", ")
41
+ content = keys.join(", ")
42
+ end
39
43
  end
40
44
 
41
45
  current_hash[parts.last] = content
@@ -43,5 +47,32 @@ module I18nTools
43
47
 
44
48
  result
45
49
  end
50
+
51
+ def suggestion_for_key(key)
52
+ root = I18n.backend.send(:lookup, @locale, "")
53
+
54
+ candidates = find_key(key.to_sym, root)
55
+
56
+ most_common_value(candidates) unless candidates.empty?
57
+ end
58
+
59
+ private
60
+ def find_key(key, hash)
61
+ if hash[key] && hash[key].is_a?(String)
62
+ [hash[key]]
63
+ else
64
+ result = []
65
+ hash.each do |_, value|
66
+ result += find_key(key, value) if value.is_a?(Hash)
67
+ end
68
+ result
69
+ end
70
+ end
71
+
72
+ def most_common_value(a)
73
+ a.group_by do |e|
74
+ e
75
+ end.values.max_by(&:size).first
76
+ end
46
77
  end
47
78
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-11 00:00:00.000000000 Z
12
+ date: 2013-03-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ya2yaml