yaml-translator 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile.lock +1 -1
- data/README.md +14 -8
- data/example/google_translate.rb +10 -3
- data/lib/yaml-translator/locale.rb +61 -0
- data/lib/yaml-translator/translator.rb +5 -39
- data/lib/yaml-translator/version.rb +1 -1
- data/lib/yaml-translator.rb +1 -1
- data/spec/fixtures/en.yml +7 -0
- data/spec/yaml-translator/locale_spec.rb +37 -0
- data/spec/yaml-translator/translator_spec.rb +6 -4
- metadata +7 -5
- data/lib/yaml-translator/translated_result.rb +0 -30
- data/spec/yaml-translator/translated_result_spec.rb +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a0efca74a8185ea52ad088ae15d9b4842683f05f
|
4
|
+
data.tar.gz: 86e359f68864bc843576574ee02fac478182cfe8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 35c49f3c52a43d8bc96edb7903e829c94e699f9457d8ad633a9befd7516989d9cb7ab08a2f5a5509743c7d2372b9f323501ba0cbe7c117452c807089e227be33
|
7
|
+
data.tar.gz: adf189f897ff70ba6ec3ac9210438a0c8b19593a3890122b2cb600d19dac214f5ed1c2a66558d0a90c256e5a52af6d8e7fe98d1f3f6fdd064b13fff6208f9e1e
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# yaml-translator
|
2
2
|
|
3
|
+
yaml-translator is a gem that translates yaml format translation files into each language.
|
4
|
+
The translation method supports google translate api in built-in.
|
5
|
+
|
3
6
|
[![Gem Version](https://badge.fury.io/rb/yaml-translator.svg)](https://badge.fury.io/rb/yaml-translator)
|
4
7
|
[![Build Status](https://travis-ci.org/holyshared/yaml-translator.svg?branch=master)](https://travis-ci.org/holyshared/yaml-translator)
|
5
8
|
[![Coverage Status](https://coveralls.io/repos/github/holyshared/yaml-translator/badge.svg?branch=master)](https://coveralls.io/github/holyshared/yaml-translator?branch=master)
|
@@ -10,17 +13,20 @@
|
|
10
13
|
Translate the language file, do as follows.
|
11
14
|
|
12
15
|
```ruby
|
13
|
-
|
14
|
-
require 'yaml-translator'
|
15
|
-
|
16
|
+
dir = File.dirname(__FILE__)
|
16
17
|
adapter = ::YamlTranslator::Adapters::GoogleTranslateAdapter.new(ENV['GOOGLE_TRANSLATE_API_KEY'])
|
17
|
-
|
18
|
-
yaml = "#{dir}/en.yml"
|
19
18
|
translator = ::YamlTranslator::Translator.new(adapter)
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
english_locale = ::YamlTranslator::Locale.load_file("#{dir}/en.yml")
|
21
|
+
japanese_locale = english_locale.translate(translator, to: :ja)
|
22
|
+
|
23
|
+
p japanese_locale.to_s # convert to japanese locale yaml format
|
24
|
+
p japanese_locale.save_to(dir) # Write a ja.yml
|
25
|
+
|
26
|
+
german_locale = english_locale.translate(translator, to: :de)
|
27
|
+
|
28
|
+
p german_locale.to_s # convert to german locale yaml format
|
29
|
+
p german_locale.save_to(dir) # Write a de.yml
|
24
30
|
```
|
25
31
|
|
26
32
|
## Run the test
|
data/example/google_translate.rb
CHANGED
@@ -6,7 +6,14 @@ require 'yaml-translator'
|
|
6
6
|
dir = File.dirname(__FILE__)
|
7
7
|
adapter = ::YamlTranslator::Adapters::GoogleTranslateAdapter.new(ENV['GOOGLE_TRANSLATE_API_KEY'])
|
8
8
|
translator = ::YamlTranslator::Translator.new(adapter)
|
9
|
-
result = translator.translate_file("#{dir}/en.yml", to: :ja)
|
10
9
|
|
11
|
-
|
12
|
-
|
10
|
+
english_locale = ::YamlTranslator::Locale.load_file("#{dir}/en.yml")
|
11
|
+
japanese_locale = english_locale.translate(translator, to: :ja)
|
12
|
+
|
13
|
+
p japanese_locale.to_s
|
14
|
+
p japanese_locale.save_to(dir)
|
15
|
+
|
16
|
+
german_locale = english_locale.translate(translator, to: :de)
|
17
|
+
|
18
|
+
p german_locale.to_s
|
19
|
+
p german_locale.save_to(dir)
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module YamlTranslator
|
4
|
+
class Locale
|
5
|
+
attr_reader :lang, :values
|
6
|
+
|
7
|
+
def initialize(values, lang)
|
8
|
+
@lang = lang
|
9
|
+
@values = values
|
10
|
+
end
|
11
|
+
|
12
|
+
def translate(translator, options={})
|
13
|
+
translator.translate(self, options)
|
14
|
+
end
|
15
|
+
|
16
|
+
def save(dir=Dir.pwd)
|
17
|
+
write_file(File.join(dir, "#{lang}.yml"))
|
18
|
+
end
|
19
|
+
|
20
|
+
def save_to(dir)
|
21
|
+
save(dir)
|
22
|
+
end
|
23
|
+
|
24
|
+
def flatten_hash
|
25
|
+
flatten(values)
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_s
|
29
|
+
YAML.dump(values)
|
30
|
+
end
|
31
|
+
|
32
|
+
class << self
|
33
|
+
def load_file(file)
|
34
|
+
lang = File.basename(file).gsub(/.(yml|yaml)/, '')
|
35
|
+
yaml = YAML.load(File.open(file, &:read))
|
36
|
+
self.new(yaml, lang)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
# Covert to a flatten hash
|
43
|
+
def flatten(values={}, path=KeyPath.new)
|
44
|
+
result = {}
|
45
|
+
values.each_with_index do |(i, v)|
|
46
|
+
path.move_to(i)
|
47
|
+
if v.is_a?(Hash)
|
48
|
+
result.merge!(flatten(v, path))
|
49
|
+
else
|
50
|
+
result[path.to_s] = v
|
51
|
+
end
|
52
|
+
path.leave
|
53
|
+
end
|
54
|
+
result
|
55
|
+
end
|
56
|
+
|
57
|
+
def write_file(file_path)
|
58
|
+
File.write(file_path, to_s)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -3,37 +3,20 @@ require "yaml-translator/adapters"
|
|
3
3
|
module YamlTranslator
|
4
4
|
class Translator
|
5
5
|
def initialize(adapter = Adapters::NoopAdaptor.new)
|
6
|
-
@path = KeyPath.new
|
7
6
|
@adapter = adapter
|
8
7
|
end
|
9
8
|
|
10
9
|
# Translate target
|
11
10
|
#
|
12
|
-
# @param [
|
13
|
-
# @return [
|
14
|
-
def translate(
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
# Translate target file
|
19
|
-
#
|
20
|
-
# @param [Hash] file YAML file of translate target
|
21
|
-
# @return [TranslatedResult] translated result
|
22
|
-
def translate_file(file, options={})
|
23
|
-
yaml = YAML.load(File.open(file, &:read))
|
24
|
-
translate(yaml)
|
11
|
+
# @param [Locale] locale of translate target
|
12
|
+
# @return [Locale] locale
|
13
|
+
def translate(locale, options={})
|
14
|
+
translated = @adapter.translate(locale.flatten_hash, options)
|
15
|
+
Locale.new(rebuild(translated), options[:to])
|
25
16
|
end
|
26
17
|
|
27
18
|
private
|
28
19
|
|
29
|
-
# Translate target
|
30
|
-
#
|
31
|
-
# @param [Hash] values Hash of translate target
|
32
|
-
# @return [Hash] translated hash
|
33
|
-
def translate_tree(values, options={})
|
34
|
-
@adapter.translate(values, options)
|
35
|
-
end
|
36
|
-
|
37
20
|
# Returning the flattened structure to the tree structure
|
38
21
|
#
|
39
22
|
# @param [Hash] values flatten Hash
|
@@ -57,22 +40,5 @@ module YamlTranslator
|
|
57
40
|
end
|
58
41
|
result
|
59
42
|
end
|
60
|
-
|
61
|
-
# Covert to a flat hash
|
62
|
-
# @param [Hash] values flatten target
|
63
|
-
# @return [Hash]
|
64
|
-
def flatten(values)
|
65
|
-
result = {}
|
66
|
-
values.each_with_index do |(i, v)|
|
67
|
-
@path.move_to(i)
|
68
|
-
if v.is_a?(Hash)
|
69
|
-
result.merge!(flatten(v))
|
70
|
-
else
|
71
|
-
result[@path.to_s] = v
|
72
|
-
end
|
73
|
-
@path.leave
|
74
|
-
end
|
75
|
-
result
|
76
|
-
end
|
77
43
|
end
|
78
44
|
end
|
data/lib/yaml-translator.rb
CHANGED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'rspec/temp_dir'
|
2
|
+
|
3
|
+
describe YamlTranslator::Locale do
|
4
|
+
include_context 'uses temp dir'
|
5
|
+
|
6
|
+
let(:tmp_dir) { temp_dir }
|
7
|
+
let(:lang_file) { File.join(File.dirname(__FILE__), '/../fixtures/en.yml') }
|
8
|
+
let(:lang_file_content) { File.open(lang_file, &:read) }
|
9
|
+
let(:locale) { YamlTranslator::Locale.load_file(lang_file) }
|
10
|
+
let(:output_file) { File.join(tmp_dir, 'en.yml') }
|
11
|
+
let(:file_exist) { File.exist?(output_file) }
|
12
|
+
|
13
|
+
describe '#flatten_hash' do
|
14
|
+
let(:flatten_hash) do
|
15
|
+
{
|
16
|
+
'en.bar' => 'bar',
|
17
|
+
'en.foo'=> 'foo',
|
18
|
+
'en.foo1.foo1' => 'foo1-1',
|
19
|
+
'en.foo1.foo2' => 'foo1-2'
|
20
|
+
}
|
21
|
+
end
|
22
|
+
it 'should be return flatten hash' do
|
23
|
+
expect(locale.flatten_hash).to eq(flatten_hash)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
describe '#to_s' do
|
27
|
+
it 'should be return yaml formatted string' do
|
28
|
+
expect(locale.to_s).to eq(lang_file_content)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
describe '#save_to' do
|
32
|
+
before { locale.save_to(tmp_dir) }
|
33
|
+
it 'should be write a language file' do
|
34
|
+
expect(file_exist).to be true
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -3,16 +3,18 @@ describe YamlTranslator::Translator do
|
|
3
3
|
let(:translator) { YamlTranslator::Translator.new }
|
4
4
|
context 'when have root' do
|
5
5
|
let(:yaml_tree) { load_yaml(:with_root) }
|
6
|
+
let(:locale) { YamlTranslator::Locale.new(yaml_tree, :en) }
|
6
7
|
it 'translated tree' do
|
7
|
-
|
8
|
-
expect(
|
8
|
+
translated_locale = translator.translate(locale)
|
9
|
+
expect(translated_locale.values).to eq(locale.values)
|
9
10
|
end
|
10
11
|
end
|
11
12
|
context 'when have root' do
|
12
13
|
let(:yaml_tree) { load_yaml(:no_root) }
|
14
|
+
let(:locale) { YamlTranslator::Locale.new(yaml_tree, :en) }
|
13
15
|
it 'translated tree' do
|
14
|
-
|
15
|
-
expect(
|
16
|
+
translated_locale = translator.translate(locale)
|
17
|
+
expect(translated_locale.values).to eq(locale.values)
|
16
18
|
end
|
17
19
|
end
|
18
20
|
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
|
+
version: 0.3.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-
|
11
|
+
date: 2017-02-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: easy_translate
|
@@ -147,14 +147,15 @@ files:
|
|
147
147
|
- lib/yaml-translator/adapters/google_translate_adapter.rb
|
148
148
|
- lib/yaml-translator/adapters/noop_adapter.rb
|
149
149
|
- lib/yaml-translator/key_path.rb
|
150
|
-
- lib/yaml-translator/
|
150
|
+
- lib/yaml-translator/locale.rb
|
151
151
|
- lib/yaml-translator/translator.rb
|
152
152
|
- lib/yaml-translator/version.rb
|
153
|
+
- spec/fixtures/en.yml
|
153
154
|
- spec/fixtures/no_root.yml
|
154
155
|
- spec/fixtures/with_root.yml
|
155
156
|
- spec/helpers/locale_helper.rb
|
156
157
|
- spec/spec_helper.rb
|
157
|
-
- spec/yaml-translator/
|
158
|
+
- spec/yaml-translator/locale_spec.rb
|
158
159
|
- spec/yaml-translator/translator_spec.rb
|
159
160
|
- yaml-translator.gemspec
|
160
161
|
homepage: https://github.com/holyshared/yaml-translator
|
@@ -182,9 +183,10 @@ signing_key:
|
|
182
183
|
specification_version: 4
|
183
184
|
summary: Translate for the locales configuration file
|
184
185
|
test_files:
|
186
|
+
- spec/fixtures/en.yml
|
185
187
|
- spec/fixtures/no_root.yml
|
186
188
|
- spec/fixtures/with_root.yml
|
187
189
|
- spec/helpers/locale_helper.rb
|
188
190
|
- spec/spec_helper.rb
|
189
|
-
- spec/yaml-translator/
|
191
|
+
- spec/yaml-translator/locale_spec.rb
|
190
192
|
- spec/yaml-translator/translator_spec.rb
|
@@ -1,30 +0,0 @@
|
|
1
|
-
require 'yaml'
|
2
|
-
|
3
|
-
module YamlTranslator
|
4
|
-
class TranslatedResult
|
5
|
-
attr_reader :lang, :values
|
6
|
-
|
7
|
-
def initialize(values, lang)
|
8
|
-
@lang = lang
|
9
|
-
@values = values
|
10
|
-
end
|
11
|
-
|
12
|
-
def save(dir=Dir.pwd)
|
13
|
-
write_file(File.join(dir, "#{lang}.yml"))
|
14
|
-
end
|
15
|
-
|
16
|
-
def save_to(dir)
|
17
|
-
save(dir)
|
18
|
-
end
|
19
|
-
|
20
|
-
def to_s
|
21
|
-
YAML.dump(@values)
|
22
|
-
end
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
def write_file(file_path)
|
27
|
-
File.write(file_path, to_s)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
require 'rspec/temp_dir'
|
2
|
-
|
3
|
-
describe YamlTranslator::TranslatedResult do
|
4
|
-
let(:result) { YamlTranslator::TranslatedResult.new({ foo: 'bar' }, 'ja') }
|
5
|
-
describe '#save_to' do
|
6
|
-
let(:tmp_dir) { temp_dir }
|
7
|
-
let(:output_file) { File.join(tmp_dir, 'ja.yml') }
|
8
|
-
let(:file_exist) { File.exist?(output_file) }
|
9
|
-
include_context 'uses temp dir'
|
10
|
-
it 'should be write a language file' do
|
11
|
-
result.save_to(tmp_dir)
|
12
|
-
expect(file_exist).to be true
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|