yaml-translator 0.3.0 → 0.4.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: a0efca74a8185ea52ad088ae15d9b4842683f05f
4
- data.tar.gz: 86e359f68864bc843576574ee02fac478182cfe8
3
+ metadata.gz: c5efedd6c0d7d7ab451309c07e4a12d2368f4ae7
4
+ data.tar.gz: 2ce47bada38eaa653ab2c08edce3802ff76c7502
5
5
  SHA512:
6
- metadata.gz: 35c49f3c52a43d8bc96edb7903e829c94e699f9457d8ad633a9befd7516989d9cb7ab08a2f5a5509743c7d2372b9f323501ba0cbe7c117452c807089e227be33
7
- data.tar.gz: adf189f897ff70ba6ec3ac9210438a0c8b19593a3890122b2cb600d19dac214f5ed1c2a66558d0a90c256e5a52af6d8e7fe98d1f3f6fdd064b13fff6208f9e1e
6
+ metadata.gz: 2e89577c0e8997d224141d13c152fcfd340d610215f245c54f63a38f46fa34fbd072a88661accebc6fb52b2ba0884de79620e3b327edce1d491df7ddadc52d64
7
+ data.tar.gz: fe40499add8f2425693a02d906f51a65878673063324cdf15f5203bf29715356afc9227dc5dba62a459e1fdb9d20b605d177d6663dbef7196fb805d2f43c4f1f
data/Gemfile.lock CHANGED
@@ -1,7 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- yaml-translator (0.3.0)
4
+ yaml-translator (0.4.0)
5
+ diff-lcs
5
6
  easy_translate
6
7
 
7
8
  GEM
data/README.md CHANGED
@@ -8,9 +8,9 @@ The translation method supports google translate api in built-in.
8
8
  [![Coverage Status](https://coveralls.io/repos/github/holyshared/yaml-translator/badge.svg?branch=master)](https://coveralls.io/github/holyshared/yaml-translator?branch=master)
9
9
  [![Code Climate](https://codeclimate.com/github/holyshared/yaml-translator/badges/gpa.svg)](https://codeclimate.com/github/holyshared/yaml-translator)
10
10
 
11
- # Basic usage
11
+ ## Basic usage
12
12
 
13
- Translate the language file, do as follows.
13
+ The method of translating the language file is as follows.
14
14
 
15
15
  ```ruby
16
16
  dir = File.dirname(__FILE__)
@@ -29,6 +29,23 @@ p german_locale.to_s # convert to german locale yaml format
29
29
  p german_locale.save_to(dir) # Write a de.yml
30
30
  ```
31
31
 
32
+ ## Translation of difference
33
+
34
+ The method of translating the difference is as follows.
35
+
36
+ ```ruby
37
+ adapter = ::YamlTranslator::Adapters::GoogleTranslateAdapter.new(ENV['GOOGLE_TRANSLATE_API_KEY'])
38
+ translator = ::YamlTranslator::Translator.new(adapter)
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")
42
+
43
+ diff_locale = before_english_locale.diff(after_english_locale)
44
+ diff_japanese_locale = diff_locale.translate(translator, to: :ja)
45
+
46
+ diff_japanese_locale.save # Save the difference translation file
47
+ ```
48
+
32
49
  ## Run the test
33
50
 
34
51
  bundle install
@@ -1,4 +1,5 @@
1
1
  require 'yaml'
2
+ require 'diff/lcs'
2
3
 
3
4
  module YamlTranslator
4
5
  class Locale
@@ -13,6 +14,18 @@ module YamlTranslator
13
14
  translator.translate(self, options)
14
15
  end
15
16
 
17
+ def diff(other)
18
+ before_seq = flatten_hash.map { |k, v| "#{k}: #{v}" }
19
+ after_seq = other.flatten_hash.map { |k, v| "#{k}: #{v}" }
20
+ diffs = Diff::LCS.diff(before_seq, after_seq).flatten.map do |operation|
21
+ type, position, element = *operation
22
+ next if type == '-'
23
+ key, text = *element.split(':')
24
+ [key, text.strip]
25
+ end
26
+ Locale.new(Hash[diffs.compact], lang)
27
+ end
28
+
16
29
  def save(dir=Dir.pwd)
17
30
  write_file(File.join(dir, "#{lang}.yml"))
18
31
  end
@@ -1,3 +1,3 @@
1
1
  module YamlTranslator
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -0,0 +1,6 @@
1
+ ---
2
+ en:
3
+ bar: bar
4
+ foo: foo
5
+ foo1:
6
+ foo3: foo1-3
@@ -0,0 +1,7 @@
1
+ ---
2
+ en:
3
+ bar: bar
4
+ foo: foo
5
+ foo1:
6
+ foo1: foo1-1
7
+ foo2: foo1-2
@@ -6,5 +6,9 @@ module Helpers
6
6
  path = "#{File.dirname(__FILE__)}/../fixtures/#{name.to_s}.yml"
7
7
  YAML.load(File::open(path, &:read))
8
8
  end
9
+ def load_locale(name)
10
+ path = "#{File.dirname(__FILE__)}/../fixtures/#{name.to_s}.yml"
11
+ YamlTranslator::Locale.load_file(path)
12
+ end
9
13
  end
10
14
  end
@@ -10,6 +10,21 @@ describe YamlTranslator::Locale do
10
10
  let(:output_file) { File.join(tmp_dir, 'en.yml') }
11
11
  let(:file_exist) { File.exist?(output_file) }
12
12
 
13
+ describe '#diff' do
14
+ let(:before_locale) { load_locale('diff/before/en') }
15
+ let(:after_locale) { load_locale('diff/after/en') }
16
+ let(:flatten_hash) do
17
+ {
18
+ 'en.foo1.foo3' => 'foo1-3'
19
+ }
20
+ end
21
+ it 'should be return flatten hash' do
22
+ diff_locale = before_locale.diff(after_locale)
23
+ expect(diff_locale.lang).to eq('en')
24
+ expect(diff_locale.flatten_hash).to eq(flatten_hash)
25
+ end
26
+ end
27
+
13
28
  describe '#flatten_hash' do
14
29
  let(:flatten_hash) do
15
30
  {
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ['lib']
20
20
 
21
21
  spec.add_dependency 'easy_translate'
22
+ spec.add_dependency 'diff-lcs'
22
23
 
23
24
  spec.add_development_dependency 'bundler', '~> 1.0'
24
25
  spec.add_development_dependency 'rake', '~> 10.0'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaml-translator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Noritaka Horio
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: diff-lcs
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -150,6 +164,8 @@ files:
150
164
  - lib/yaml-translator/locale.rb
151
165
  - lib/yaml-translator/translator.rb
152
166
  - lib/yaml-translator/version.rb
167
+ - spec/fixtures/diff/after/en.yml
168
+ - spec/fixtures/diff/before/en.yml
153
169
  - spec/fixtures/en.yml
154
170
  - spec/fixtures/no_root.yml
155
171
  - spec/fixtures/with_root.yml
@@ -183,6 +199,8 @@ signing_key:
183
199
  specification_version: 4
184
200
  summary: Translate for the locales configuration file
185
201
  test_files:
202
+ - spec/fixtures/diff/after/en.yml
203
+ - spec/fixtures/diff/before/en.yml
186
204
  - spec/fixtures/en.yml
187
205
  - spec/fixtures/no_root.yml
188
206
  - spec/fixtures/with_root.yml