yaml-translator 0.6.0 → 0.7.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: 615a90101e9d6e0174bbe05202ae96270cb5aac7
4
- data.tar.gz: 5f4087a2954b3e0225bab4c199101fe1a531fac9
3
+ metadata.gz: 91450cec2cf7f90ca1dbe8ec5b949bd7a3bd48b1
4
+ data.tar.gz: 84b3a8d5ef741ea905b9a3cf405b5fc1057a02be
5
5
  SHA512:
6
- metadata.gz: 15951fd552e844646c3c138f9907a0cb1193627a9492495ce51a756751c1bbbf3d4d3aa8ea53a40d7e71927cd57f185a147dcb16feed6ff116a9d07008e35b45
7
- data.tar.gz: fea0ff6f1d5d0d672cc04fa05c9f9bf567054721753078a56dcfa37cb9057c0530b711b669d0b16edb31a62aff815967151a004ad3eff776467acdfd36aac9fb
6
+ metadata.gz: 8c0f956835f849caaef6fe3a4e391043c26811071d567f88a11ebf1e8091f80cbce0e5d2ed45a58d29d90974622a35e113d64e6e6f99fef68c42886cce562d61
7
+ data.tar.gz: 752d7fbb490da950d890990f6e5dfb190170d031d3709036e0822915e22fb3cb2b1ebbbc92fb73864663baaad6aac62c0ce16e6c8035c998d0a092cbff19bdec
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- yaml-translator (0.6.0)
4
+ yaml-translator (0.7.0)
5
5
  diff-lcs
6
6
  easy_translate
7
7
 
data/README.md CHANGED
@@ -14,9 +14,11 @@ The method of translating the language file is as follows.
14
14
 
15
15
  ```ruby
16
16
  dir = File.dirname(__FILE__)
17
- adapter = ::YamlTranslator::Adapters::GoogleTranslateAdapter.new(ENV['GOOGLE_TRANSLATE_API_KEY'])
18
- translator = ::YamlTranslator::Translator.new(adapter)
19
17
 
18
+ translator = ::YamlTranslator.create(
19
+ :google_translate,
20
+ api_key: ENV['GOOGLE_TRANSLATE_API_KEY']
21
+ )
20
22
 
21
23
  english_locale = translator.file("#{dir}/en.yml")
22
24
  japanese_locale = english_locale.to(:ja)
@@ -36,8 +38,11 @@ The method of translating the difference is as follows.
36
38
 
37
39
  ```ruby
38
40
  dir = File.dirname(__FILE__)
39
- adapter = ::YamlTranslator::Adapters::GoogleTranslateAdapter.new(ENV['GOOGLE_TRANSLATE_API_KEY'])
40
- translator = ::YamlTranslator::Translator.new(adapter)
41
+
42
+ translator = ::YamlTranslator.create(
43
+ :google_translate,
44
+ api_key: ENV['GOOGLE_TRANSLATE_API_KEY']
45
+ )
41
46
 
42
47
  diff_locale = translator.file("/path/to/before/en.yml").diff("/path/to/after/en.yml")
43
48
  diff_locale.to(:ja).save
@@ -4,8 +4,10 @@ require 'yaml'
4
4
  require 'yaml-translator'
5
5
 
6
6
  dir = File.dirname(__FILE__)
7
- adapter = ::YamlTranslator::Adapters::GoogleTranslateAdapter.new(ENV['GOOGLE_TRANSLATE_API_KEY'])
8
- translator = ::YamlTranslator::Translator.new(adapter)
7
+ translator = ::YamlTranslator.create(
8
+ :google_translate,
9
+ api_key: ENV['GOOGLE_TRANSLATE_API_KEY']
10
+ )
9
11
 
10
12
  diff_locale = translator.file("#{dir}/en.yml").diff("#{dir}/new.en.yml")
11
13
  diff_locale.to(:ja).save_to("/tmp")
data/example/simple.rb CHANGED
@@ -4,8 +4,10 @@ require 'yaml'
4
4
  require 'yaml-translator'
5
5
 
6
6
  dir = File.dirname(__FILE__)
7
- adapter = ::YamlTranslator::Adapters::GoogleTranslateAdapter.new(ENV['GOOGLE_TRANSLATE_API_KEY'])
8
- translator = ::YamlTranslator::Translator.new(adapter)
7
+ translator = ::YamlTranslator.create(
8
+ :google_translate,
9
+ api_key: ENV['GOOGLE_TRANSLATE_API_KEY']
10
+ )
9
11
 
10
12
  english_locale = translator.file("#{dir}/en.yml")
11
13
  japanese_locale = english_locale.to(:ja)
@@ -3,8 +3,8 @@ require 'easy_translate'
3
3
  module YamlTranslator
4
4
  module Adapters
5
5
  class GoogleTranslateAdapter < BaseAdapter
6
- def initialize(api_key=nil)
7
- @api_key = api_key
6
+ def initialize(options={})
7
+ @options = options
8
8
  end
9
9
  def translate(values, options={})
10
10
  keys = []
@@ -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, :to => opts[:to], :key => @api_key)
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
@@ -1,3 +1,22 @@
1
1
  require 'yaml-translator/adapters/base_adapter'
2
2
  require 'yaml-translator/adapters/noop_adapter'
