yaml-translator 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c5efedd6c0d7d7ab451309c07e4a12d2368f4ae7
4
- data.tar.gz: 2ce47bada38eaa653ab2c08edce3802ff76c7502
3
+ metadata.gz: 5387a0e1e5e1482632320ccf25455514f2c1fc96
4
+ data.tar.gz: ca5121bb2cae23d58858993569ec76685b9df661
5
5
  SHA512:
6
- metadata.gz: 2e89577c0e8997d224141d13c152fcfd340d610215f245c54f63a38f46fa34fbd072a88661accebc6fb52b2ba0884de79620e3b327edce1d491df7ddadc52d64
7
- data.tar.gz: fe40499add8f2425693a02d906f51a65878673063324cdf15f5203bf29715356afc9227dc5dba62a459e1fdb9d20b605d177d6663dbef7196fb805d2f43c4f1f
6
+ metadata.gz: e10f2395ce53bdb9460a04e40c5e7ba21208cb15f2118f7c4575530fccd3df87cc37be0f85d558fba212edd07faf2a3d22219efc194efb9b5e01c93f77219516
7
+ data.tar.gz: 16cf23202f4c212aaad45812cc53d55ca948ae501ff1596b82788c90f1515ef061e0f7832084cfc354a57ae7e073f167f3668dcca67bac1f8ef62b4a0da0aa5e
@@ -2,7 +2,6 @@ language: ruby
2
2
  env:
3
3
  secure: "OPbJVv+xHdc56rzLDDpBiPsVtQ6Gmc7k4373BaZArjJbB1Dx34FHBNG0UoyF2XUKXVRs5TWgwbs9M4MDM9U13hv2VozT8/RaOkH48d2eHQ93vJX4P8vfR68AA73TV+LtJ7ktlkPp0JLJ54yfHVmANyOxRv2MIDSGGxg+m/FdaTssded4v0YF2dQSKCwfokwyVSCtAx8dsP0Q5UbOYKRDyjn5MR8xZWWiB9wmxuyTnhtKSyGASb+hO4aHll7pTYhebz1NViQhz0vB8BBP5AWdoi5LsToHi2obhu3+5f18pEfqtraJQJ2wa8ix3VaTuL5z1FHfxw2Etq9Eooa3sbrxvQEisSX+JpCPrxBNy4S4GcpXcc8Zg4b2IhLIww6EpDnOxod85N6Q1EzYvo5URxdM5bYVBNuFLjeVg8BSFGGYfebR/Xx4Zh5vDVWJlfkc7fbpGV4IGKUMHhTVSyT19TwTnJnKcahVZO9lChZFPj+SkTFp+SsdReJg3xnIOr+AuPuxqn2n9PPMMyvQng3VxWog7a2SP8Db6VTY5A1JPpOJXVEsNkHwqiUuE3nNLp6WFIXbYIzmJyYFmm5ij7aoaK/gcvDNHoGz/cPZrW+juIXKJC24Qk8hXrZPzEO54+e+M6qsR3p8TlpN5TSxPoeO0hET2cMX0xHFOHU+UnFDx1oTC0c="
4
4
  rvm:
5
- - 2.0.0
6
5
  - 2.1
7
6
  - 2.2
8
7
  - 2.3.0
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- yaml-translator (0.4.0)
4
+ yaml-translator (0.5.0)
5
5
  diff-lcs
6
6
  easy_translate
7
7
 
data/README.md CHANGED
@@ -17,7 +17,7 @@ dir = File.dirname(__FILE__)
17
17
  adapter = ::YamlTranslator::Adapters::GoogleTranslateAdapter.new(ENV['GOOGLE_TRANSLATE_API_KEY'])
18
18
  translator = ::YamlTranslator::Translator.new(adapter)
19
19
 
20
- english_locale = ::YamlTranslator::Locale.load_file("#{dir}/en.yml")
20
+ english_locale = ::YamlTranslator.load_file("#{dir}/en.yml")
21
21
  japanese_locale = english_locale.translate(translator, to: :ja)
22
22
 
