i18n-tasks 0.3.11 → 0.4.0.beta1
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/.jrubyrc +2 -0
- data/.travis.yml +5 -2
- data/CHANGES.md +8 -0
- data/Gemfile +1 -9
- data/README.md +36 -3
- data/Rakefile +6 -0
- data/bin/i18n-tasks +3 -1
- data/config/locales/en.yml +4 -0
- data/config/locales/es.yml +1 -0
- data/i18n-tasks.gemspec +2 -2
- data/lib/i18n/tasks.rb +9 -5
- data/lib/i18n/tasks/base_task.rb +2 -4
- data/lib/i18n/tasks/commands.rb +9 -0
- data/lib/i18n/tasks/data.rb +77 -60
- data/lib/i18n/tasks/data/file_formats.rb +3 -0
- data/lib/i18n/tasks/data/file_system_base.rb +53 -30
- data/lib/i18n/tasks/data/router/conservative_router.rb +51 -0
- data/lib/i18n/tasks/data/router/pattern_router.rb +57 -0
- data/lib/i18n/tasks/data/tree/node.rb +199 -0
- data/lib/i18n/tasks/data/tree/nodes.rb +85 -0
- data/lib/i18n/tasks/data/tree/siblings.rb +131 -0
- data/lib/i18n/tasks/data/tree/traversal.rb +81 -0
- data/lib/i18n/tasks/file_structure.rb +18 -0
- data/lib/i18n/tasks/missing_keys.rb +7 -7
- data/lib/i18n/tasks/plural_keys.rb +5 -5
- data/lib/i18n/tasks/unused_keys.rb +5 -6
- data/lib/i18n/tasks/version.rb +1 -1
- data/spec/conservative_router_spec.rb +50 -0
- data/spec/file_system_data_spec.rb +5 -4
- data/spec/google_translate_spec.rb +2 -2
- data/spec/i18n_tasks_spec.rb +1 -1
- data/spec/locale_tree/siblings_spec.rb +29 -0
- data/spec/plural_keys_spec.rb +3 -2
- data/spec/spec_helper.rb +1 -0
- data/spec/support/trees.rb +5 -0
- metadata +25 -11
- data/lib/i18n/tasks/data/locale_tree.rb +0 -86
- data/lib/i18n/tasks/data/router.rb +0 -47
@@ -1,86 +0,0 @@
|
|
1
|
-
module I18n::Tasks
|
2
|
-
module Data
|
3
|
-
# A tree of keys. Roots are locales, leaves are values.
|
4
|
-
class LocaleTree
|
5
|
-
attr_reader :locale, :data
|
6
|
-
|
7
|
-
def initialize(locale, data = {})
|
8
|
-
@locale = locale.to_s
|
9
|
-
@data = to_tree_data(data)
|
10
|
-
end
|
11
|
-
|
12
|
-
def merge(other)
|
13
|
-
self.class.new locale, data.deep_merge(to_tree_data(other))
|
14
|
-
end
|
15
|
-
|
16
|
-
alias + merge
|
17
|
-
|
18
|
-
def to_hash
|
19
|
-
{locale => data}
|
20
|
-
end
|
21
|
-
|
22
|
-
# @return [String,nil] translation of the key found in the passed hash or nil
|
23
|
-
def t(key)
|
24
|
-
key.to_s.split('.').inject(data) { |r, seg| r[seg] if r }
|
25
|
-
end
|
26
|
-
|
27
|
-
# traverse => map if yield(k, v)
|
28
|
-
# @return [Array] mapped list
|
29
|
-
def traverse_map_if
|
30
|
-
list = []
|
31
|
-
traverse do |k, v|
|
32
|
-
mapped = yield(k, v)
|
33
|
-
list << mapped if mapped
|
34
|
-
end
|
35
|
-
list
|
36
|
-
end
|
37
|
-
|
38
|
-
# traverse data, yielding with full key and value
|
39
|
-
# @yield [full_key, value]
|
40
|
-
# @return self
|
41
|
-
def traverse
|
42
|
-
q = [['', data]]
|
43
|
-
until q.empty?
|
44
|
-
path, value = q.pop
|
45
|
-
if value.is_a?(Hash)
|
46
|
-
value.each { |k, v| q << ["#{path}.#{k}", v] }
|
47
|
-
else
|
48
|
-
yield path[1..-1], value
|
49
|
-
end
|
50
|
-
end
|
51
|
-
self
|
52
|
-
end
|
53
|
-
|
54
|
-
def to_tree_data(arg)
|
55
|
-
self.class.to_tree_data(arg)
|
56
|
-
end
|
57
|
-
|
58
|
-
class << self
|
59
|
-
def to_tree_data(any)
|
60
|
-
if any.is_a?(Hash)
|
61
|
-
any
|
62
|
-
elsif any.is_a?(Array)
|
63
|
-
list_to_tree_data any
|
64
|
-
elsif any.is_a?(LocaleTree)
|
65
|
-
any.data
|
66
|
-
else
|
67
|
-
raise "Can't get tree data from #{any.inspect}"
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
def list_to_tree_data(list)
|
72
|
-
key_values = list.sort_by(&:first)
|
73
|
-
tree_data = {}
|
74
|
-
key_values.each do |key, value|
|
75
|
-
key_segments = key.to_s.split('.')
|
76
|
-
node = key_segments[0..-2].inject(tree_data) do |subtree, seg|
|
77
|
-
subtree[seg] ||= {}
|
78
|
-
end
|
79
|
-
node[key_segments.last] = value
|
80
|
-
end
|
81
|
-
tree_data
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
require 'i18n/tasks/data/locale_tree'
|
2
|
-
|
3
|
-
module I18n::Tasks
|
4
|
-
module Data
|
5
|
-
module Router
|
6
|
-
include ::I18n::Tasks::KeyPatternMatching
|
7
|
-
|
8
|
-
def compile_routes(routes)
|
9
|
-
routes.map { |x| x.is_a?(String) ? ['*', x] : x }.map { |x|
|
10
|
-
[compile_key_pattern(x[0]), x[1]]
|
11
|
-
}
|
12
|
-
end
|
13
|
-
|
14
|
-
# Route keys to destinations
|
15
|
-
# @param routes [Array] of routes
|
16
|
-
# @example
|
17
|
-
# # keys matched top to bottom
|
18
|
-
# [['devise.*', 'config/locales/devise.%{locale}.yml'],
|
19
|
-
# # default catch-all (same as ['*', 'config/locales/%{locale}.yml'])
|
20
|
-
# 'config/locales/%{locale}.yml']
|
21
|
-
# @param tree [I18n::Tasks::Data::Tree] locale tree, where roots are locales.
|
22
|
-
# @param route_args [Hash] route arguments, %-interpolated
|
23
|
-
# @return [Hash] mapping of destination => [ [key, value], ... ]
|
24
|
-
def route_values(routes, values, locale, &block)
|
25
|
-
out = {}
|
26
|
-
values = LocaleTree.new(locale, values) unless values.is_a?(LocaleTree)
|
27
|
-
values.traverse do |key, value|
|
28
|
-
route = routes.detect { |route| route[0] =~ key }
|
29
|
-
key_match = $~
|
30
|
-
path = route[1] % {locale: locale}
|
31
|
-
path.gsub!(/[\\]\d+/) { |m| key_match[m[1..-1].to_i] }
|
32
|
-
(out[path] ||= []) << [key, value]
|
33
|
-
end
|
34
|
-
out.each do |dest, key_values|
|
35
|
-
out[dest] = LocaleTree.new(locale, key_values)
|
36
|
-
end
|
37
|
-
if block
|
38
|
-
out.each(&block)
|
39
|
-
else
|
40
|
-
out
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|