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 +4 -4
- data/Gemfile.lock +2 -1
- data/README.md +19 -2
- data/lib/yaml-translator/locale.rb +13 -0
- data/lib/yaml-translator/version.rb +1 -1
- data/spec/fixtures/diff/after/en.yml +6 -0
- data/spec/fixtures/diff/before/en.yml +7 -0
- data/spec/helpers/locale_helper.rb +4 -0
- data/spec/yaml-translator/locale_spec.rb +15 -0
- data/yaml-translator.gemspec +1 -0
- metadata +19 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c5efedd6c0d7d7ab451309c07e4a12d2368f4ae7
|
4
|
+
data.tar.gz: 2ce47bada38eaa653ab2c08edce3802ff76c7502
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2e89577c0e8997d224141d13c152fcfd340d610215f245c54f63a38f46fa34fbd072a88661accebc6fb52b2ba0884de79620e3b327edce1d491df7ddadc52d64
|
7
|
+
data.tar.gz: fe40499add8f2425693a02d906f51a65878673063324cdf15f5203bf29715356afc9227dc5dba62a459e1fdb9d20b605d177d6663dbef7196fb805d2f43c4f1f
|
data/Gemfile.lock
CHANGED
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
|
-
|
11
|
+
## Basic usage
|
12
12
|
|
13
|
-
|
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
|
@@ -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
|
{
|
data/yaml-translator.gemspec
CHANGED
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.
|
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
|