yaml-translator 0.11.1 → 0.11.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +16 -0
- data/Gemfile.lock +21 -2
- data/Rakefile +2 -2
- data/example/diff_translate.rb +2 -2
- data/lib/yaml-translator.rb +1 -1
- data/lib/yaml-translator/adapters.rb +2 -2
- data/lib/yaml-translator/adapters/base_adapter.rb +11 -11
- data/lib/yaml-translator/adapters/google_translate_adapter.rb +2 -2
- data/lib/yaml-translator/adapters/noop_adapter.rb +1 -1
- data/lib/yaml-translator/locale.rb +43 -43
- data/lib/yaml-translator/translator.rb +2 -2
- data/lib/yaml-translator/version.rb +1 -1
- data/spec/helpers/locale_helper.rb +5 -2
- data/spec/spec_helper.rb +1 -1
- data/spec/yaml-translator/adapters/google_translate_adapter_spec.rb +1 -1
- data/spec/yaml-translator/locale_spec.rb +3 -3
- data/yaml-translator.gemspec +3 -2
- metadata +18 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e3bfa580dc446bc7a0c98da5d0cc7ce74d191b35
|
4
|
+
data.tar.gz: 75427c09ee6f7749e01509990230b47a8e4aa0a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 984c67e93ed798aca92a50e4fb78738c72bda3afb8c3e470fd94003f19f21e67615a8cefac9278f765235713444abaf0ff427f43958931aba57fed2dde99206e
|
7
|
+
data.tar.gz: 1115fdb4126c618712970663bd19df037d8d796b15eb94bd4584d2c9ba88013b4d03c81ed7d54fbf88195fe12f75f3142ce46039d36deb8caa8dc0cb5d8edc9f
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
inherit_gem:
|
2
|
+
onkcop:
|
3
|
+
- "config/rubocop.yml"
|
4
|
+
# uncomment if use rails cops
|
5
|
+
# - "config/rails.yml"
|
6
|
+
# uncomment if use rspec cops
|
7
|
+
# - "config/rspec.yml"
|
8
|
+
|
9
|
+
AllCops:
|
10
|
+
TargetRubyVersion: 2.4
|
11
|
+
|
12
|
+
Style/PreferredHashMethods:
|
13
|
+
EnforcedStyle: short
|
14
|
+
|
15
|
+
Style/StringLiterals:
|
16
|
+
EnforcedStyle: single_quotes
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
yaml-translator (0.11.
|
4
|
+
yaml-translator (0.11.2)
|
5
5
|
diff-lcs
|
6
6
|
easy_translate
|
7
7
|
|
@@ -9,6 +9,7 @@ GEM
|
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
11
|
ansi (1.5.0)
|
12
|
+
ast (2.3.0)
|
12
13
|
coveralls (0.7.2)
|
13
14
|
multi_json (~> 1.3)
|
14
15
|
rest-client (= 1.6.7)
|
@@ -27,6 +28,13 @@ GEM
|
|
27
28
|
mime-types-data (~> 3.2015)
|
28
29
|
mime-types-data (3.2016.0521)
|
29
30
|
multi_json (1.12.1)
|
31
|
+
onkcop (0.47.1.2)
|
32
|
+
rubocop (~> 0.47.1)
|
33
|
+
rubocop-rspec (>= 1.12.0)
|
34
|
+
parser (2.4.0.0)
|
35
|
+
ast (~> 2.2)
|
36
|
+
powerpack (0.1.1)
|
37
|
+
rainbow (2.2.1)
|
30
38
|
rake (10.5.0)
|
31
39
|
rest-client (1.6.7)
|
32
40
|
mime-types (>= 1.16)
|
@@ -45,6 +53,15 @@ GEM
|
|
45
53
|
rspec-support (3.5.0)
|
46
54
|
rspec-temp_dir (1.0.0)
|
47
55
|
rspec (>= 3.0)
|
56
|
+
rubocop (0.47.1)
|
57
|
+
parser (>= 2.3.3.1, < 3.0)
|
58
|
+
powerpack (~> 0.1)
|
59
|
+
rainbow (>= 1.99.1, < 3.0)
|
60
|
+
ruby-progressbar (~> 1.7)
|
61
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
62
|
+
rubocop-rspec (1.13.0)
|
63
|
+
rubocop (>= 0.42.0)
|
64
|
+
ruby-progressbar (1.8.1)
|
48
65
|
simplecov (0.13.0)
|
49
66
|
docile (~> 1.1.0)
|
50
67
|
json (>= 1.8, < 3)
|
@@ -60,6 +77,7 @@ GEM
|
|
60
77
|
thread (0.2.2)
|
61
78
|
thread_safe (0.3.6)
|
62
79
|
tins (0.13.2)
|
80
|
+
unicode-display_width (1.1.3)
|
63
81
|
|
64
82
|
PLATFORMS
|
65
83
|
ruby
|
@@ -67,6 +85,7 @@ PLATFORMS
|
|
67
85
|
DEPENDENCIES
|
68
86
|
bundler (~> 1.0)
|
69
87
|
coveralls
|
88
|
+
onkcop
|
70
89
|
rake (~> 10.0)
|
71
90
|
rspec
|
72
91
|
rspec-temp_dir
|
@@ -75,4 +94,4 @@ DEPENDENCIES
|
|
75
94
|
yaml-translator!
|
76
95
|
|
77
96
|
BUNDLED WITH
|
78
|
-
1.14.
|
97
|
+
1.14.6
|
data/Rakefile
CHANGED
data/example/diff_translate.rb
CHANGED
@@ -10,9 +10,9 @@ translator = ::YamlTranslator.create(
|
|
10
10
|
|
11
11
|
diff_some_dir = File.join(File.dirname(__FILE__), 'diff/some')
|
12
12
|
diff_locale = translator.file("#{diff_some_dir}/en.yml").diff("#{diff_some_dir}/new.en.yml")
|
13
|
-
diff_locale.to(:ja).save_to(
|
13
|
+
diff_locale.to(:ja).save_to('/tmp', prefix: 'some_diff.')
|
14
14
|
|
15
15
|
# diff nothing!!
|
16
16
|
diff_none_dir = File.join(File.dirname(__FILE__), 'diff/none')
|
17
17
|
diff_locale = translator.file("#{diff_none_dir}/en.yml").diff("#{diff_none_dir}/new.en.yml")
|
18
|
-
diff_locale.to(:ja).save_to(
|
18
|
+
diff_locale.to(:ja).save_to('/tmp', prefix: 'no_diff.')
|
data/lib/yaml-translator.rb
CHANGED
@@ -5,8 +5,8 @@ require 'yaml-translator/adapters/google_translate_adapter'
|
|
5
5
|
module YamlTranslator
|
6
6
|
module Adapters
|
7
7
|
class << self
|
8
|
-
def find_and_create(name, options={})
|
9
|
-
prefix = name.to_s.split('_').map
|
8
|
+
def find_and_create(name, options = {})
|
9
|
+
prefix = name.to_s.split('_').map(&:capitalize).join('')
|
10
10
|
find_adapter_name = "#{prefix}Adapter"
|
11
11
|
raise "adapter #{find_adapter_name} is not defined" unless class_exists?(find_adapter_name)
|
12
12
|
const_get(find_adapter_name).new(options)
|
@@ -3,12 +3,12 @@ module YamlTranslator
|
|
3
3
|
class BaseAdapter
|
4
4
|
attr_reader :name
|
5
5
|
|
6
|
-
def initialize(options={})
|
6
|
+
def initialize(options = {})
|
7
7
|
@name = adapter_name
|
8
8
|
@options = options
|
9
9
|
end
|
10
10
|
|
11
|
-
def translate(values, options={})
|
11
|
+
def translate(values, options = {})
|
12
12
|
raise 'Translation processing is not implemented'
|
13
13
|
end
|
14
14
|
|
@@ -18,15 +18,15 @@ module YamlTranslator
|
|
18
18
|
|
19
19
|
private
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
21
|
+
def adapter_name
|
22
|
+
ptn = /[A-Z\s]*[^A-Z]*/
|
23
|
+
class_name = self.class.name.split('::').last
|
24
|
+
snake_case_s = class_name.gsub(/Adapter/, '').scan(ptn).
|
25
|
+
map { |v| v.empty? ? nil : v.downcase }.
|
26
|
+
compact.
|
27
|
+
join('_')
|
28
|
+
snake_case_s.to_sym
|
29
|
+
end
|
30
30
|
end
|
31
31
|
end
|
32
32
|
end
|
@@ -6,7 +6,7 @@ module YamlTranslator
|
|
6
6
|
# Translate for locale texts
|
7
7
|
# @param [Hash] locale texts of translate target
|
8
8
|
# @return [Hash] locale texts
|
9
|
-
def translate(values, options={})
|
9
|
+
def translate(values, options = {})
|
10
10
|
keys = []
|
11
11
|
texts = []
|
12
12
|
opts = default_options.merge(options)
|
@@ -15,7 +15,7 @@ module YamlTranslator
|
|
15
15
|
keys << key # 0: a.b, 1: a.b.c
|
16
16
|
texts << text # 0: a, 1: b
|
17
17
|
end
|
18
|
-
translated_texts = EasyTranslate.translate(texts, :
|
18
|
+
translated_texts = EasyTranslate.translate(texts, to: opts[:to], key: @options[:api_key])
|
19
19
|
Hash[keys.zip(translated_texts || [])]
|
20
20
|
end
|
21
21
|
end
|
@@ -19,16 +19,16 @@ module YamlTranslator
|
|
19
19
|
|
20
20
|
def initialize(config)
|
21
21
|
super(config)
|
22
|
-
@lang = config.keys.first.to_sym # FIXME check support language
|
22
|
+
@lang = config.keys.first.to_sym # FIXME: check support language
|
23
23
|
end
|
24
24
|
|
25
25
|
def locale_texts
|
26
26
|
self[lang]
|
27
27
|
end
|
28
28
|
|
29
|
-
def translate(translator, options={})
|
29
|
+
def translate(translator, options = {})
|
30
30
|
translated_values = translator.translate(compact_of(locale_texts), options)
|
31
|
-
Locale.new(Hash[
|
31
|
+
Locale.new(Hash[options[:to], tree_of(translated_values)])
|
32
32
|
end
|
33
33
|
|
34
34
|
def diff(other_locale)
|
@@ -70,12 +70,12 @@ module YamlTranslator
|
|
70
70
|
# @param dir [String] Directory path to save the file
|
71
71
|
# @param options [Hash] Options for saving
|
72
72
|
# @return int
|
73
|
-
def save(dir=Dir.pwd, options={})
|
73
|
+
def save(dir = Dir.pwd, options = {})
|
74
74
|
prefix = options[:prefix] if options.key?(:prefix)
|
75
75
|
write_file(File.join(dir, "#{prefix}#{lang}.yml"), options)
|
76
76
|
end
|
77
77
|
|
78
|
-
def save_to(dir, options={})
|
78
|
+
def save_to(dir, options = {})
|
79
79
|
save(dir, options)
|
80
80
|
end
|
81
81
|
|
@@ -88,53 +88,53 @@ module YamlTranslator
|
|
88
88
|
to_yaml
|
89
89
|
end
|
90
90
|
|
91
|
-
def to_yaml(options={})
|
92
|
-
Hash[
|
91
|
+
def to_yaml(options = {})
|
92
|
+
Hash[lang.to_s, locale_texts].to_yaml(options)
|
93
93
|
end
|
94
94
|
|
95
95
|
private
|
96
96
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
97
|
+
# Covert to a flatten hash
|
98
|
+
def compact_of(values = {}, path = KeyPath.new)
|
99
|
+
result = {}
|
100
|
+
values.each_with_index do |(i, v)|
|
101
|
+
path.move_to(i)
|
102
|
+
if v.is_a?(Hash)
|
103
|
+
result.merge!(compact_of(v, path))
|
104
|
+
else
|
105
|
+
result[path.to_s] = v
|
106
|
+
end
|
107
|
+
path.leave
|
106
108
|
end
|
107
|
-
|
109
|
+
result
|
108
110
|
end
|
109
|
-
result
|
110
|
-
end
|
111
111
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
current = result
|
119
|
-
values.each do |k, v|
|
120
|
-
keys = k.to_s.split('.')
|
121
|
-
last_key = keys.pop
|
122
|
-
keys.each do |ks|
|
123
|
-
current = if current.key?(ks)
|
124
|
-
current[ks]
|
125
|
-
else
|
126
|
-
current[ks] = {}
|
127
|
-
current[ks]
|
128
|
-
end
|
129
|
-
end
|
130
|
-
current[last_key] = v
|
112
|
+
# Returning the flattened structure to the tree structure
|
113
|
+
#
|
114
|
+
# @param [Hash] values flatten Hash
|
115
|
+
# @return [Hash] translated hash
|
116
|
+
def tree_of(values)
|
117
|
+
result = {}
|
131
118
|
current = result
|
119
|
+
values.each do |k, v|
|
120
|
+
keys = k.to_s.split('.')
|
121
|
+
last_key = keys.pop
|
122
|
+
keys.each do |ks|
|
123
|
+
current = if current.key?(ks)
|
124
|
+
current[ks]
|
125
|
+
else
|
126
|
+
current[ks] = {}
|
127
|
+
current[ks]
|
128
|
+
end
|
129
|
+
end
|
130
|
+
current[last_key] = v
|
131
|
+
current = result
|
132
|
+
end
|
133
|
+
result
|
132
134
|
end
|
133
|
-
result
|
134
|
-
end
|
135
135
|
|
136
|
-
|
137
|
-
|
138
|
-
|
136
|
+
def write_file(file_path, options = {})
|
137
|
+
File.write(file_path, to_yaml(options))
|
138
|
+
end
|
139
139
|
end
|
140
140
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'yaml-translator/adapters'
|
2
2
|
|
3
3
|
module YamlTranslator
|
4
4
|
class Translator
|
@@ -14,7 +14,7 @@ module YamlTranslator
|
|
14
14
|
#
|
15
15
|
# @param [Hash] locale texts of translate target
|
16
16
|
# @return [Hash] locale texts
|
17
|
-
def translate(locale_texts, options={})
|
17
|
+
def translate(locale_texts, options = {})
|
18
18
|
@adapter.translate(locale_texts, options)
|
19
19
|
end
|
20
20
|
|
@@ -3,14 +3,17 @@ require 'yaml'
|
|
3
3
|
module Helpers
|
4
4
|
module LocaleHelper
|
5
5
|
def yaml_path(name)
|
6
|
-
"#{File.dirname(__FILE__)}/../fixtures/#{name
|
6
|
+
"#{File.dirname(__FILE__)}/../fixtures/#{name}.yml"
|
7
7
|
end
|
8
|
+
|
8
9
|
def yaml_contents(name)
|
9
|
-
File
|
10
|
+
File.open(yaml_path(name), &:read)
|
10
11
|
end
|
12
|
+
|
11
13
|
def load_yaml(name)
|
12
14
|
YAML.load(yaml_contents(name))
|
13
15
|
end
|
16
|
+
|
14
17
|
def load_locale(name)
|
15
18
|
YamlTranslator::Locale.load_file(yaml_path(name))
|
16
19
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -9,7 +9,7 @@ describe YamlTranslator::Adapters::GoogleTranslateAdapter do
|
|
9
9
|
values
|
10
10
|
end
|
11
11
|
before do
|
12
|
-
allow(EasyTranslate).to receive(:translate).and_return(
|
12
|
+
allow(EasyTranslate).to receive(:translate).and_return(%w(a_a a_b))
|
13
13
|
end
|
14
14
|
subject { adapter.translate(some_values, to: :ja) }
|
15
15
|
it 'should be return translate results' do
|
@@ -22,7 +22,7 @@ describe YamlTranslator::Locale do
|
|
22
22
|
let(:after_locale) { load_locale('diff/after/en') }
|
23
23
|
let(:single_key_hash) do
|
24
24
|
{
|
25
|
-
'en.foo1.foo3' => 'foo1-3'
|
25
|
+
'en.foo1.foo3' => 'foo1-3',
|
26
26
|
}
|
27
27
|
end
|
28
28
|
it 'should be return flatten hash' do
|
@@ -36,9 +36,9 @@ describe YamlTranslator::Locale do
|
|
36
36
|
let(:single_key_hash) do
|
37
37
|
{
|
38
38
|
'en.bar' => 'bar',
|
39
|
-
'en.foo'=> 'foo',
|
39
|
+
'en.foo' => 'foo',
|
40
40
|
'en.foo1.foo1' => 'foo1-1',
|
41
|
-
'en.foo1.foo2' => 'foo1-2'
|
41
|
+
'en.foo1.foo2' => 'foo1-2',
|
42
42
|
}
|
43
43
|
end
|
44
44
|
it 'should be return flatten hash' do
|
data/yaml-translator.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = YamlTranslator::VERSION
|
9
9
|
spec.authors = ['Noritaka Horio']
|
10
10
|
spec.email = ['holy.shared.design@gmail.com']
|
11
|
-
spec.summary =
|
12
|
-
spec.description =
|
11
|
+
spec.summary = 'Translate for the locales configuration file'
|
12
|
+
spec.description = 'Translate for the locales configuration file'
|
13
13
|
spec.homepage = 'https://github.com/holyshared/yaml-translator'
|
14
14
|
spec.license = 'MIT'
|
15
15
|
|
@@ -28,4 +28,5 @@ Gem::Specification.new do |spec|
|
|
28
28
|
spec.add_development_dependency 'simplecov-console'
|
29
29
|
spec.add_development_dependency 'coveralls'
|
30
30
|
spec.add_development_dependency 'rspec-temp_dir'
|
31
|
+
spec.add_development_dependency 'onkcop'
|
31
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yaml-translator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.11.
|
4
|
+
version: 0.11.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Noritaka Horio
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-03-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: easy_translate
|
@@ -136,6 +136,20 @@ dependencies:
|
|
136
136
|
- - ">="
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: onkcop
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
139
153
|
description: Translate for the locales configuration file
|
140
154
|
email:
|
141
155
|
- holy.shared.design@gmail.com
|
@@ -147,6 +161,7 @@ files:
|
|
147
161
|
- ".editorconfig"
|
148
162
|
- ".gitignore"
|
149
163
|
- ".rspec"
|
164
|
+
- ".rubocop.yml"
|
150
165
|
- ".travis.yml"
|
151
166
|
- Gemfile
|
152
167
|
- Gemfile.lock
|
@@ -203,7 +218,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
203
218
|
version: '0'
|
204
219
|
requirements: []
|
205
220
|
rubyforge_project:
|
206
|
-
rubygems_version: 2.
|
221
|
+
rubygems_version: 2.6.8
|
207
222
|
signing_key:
|
208
223
|
specification_version: 4
|
209
224
|
summary: Translate for the locales configuration file
|