yaml-translator 0.5.0 → 0.6.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: 5387a0e1e5e1482632320ccf25455514f2c1fc96
4
- data.tar.gz: ca5121bb2cae23d58858993569ec76685b9df661
3
+ metadata.gz: 615a90101e9d6e0174bbe05202ae96270cb5aac7
4
+ data.tar.gz: 5f4087a2954b3e0225bab4c199101fe1a531fac9
5
5
  SHA512:
6
- metadata.gz: e10f2395ce53bdb9460a04e40c5e7ba21208cb15f2118f7c4575530fccd3df87cc37be0f85d558fba212edd07faf2a3d22219efc194efb9b5e01c93f77219516
7
- data.tar.gz: 16cf23202f4c212aaad45812cc53d55ca948ae501ff1596b82788c90f1515ef061e0f7832084cfc354a57ae7e073f167f3668dcca67bac1f8ef62b4a0da0aa5e
6
+ metadata.gz: 15951fd552e844646c3c138f9907a0cb1193627a9492495ce51a756751c1bbbf3d4d3aa8ea53a40d7e71927cd57f185a147dcb16feed6ff116a9d07008e35b45
7
+ data.tar.gz: fea0ff6f1d5d0d672cc04fa05c9f9bf567054721753078a56dcfa37cb9057c0530b711b669d0b16edb31a62aff815967151a004ad3eff776467acdfd36aac9fb
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- yaml-translator (0.5.0)
4
+ yaml-translator (0.6.0)
5
5
  diff-lcs
6
6
  easy_translate
7
7
 
data/README.md CHANGED
@@ -17,13 +17,14 @@ 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.load_file("#{dir}/en.yml")
21
- japanese_locale = english_locale.translate(translator, to: :ja)
20
+
21
+ english_locale = translator.file("#{dir}/en.yml")
22
+ japanese_locale = english_locale.to(:ja)
22
23
 
23
24
  p japanese_locale.to_s # convert to japanese locale yaml format
24
25
  p japanese_locale.save_to(dir) # Write a ja.yml
25
26
 
26
- german_locale = english_locale.translate(translator, to: :de)
27
+ german_locale = english_locale.to(:de)
27
28
 
28
29
  p german_locale.to_s # convert to german locale yaml format
29
30
  p german_locale.save_to(dir) # Write a de.yml
@@ -34,16 +35,12 @@ p german_locale.save_to(dir) # Write a de.yml
34
35
  The method of translating the difference is as follows.
35
36
 
36
37
  ```ruby
38
+ dir = File.dirname(__FILE__)
37
39
  adapter = ::YamlTranslator::Adapters::GoogleTranslateAdapter.new(ENV['GOOGLE_TRANSLATE_API_KEY'])
38
40
  translator = ::YamlTranslator::Translator.new(adapter)
39
41
 
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
-
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
42
+ diff_locale = translator.file("/path/to/before/en.yml").diff("/path/to/after/en.yml")
43
+ diff_locale.to(:ja).save
47
44
  ```
48
45
 
49
46
  ## Run the test
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'yaml'
4
+ require 'yaml-translator'
5
+
6
+ dir = File.dirname(__FILE__)
7
+ adapter = ::YamlTranslator::Adapters::GoogleTranslateAdapter.new(ENV['GOOGLE_TRANSLATE_API_KEY'])
8
+ translator = ::YamlTranslator::Translator.new(adapter)
9
+
10
+ diff_locale = translator.file("#{dir}/en.yml").diff("#{dir}/new.en.yml")
11
+ diff_locale.to(:ja).save_to("/tmp")
@@ -7,13 +7,13 @@ dir = File.dirname(__FILE__)
7
7
  adapter = ::YamlTranslator::Adapters::GoogleTranslateAdapter.new(ENV['GOOGLE_TRANSLATE_API_KEY'])
8
8
  translator = ::YamlTranslator::Translator.new(adapter)
9
9
 
10
- english_locale = ::YamlTranslator::Locale.load_file("#{dir}/en.yml")
11
- japanese_locale = english_locale.translate(translator, to: :ja)
10
+ english_locale = translator.file("#{dir}/en.yml")
11
+ japanese_locale = english_locale.to(:ja)
12
12
 
13
13
  p japanese_locale.to_s
14
14
  p japanese_locale.save_to(dir)
15
15
 
16
- german_locale = english_locale.translate(translator, to: :de)
16
+ german_locale = english_locale.to(:de)
17
17
 
18
18
  p german_locale.to_s
19
19
  p german_locale.save_to(dir)
@@ -0,0 +1,28 @@
1
+ module YamlTranslator
2
+ class Context
3
+ def initialize(locale, translator)
4
+ @locale = locale
5
+ @translator = translator
6
+ end
7
+ def to(lang)
8
+ @translator.translate(@locale, to: lang)
9
+ end
10
+ end
11
+
12
+ class FileContext < Context
13
+ def diff(path)
14
+ diff_locale = @locale.diff(Locale.load_file(path))
15
+ DiffContext.new(diff_locale, @translator)
16
+ end
17
+ end
18
+
19
+ class StringContext < Context
20
+ def diff(s)
21
+ diff_locale = @locale.diff(Locale.load(s))
22
+ DiffContext.new(diff_locale, @translator)
23
+ end
24
+ end
25
+
26
+ class DiffContext < Context
27
+ end
28
+ end
@@ -50,11 +50,15 @@ module YamlTranslator
50
50
  end
51
51
 
52
52
  class << self