23
23
  p japanese_locale.to_s # convert to japanese locale yaml format
@@ -37,8 +37,8 @@ The method of translating the difference is as follows.
37
37
  adapter = ::YamlTranslator::Adapters::GoogleTranslateAdapter.new(ENV['GOOGLE_TRANSLATE_API_KEY'])
38
38
  translator = ::YamlTranslator::Translator.new(adapter)
39
39
 
40
- before_english_locale = ::YamlTranslator::Locale.load_file("/path/to/before/en.yml")
41
- after_english_locale = ::YamlTranslator::Locale.load_file("/path/to/after/en.yml")
40
+ before_english_locale = ::YamlTranslator.load_file("/path/to/before/en.yml")
41
+ after_english_locale = ::YamlTranslator.load_file("/path/to/after/en.yml")
42
42
 
43
43
  diff_locale = before_english_locale.diff(after_english_locale)
44
44
  diff_japanese_locale = diff_locale.translate(translator, to: :ja)
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'yaml'
4
+ require 'yaml-translator'
5
+
6
+ dir = File.dirname(__FILE__)
7
+ english_locale1 = ::YamlTranslator.load_file("#{dir}/en.yml")
8
+ english_locale2 = ::YamlTranslator.load_file("#{dir}/new.en.yml")
9
+
10
+ diff_locale = english_locale1.diff(english_locale2)
11
+
12
+ merged_locale = english_locale1.merge(diff_locale)
13
+ merged_locale.save_to("/tmp")
@@ -0,0 +1,3 @@
1
+ en:
2
+ hello_world: 'Hello would'
3
+ hello_new_world: 'Hello would'
@@ -1,5 +1,14 @@
1
1
  require 'yaml-translator/version'
2
+ require 'yaml-translator/structure'
2
3
  require 'yaml-translator/locale'
3
4
  require 'yaml-translator/key_path'
4
5
  require 'yaml-translator/translator'
5
6
  require 'yaml-translator/adapters'
7
+
8
+ module YamlTranslator
9
+ class << self
10
+ def load_file(file)
11
+ Locale.load_file(file)
12
+ end
13
+ end
14
+ end
@@ -3,6 +3,7 @@ require 'diff/lcs'
3
3
 
4
4
  module YamlTranslator
5
5
  class Locale
6
+
6
7
  attr_reader :lang, :values
7
8
 
8
9
  def initialize(values, lang)
@@ -15,15 +16,21 @@ module YamlTranslator
15
16
  end
16
17
 
17
18
  def diff(other)
18
- before_seq = flatten_hash.map { |k, v| "#{k}: #{v}" }
19
- after_seq = other.flatten_hash.map { |k, v| "#{k}: #{v}" }
19
+ before_seq = to_single_key_hash.map { |k, v| "#{k}: #{v}" }
20
+ after_seq = other.to_single_key_hash.map { |k, v| "#{k}: #{v}" }
20
21
  diffs = Diff::LCS.diff(before_seq, after_seq).flatten.map do |operation|
21
22
  type, position, element = *operation
22
23
  next if type == '-'
23
24
  key, text = *element.split(':')
24
25
  [key, text.strip]
25
26
  end
26
- Locale.new(Hash[diffs.compact], lang)
27
+ single_key_hash = Hash[diffs.compact]
28
+ Locale.new(single_key_hash.to_tree, lang)
29
+ end
30
+
31
+ def merge(locale)
32
+ merged = flatten_hash.merge(locale.flatten_hash)
33
+ Locale.new(merged.to_tree, lang)
27
34
  end
28
35
 
29
36
  def save(dir=Dir.pwd)
@@ -34,8 +41,8 @@ module YamlTranslator
34
41
  save(dir)
35
42
  end
36
43
 
37
- def flatten_hash
38
- flatten(values)
44
+ def to_single_key_hash
45
+ values.to_single_key
39
46
  end
40
47
 
41
48
  def to_s
@@ -52,21 +59,6 @@ module YamlTranslator
52
59
 
53
60
  private
54
61
 