3
3
  require 'yaml-translator/adapters/google_translate_adapter'
4
+
5
+ module YamlTranslator
6
+ module Adapters
7
+ class << self
8
+ def find_and_create(name, options={})
9
+ prefix = name.to_s.split('_').map { |v| v.capitalize }.join('')
10
+ find_adapter_name = "#{prefix}Adapter"
11
+ raise "adapter #{find_adapter_name} is not defined" unless class_exists?(find_adapter_name)
12
+ const_get(find_adapter_name).new(options)
13
+ end
14
+
15
+ private
16
+
17
+ def class_exists?(class_name)
18
+ const_defined?(class_name) && const_get(class_name).is_a?(Class)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -29,8 +29,23 @@ module YamlTranslator
29
29
  end
30
30
 
31
31
  def merge(locale)
32
- merged = flatten_hash.merge(locale.flatten_hash)
33
- Locale.new(merged.to_tree, lang)
32
+ s = to_single_key_hash
33
+ o = locale.to_single_key_hash
34
+ Locale.new(s.merge(o).to_tree, lang)
35
+ end
36
+
37
+ def merge_to(locale)
38
+ locale.merge(self)
39
+ end
40
+
41
+ def merge_to_file(file)
42
+ target_locale = Locale.load_file(file)
43
+ target_locale.merge(self)
44
+ end
45
+
46
+ def merge_to_s(s)
47
+ target_locale = Locale.load(s)
48
+ target_locale.merge(self)
34
49
  end
35
50
 
36
51
  def save(dir=Dir.pwd)
@@ -1,3 +1,3 @@
1
1
  module YamlTranslator
2
- VERSION = '0.6.0'
2
+ VERSION = '0.7.0'
3
3
  end
@@ -15,5 +15,10 @@ module YamlTranslator
15
15
  def load_file(file)
16
16
  Locale.load_file(file)
17
17
  end
18
+
19
+ def create(name, options={})
20
+ adapter = Adapters.find_and_create(name, options)
21
+ Translator.new(adapter)
22
+ end
18
23
  end
19
24
  end
@@ -0,0 +1,3 @@
1
+ ---
2
+ en:
3
+ target: bar
@@ -45,6 +45,22 @@ describe YamlTranslator::Locale do
45
45
  expect(locale.to_single_key_hash).to eq(single_key_hash)
46
46
  end
47
47
  end
48
+
49
+ describe '#merge_to_file' do
50
+ let(:merge_target_file) { File.join(File.dirname(__FILE__), '/../fixtures/merge_target.yml') }
51
+ it 'should be merge to yaml file' do
52
+ merged = locale.merge_to_file(merge_target_file)
53
+ expect(merged.values['en']['target']).to eq('bar')
54
+ end
55
+ end
56
+
57
+ describe '#merge_to_s' do
58
+ it 'should be merge to yaml formatted string' do
59
+ merged = locale.merge_to_s("---\nen:\n merge_to_s: ok?")
60
+ expect(merged.values['en']['merge_to_s']).to eq('ok?')
61
+ end
62
+ end
63
+
48
64
  describe '#to_s' do
49
65
  it 'should be return yaml formatted string' do
50
66
  expect(locale.to_s).to eq(lang_file_content)
@@ -0,0 +1,10 @@
1
+ require 'rspec/temp_dir'
2
+
3
+ describe YamlTranslator do
4
+ describe '#create' do
5
+ it 'should be load translator' do
6
+ translator = YamlTranslator.create(:google_translate, api_key: 'xyz')
7
+ expect(translator).to be_kind_of(YamlTranslator::Translator)
8
+ end
9
+ end
10
+ end
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.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Noritaka Horio
@@ -173,6 +173,7 @@ files:
173
173
  - spec/fixtures/diff/after/en.yml
174
174
  - spec/fixtures/diff/before/en.yml
175
175
  - spec/fixtures/en.yml
176
+ - spec/fixtures/merge_target.yml
176
177
  - spec/fixtures/no_root.yml
177
178
  - spec/fixtures/with_root.yml
178
179
  - spec/helpers/locale_helper.rb
@@ -181,6 +182,7 @@ files:
181
182
  - spec/yaml-translator/structure/single_key.rb
182
183
  - spec/yaml-translator/structure/tree_spec.rb
183
184
  - spec/yaml-translator/translator_spec.rb
185
+ - spec/yaml_translator_spec.rb
184
186
  - yaml-translator.gemspec
185
187
  homepage: https://github.com/holyshared/yaml-translator
186
188
  licenses:
@@ -210,6 +212,7 @@ test_files:
210
212
  - spec/fixtures/diff/after/en.yml
211
213
  - spec/fixtures/diff/before/en.yml
212
214
  - spec/fixtures/en.yml
215
+ - spec/fixtures/merge_target.yml
213
216
  - spec/fixtures/no_root.yml
214
217
  - spec/fixtures/with_root.yml
215
218
  - spec/helpers/locale_helper.rb
@@ -218,3 +221,4 @@ test_files:
218
221
  - spec/yaml-translator/structure/single_key.rb
219
222
  - spec/yaml-translator/structure/tree_spec.rb
220
223
  - spec/yaml-translator/translator_spec.rb
224
+ - spec/yaml_translator_spec.rb