53
- def load_file(file)
54
- lang = File.basename(file).gsub(/.(yml|yaml)/, '')
55
- yaml = YAML.load(File.open(file, &:read))
53
+ def load(s)
54
+ yaml = YAML.load(s)
55
+ lang = yaml.keys.first # FIXME check support language
56
56
  self.new(yaml, lang)
57
57
  end
58
+
59
+ def load_file(file)
60
+ load(File.open(file, &:read))
61
+ end
58
62
  end
59
63
 
60
64
  private
@@ -15,5 +15,13 @@ module YamlTranslator
15
15
  translated = @adapter.translate(locale.to_single_key_hash, options)
16
16
  Locale.new(translated.to_tree, options[:to])
17
17
  end
18
+
19
+ def string(s)
20
+ StringContext.new(Locale.load(s), self)
21
+ end
22
+
23
+ def file(f)
24
+ FileContext.new(Locale.load_file(f), self)
25
+ end
18
26
  end
19
27
  end
@@ -1,3 +1,3 @@
1
1
  module YamlTranslator
2
- VERSION = '0.5.0'
2
+ VERSION = '0.6.0'
3
3
  end
@@ -1,12 +1,17 @@
1
1
  require 'yaml-translator/version'
2
2
  require 'yaml-translator/structure'
3
3
  require 'yaml-translator/locale'
4
+ require 'yaml-translator/context'
4
5
  require 'yaml-translator/key_path'
5
6
  require 'yaml-translator/translator'
6
7
  require 'yaml-translator/adapters'
7
8
 
8
9
  module YamlTranslator
9
10
  class << self
11
+ def load(s)
12
+ Locale.load(s)
13
+ end
14
+
10
15
  def load_file(file)
11
16
  Locale.load_file(file)
12
17
  end
@@ -2,13 +2,17 @@ require 'yaml'
2
2
 
3
3
  module Helpers
4
4
  module LocaleHelper
5
+ def yaml_path(name)
6
+ "#{File.dirname(__FILE__)}/../fixtures/#{name.to_s}.yml"
7
+ end
8
+ def yaml_contents(name)
9
+ File::open(yaml_path(name), &:read)
10
+ end
5
11
  def load_yaml(name)
6
- path = "#{File.dirname(__FILE__)}/../fixtures/#{name.to_s}.yml"
7
- YAML.load(File::open(path, &:read))
12
+ YAML.load(yaml_contents(name))
8
13
  end
9
14
  def load_locale(name)
10
- path = "#{File.dirname(__FILE__)}/../fixtures/#{name.to_s}.yml"
11
- YamlTranslator::Locale.load_file(path)
15
+ YamlTranslator::Locale.load_file(yaml_path(name))
12
16
  end
13
17
  end
14
18
  end
@@ -10,6 +10,13 @@ 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 '#load' do
14
+ it 'should be load from string' do
15
+ locale = YamlTranslator::Locale.load(lang_file_content)
16
+ expect(locale.lang).to eq('en')
17
+ end
18
+ end
19
+
13
20
  describe '#diff' do
14
21
  let(:before_locale) { load_locale('diff/before/en') }
15
22
  let(:after_locale) { load_locale('diff/after/en') }
@@ -1,6 +1,6 @@
1
1
  describe YamlTranslator::Translator do
2
+ let(:translator) { YamlTranslator::Translator.new }
2
3
  describe '#translate' do
3
- let(:translator) { YamlTranslator::Translator.new }
4
4
  context 'when have root' do
5
5
  let(:yaml_tree) { load_yaml(:with_root) }
6
6
  let(:locale) { YamlTranslator::Locale.new(yaml_tree, :en) }
@@ -18,4 +18,20 @@ describe YamlTranslator::Translator do
18
18
  end
19
19
  end
20
20
  end
21
+ describe '#file' do
22
+ let(:path) { yaml_path(:en) }
23
+ let(:expected_locale) { YamlTranslator::Locale.load_file(path) }
24
+ it 'translate files' do
25
+ result = translator.file(path).to(:en)
26
+ expect(result.values).to eq(expected_locale.values)
27
+ end
28
+ end
29
+ describe '#string' do
30
+ let(:yaml_source) { yaml_contents(:en) }
31
+ let(:expected_locale) { YamlTranslator::Locale.load(yaml_source) }
32
+ it 'translate yaml string' do
33
+ result = translator.string(yaml_source).to(:en)
34
+ expect(result.values).to eq(expected_locale.values)
35
+ end
36
+ end
21
37
  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.5.0
4
+ version: 0.6.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-06 00:00:00.000000000 Z
11
+ date: 2017-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: easy_translate
@@ -153,15 +153,16 @@ files:
153
153
  - LICENSE
154
154
  - README.md
155
155
  - Rakefile
156
+ - example/diff_translate.rb
156
157
  - example/en.yml
157
- - example/google_translate.rb
158
- - example/locale.rb
159
158
  - example/new.en.yml
159
+ - example/simple.rb
160
160
  - lib/yaml-translator.rb
161
161
  - lib/yaml-translator/adapters.rb
162
162
  - lib/yaml-translator/adapters/base_adapter.rb
163
163
  - lib/yaml-translator/adapters/google_translate_adapter.rb
164
164
  - lib/yaml-translator/adapters/noop_adapter.rb
165
+ - lib/yaml-translator/context.rb
165
166
  - lib/yaml-translator/key_path.rb
166
167
  - lib/yaml-translator/locale.rb
167
168
  - lib/yaml-translator/structure.rb
data/example/locale.rb DELETED
@@ -1,13 +0,0 @@
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")