55
- # Covert to a flatten hash
56
- def flatten(values={}, path=KeyPath.new)
57
- result = {}
58
- values.each_with_index do |(i, v)|
59
- path.move_to(i)
60
- if v.is_a?(Hash)
61
- result.merge!(flatten(v, path))
62
- else
63
- result[path.to_s] = v
64
- end
65
- path.leave
66
- end
67
- result
68
- end
69
-
70
62
  def write_file(file_path)
71
63
  File.write(file_path, to_s)
72
64
  end
@@ -0,0 +1,5 @@
1
+ require 'yaml-translator/structure/tree'
2
+ require 'yaml-translator/structure/single_key'
3
+
4
+ Hash.include YamlTranslator::Structure::Tree
5
+ Hash.include YamlTranslator::Structure::SingleKey
@@ -0,0 +1,27 @@
1
+ module YamlTranslator
2
+ module Structure
3
+ module SingleKey
4
+ # Covert to a flatten hash
5
+ def to_single_key
6
+ compact_of(self, KeyPath.new)
7
+ end
8
+
9
+ private
10
+
11
+ # Covert to a flatten hash
12
+ def compact_of(values={}, path=KeyPath.new)
13
+ result = {}
14
+ values.each_with_index do |(i, v)|
15
+ path.move_to(i)
16
+ if v.is_a?(Hash)
17
+ result.merge!(compact_of(v, path))
18
+ else
19
+ result[path.to_s] = v
20
+ end
21
+ path.leave
22
+ end
23
+ result
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,35 @@
1
+ module YamlTranslator
2
+ module Structure
3
+ module Tree
4
+ def to_tree
5
+ tree_of(self)
6
+ end
7
+
8
+ private
9
+
10
+ # Returning the flattened structure to the tree structure
11
+ #
12
+ # @param [Hash] values flatten Hash
13
+ # @return [Hash] translated hash
14
+ def tree_of(values)
15
+ result = {}
16
+ current = result
17
+ values.each do |k, v|
18
+ keys = k.split('.')
19
+ last_key = keys.pop
20
+ keys.each do |ks|
21
+ current = if current.key?(ks)
22
+ current[ks]
23
+ else
24
+ current[ks] = {}
25
+ current[ks]
26
+ end
27
+ end
28
+ current[last_key] = v
29
+ current = result
30
+ end
31
+ result
32
+ end
33
+ end
34
+ end
35
+ end
@@ -2,6 +2,7 @@ require "yaml-translator/adapters"
2
2
 
3
3
  module YamlTranslator
4
4
  class Translator
5
+
5
6
  def initialize(adapter = Adapters::NoopAdaptor.new)
6
7
  @adapter = adapter
7
8
  end
@@ -11,34 +12,8 @@ module YamlTranslator
11
12
  # @param [Locale] locale of translate target
12
13
  # @return [Locale] locale
13
14
  def translate(locale, options={})
14
- translated = @adapter.translate(locale.flatten_hash, options)
15
- Locale.new(rebuild(translated), options[:to])
16
- end
17
-
18
- private
19
-
20
- # Returning the flattened structure to the tree structure
21
- #
22
- # @param [Hash] values flatten Hash
23
- # @return [Hash] translated hash
24
- def rebuild(values)
25
- result = {}
26
- current = result
27
- values.each do |k, v|
28
- keys = k.split('.')
29
- last_key = keys.pop
30
- keys.each do |ks|
31
- current = if current.key?(ks)
32
- current[ks]
33
- else
34
- current[ks] = {}
35
- current[ks]
36
- end
37
- end
38
- current[last_key] = v
39
- current = result
40
- end
41
- result
15
+ translated = @adapter.translate(locale.to_single_key_hash, options)
16
+ Locale.new(translated.to_tree, options[:to])
42
17
  end
43
18
  end
44
19
  end
@@ -1,3 +1,3 @@
1
1
  module YamlTranslator
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
@@ -13,7 +13,7 @@ describe YamlTranslator::Locale do
13
13
  describe '#diff' do
14
14
  let(:before_locale) { load_locale('diff/before/en') }
15
15
  let(:after_locale) { load_locale('diff/after/en') }
16
- let(:flatten_hash) do
16
+ let(:single_key_hash) do
17
17
  {
18
18
  'en.foo1.foo3' => 'foo1-3'
19
19
  }
@@ -21,12 +21,12 @@ describe YamlTranslator::Locale do
21
21
  it 'should be return flatten hash' do
22
22
  diff_locale = before_locale.diff(after_locale)
23
23
  expect(diff_locale.lang).to eq('en')
24
- expect(diff_locale.flatten_hash).to eq(flatten_hash)
24
+ expect(diff_locale.to_single_key_hash).to eq(single_key_hash)
25
25
  end
26
26
  end
27
27
 
28
- describe '#flatten_hash' do
29
- let(:flatten_hash) do
28
+ describe '#to_single_key_hash' do
29
+ let(:single_key_hash) do
30
30
  {
31
31
  'en.bar' => 'bar',
32
32
  'en.foo'=> 'foo',
@@ -35,7 +35,7 @@ describe YamlTranslator::Locale do
35
35
  }
36
36
  end
37
37
  it 'should be return flatten hash' do
38
- expect(locale.flatten_hash).to eq(flatten_hash)
38
+ expect(locale.to_single_key_hash).to eq(single_key_hash)
39
39
  end
40
40
  end
41
41
  describe '#to_s' do
@@ -0,0 +1,8 @@
1
+ describe YamlTranslator::Structure::SingleKey do
2
+ let(:tree) { { en: { foo: 'bar' } } }
3
+ describe '#to_single_key' do
4
+ it 'should be return single key hash' do
5
+ expect(tree.to_single_key).to eq({ 'en.foo' => 'bar' })
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ describe YamlTranslator::Structure::Tree do
2
+ let(:single_key) { { 'en.foo' => 'bar' } }
3
+ describe '#to_tree' do
4
+ it 'should be return tree hash' do
5
+ expect(single_key.to_tree.to_s).to eq({ 'en' => { 'foo' => 'bar' } }.to_s)
6
+ end
7
+ end
8
+ 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.4.0
4
+ version: 0.5.0
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-02-05 00:00:00.000000000 Z
11
+ date: 2017-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: easy_translate
@@ -155,6 +155,8 @@ files:
155
155
  - Rakefile
156
156
  - example/en.yml
157
157
  - example/google_translate.rb
158
+ - example/locale.rb
159
+ - example/new.en.yml
158
160
  - lib/yaml-translator.rb
159
161
  - lib/yaml-translator/adapters.rb
160
162
  - lib/yaml-translator/adapters/base_adapter.rb
@@ -162,6 +164,9 @@ files:
162
164
  - lib/yaml-translator/adapters/noop_adapter.rb
163
165
  - lib/yaml-translator/key_path.rb
164
166
  - lib/yaml-translator/locale.rb
167
+ - lib/yaml-translator/structure.rb
168
+ - lib/yaml-translator/structure/single_key.rb
169
+ - lib/yaml-translator/structure/tree.rb
165
170
  - lib/yaml-translator/translator.rb
166
171
  - lib/yaml-translator/version.rb
167
172
  - spec/fixtures/diff/after/en.yml
@@ -172,6 +177,8 @@ files:
172
177
  - spec/helpers/locale_helper.rb
173
178
  - spec/spec_helper.rb
174
179
  - spec/yaml-translator/locale_spec.rb
180
+ - spec/yaml-translator/structure/single_key.rb
181
+ - spec/yaml-translator/structure/tree_spec.rb
175
182
  - spec/yaml-translator/translator_spec.rb
176
183
  - yaml-translator.gemspec
177
184
  homepage: https://github.com/holyshared/yaml-translator
@@ -207,4 +214,6 @@ test_files:
207
214
  - spec/helpers/locale_helper.rb
208
215
  - spec/spec_helper.rb
209
216
  - spec/yaml-translator/locale_spec.rb
217
+ - spec/yaml-translator/structure/single_key.rb
218
+ - spec/yaml-translator/structure/tree_spec.rb
210
219
  - spec/yaml-translator/translator_spec.rb