rails_locale_sorter 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rails_locale_sorter'
3
+
4
+ puts "Usage: scoop [source-dir] [out-dir]\n\n\n"
5
+
6
+ source_dir = ARGV[0] || "new-locales"
7
+ out_dir = ARGV[1] || "locales"
8
+
9
+ lm = RailsLocaleSorter::LocaleManager.new source_dir, out_dir
10
+ lm.apply_patches
@@ -8,4 +8,4 @@ out_dir = ARGV[1] || "new-locales"
8
8
  truth_locale = ARGV[2] || "en.yml"
9
9
 
10
10
  lm = RailsLocaleSorter::LocaleManager.new source_dir, out_dir
11
- lm.parse_to_yaml truth_locale
11
+ lm.create_missing_nodes truth_locale
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rails_locale_sorter'
3
+
4
+ puts "Usage: poop [source-path] [out-path] [default-language]\n\n\n"
5
+
6
+ source_dir = ARGV[0] || "locales"
7
+ out_dir = ARGV[1] || "new-locales"
8
+ truth_locale = ARGV[2] || "en.yml"
9
+
10
+ lm = RailsLocaleSorter::LocaleManager.new source_dir, out_dir
11
+ lm.create_patches truth_locale
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rails_locale_sorter'
3
+
4
+ puts "Usage: poop [source-path] [out-path] [default-language]\n\n\n"
5
+
6
+ source_dir = ARGV[0] || "locales"
7
+ out_dir = ARGV[1] || "new-locales"
8
+ truth_locale = ARGV[2] || "en.yml"
9
+
10
+ lm = RailsLocaleSorter::LocaleManager.new source_dir, out_dir
11
+ lm.create_patches truth_locale
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rails_locale_sorter'
3
+
4
+ puts "Usage: scoop [source-dir] [out-dir]\n\n\n"
5
+
6
+ source_dir = ARGV[0] || "new-locales"
7
+ out_dir = ARGV[1] || "locales"
8
+
9
+ lm = RailsLocaleSorter::LocaleManager.new source_dir, out_dir
10
+ lm.apply_patches
@@ -30,43 +30,67 @@ module RailsLocaleSorter
30
30
  end
31
31
 
32
32
  class LocaleManager
33
- MERGER = proc do |key, v1, v2|
33
+ attr_accessor :source, :out
34
+
35
+ MERGER = proc do |key, v1, v2|
34
36
  if(Hash === v1 && Hash === v2)
35
37
  v1.merge(v2, &MERGER)
36
38
  else
37
- v2
39
+ v2
38
40
  end
39
41
  end
40
42
 
41
43
  def initialize(source_dir, out_dir)
42
44
  @source = source_dir
43
45
  @out = out_dir
44
- end
45
46
 
46
- def parse_to_yaml(truth = "en.yml")
47
- # for ya2yaml
48
- $KCODE="UTF8"
49
47
  Dir::mkdir(@out) unless File.exists? @out
48
+ end
50
49
 
50
+ def create_missing_nodes(truth = "en.yml")
51
51
  translations = YAML::load_file("#{@source}/#{truth}")
52
52
  mergee = create_blank_copy(translations)
53
53
 
54
- with_each_file do |f, filename|
55
- puts "Processing #{filename}..."
56
- me = YAML::load(f)
54
+ process_each_file do |hash, file, filename|
57
55
  unless filename == truth
58
- me = create_missing_keys(me, mergee)
56
+ hash = create_missing_keys(hash, mergee)
59
57
  end
60
58
 
61
- me = OrderFact.convert_and_sort(me, true)
62
- File.open("#{@out}/#{filename}", "w+") do |fw|
63
- fw.puts me.ya2yaml(:syck_compatible => true)
64
- end
59
+ sort_and_write(filename, hash)
65
60
  end
61
+ end
62
+
63
+ def create_patches(truth = "en.yml")
64
+ translations = YAML::load_file("#{@source}/#{truth}")
66
65
 
66
+ process_each_file do |hash, file, filename|
67
+ unless filename == truth
68
+ hash = list_missing_keys(translations, hash)
69
+ end
70
+
71
+ sort_and_write(filename, hash)
72
+ end
67
73
  end
68
74
 
75
+ def apply_patches(reverse_dirs = false)
76
+ swap_dirs if reverse_dirs
77
+
78
+ process_each_file do |patch, file, filename|
79
+ target = YAML::load_file("#{@out}/#{filename}")
80
+
81
+ merged = target.merge(patch, &MERGER)
82
+
83
+ sort_and_write(filename, merged)
84
+ end
85
+
86
+ swap_dirs if reverse_dirs
87
+ end
69
88
  private
89
+ def swap_dirs
90
+ tmp = @source
91
+ @source = @out
92
+ @out = tmp
93
+ end
70
94
 
71
95
  def with_file(filename, &block)
72
96
  leafname = filename.scan(/[a-zA-Z_-]*.yml/).first.to_s
@@ -81,6 +105,26 @@ module RailsLocaleSorter
81
105
  end
82
106
  end
83
107
 
108
+ def process_each_file(&block)
109
+ # for ya2yaml
110
+ $KCODE="UTF8"
111
+
112
+ with_each_file do |f, filename|
113
+ puts "Processing #{filename}..."
114
+ hash = YAML::load(f)
115
+
116
+ block.call(hash, f, filename)
117
+ end
118
+ end
119
+
120
+ def sort_and_write(filename, hash)
121
+ hash = OrderFact.convert_and_sort(hash, true)
122
+
123
+ File.open("#{@out}/#{filename}", "w+") do |fw|
124
+ fw.puts hash.ya2yaml(:syck_compatible => true)
125
+ end
126
+ end
127
+
84
128
  def create_missing_keys(object, all_keys)
85
129
  ak_lang = all_keys.first[0]
86
130
  ob_lang = object.first[0]
@@ -90,6 +134,32 @@ module RailsLocaleSorter
90
134
  {ob_lang => new_values}
91
135
  end
92
136
 
137
+ def compare_keys(source, target)
138
+ new_hash = {}
139
+
140
+ source.each do |k, v|
141
+ if Hash === target[k] && Hash === v
142
+ res = compare_keys(v, target[k])
143
+ new_hash[k] = res unless res.empty?
144
+ elsif target[k].nil?
145
+ new_hash[k] = v
146
+ else
147
+ # do nothing
148
+ # puts "#{k} is deleted"
149
+ end
150
+ end
151
+
152
+ new_hash
153
+ end
154
+
155
+ def list_missing_keys(source, target)
156
+ source_lang = source.first[0]
157
+ target_lang = target.first[0]
158
+
159
+ new_values = compare_keys(source[source_lang], target[target_lang])
160
+ {target_lang => new_values}
161
+ end
162
+
93
163
  def blank_out_object(object)
94
164
  unless(Hash === object)
95
165
  return;
@@ -1,3 +1,3 @@
1
1
  module RailsLocaleSorter
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -0,0 +1,8 @@
1
+ en:
2
+ text:
3
+ javascript:
4
+ provide: Provide
5
+ reactivate: Reactivate
6
+ shared:
7
+ fixed: Fixed
8
+ hello: Hello
@@ -0,0 +1,10 @@
1
+ en:
2
+ text:
3
+ javascript:
4
+ invalid: Invalid
5
+ provide: Provide
6
+ reactivate: Reactivate
7
+ shared:
8
+ oops: Oops
9
+ fixed: Fixed
10
+ hello: Hello
@@ -3,6 +3,7 @@ require "spec_helper"
3
3
 
4
4
  describe RailsLocaleSorter::LocaleManager do
5
5
  FIXTURE_DIR = "resources/locale_fixtures"
6
+ TEST_DIR = "resources/locale_tests"
6
7
  SRC_DIR = "resources/src"
7
8
  OUT_DIR = "resources/out"
8
9
 
@@ -17,14 +18,60 @@ describe RailsLocaleSorter::LocaleManager do
17
18
  @lm = RailsLocaleSorter::LocaleManager.new SRC_DIR, OUT_DIR
18
19
  end
19
20
 
20
- it "writes blank keys for new values" do
21
- @lm.parse_to_yaml "src.yml"
21
+ describe "create missing nodes" do
22
+ it "writes blank keys for new values" do
23
+ @lm.create_missing_nodes "src.yml"
22
24
 
23
- result = File.open("#{OUT_DIR}/out.yml").read
24
- result.should include "source_only: ~"
25
- result.should include "src_only: ~"
26
- result.should include "both: Both"
27
- result.should include "exotic: 下一页!След"
28
- result.should include "out:"
25
+ result = File.open("#{OUT_DIR}/out.yml").read
26
+ result.should include "source_only: ~"
27
+ result.should include "src_only: ~"
28
+ result.should include "both: Both"
29
+ result.should include "exotic: 下一页!След"
30
+ result.should include "out:"
31
+ end
32
+ end
33
+
34
+ describe "create_patches" do
35
+ it "creates a file of differences only" do
36
+ @lm.create_patches "src.yml"
37
+
38
+ result = File.open("#{OUT_DIR}/out.yml").read
39
+ result.should include "out:"
40
+ result.should include "text:"
41
+ result.should include "source_only: "
42
+ result.should include "date:"
43
+ result.should include "src_only: "
44
+ result.should_not include "both: Both"
45
+ result.should_not include "exotic: 下一页!След"
46
+ end
47
+ end
48
+
49
+ describe "poop and scoop" do
50
+ before :each do
51
+ # setup test folders
52
+ FileUtils.rm_r(SRC_DIR) if File.exists? SRC_DIR
53
+ FileUtils.cp_r(TEST_DIR, SRC_DIR)
54
+ FileUtils.rm_r(OUT_DIR) if File.exists? OUT_DIR
55
+ @lm = RailsLocaleSorter::LocaleManager.new SRC_DIR, OUT_DIR
56
+ end
57
+
58
+ it "goes through the full cycle" do
59
+ @lm.create_patches "en.yml"
60
+
61
+ result = File.open("#{OUT_DIR}/en-target.yml").read
62
+ result.should include "en:"
63
+ result.should include "text:"
64
+ result.should include "invalid: "
65
+ result.should include "shared:"
66
+ result.should include "oops:"
67
+ result.should_not include "fixed:"
68
+ result.should_not include "provide:"
69
+
70
+ @lm.apply_patches true
71
+ patched = File.open("#{SRC_DIR}/en-target.yml").read
72
+ source = File.open("#{SRC_DIR}/en.yml").read
73
+
74
+ patched.should == source
75
+ end
29
76
  end
30
77
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_locale_sorter
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 7
10
- version: 0.0.7
9
+ - 8
10
+ version: 0.0.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Wheeler
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-02-09 00:00:00 -08:00
18
+ date: 2012-03-20 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -69,7 +69,11 @@ description: "Manage Rails Locale YAML: Sort, remove duplicates, and create stub
69
69
  email:
70
70
  - michael.wheeler@tapjoy.com
71
71
  executables:
72
+ - locale_apply
72
73
  - locale_generator
74
+ - locale_patch
75
+ - poop
76
+ - scoop
73
77
  extensions: []
74
78
 
75
79
  extra_rdoc_files: []
@@ -79,16 +83,18 @@ files:
79
83
  - Gemfile
80
84
  - README.md
81
85
  - Rakefile
86
+ - bin/locale_apply
82
87
  - bin/locale_generator
88
+ - bin/locale_patch
89
+ - bin/poop
90
+ - bin/scoop
83
91
  - lib/rails_locale_sorter.rb
84
92
  - lib/rails_locale_sorter/version.rb
85
93
  - rails_locale_sorter.gemspec
86
94
  - spec/resources/locale_fixtures/out.yml
87
95
  - spec/resources/locale_fixtures/src.yml
88
- - spec/resources/locale_tests/source.yml
89
- - spec/resources/locale_tests/target.yml
90
- - spec/resources/src/out.yml
91
- - spec/resources/src/src.yml
96
+ - spec/resources/locale_tests/en-target.yml
97
+ - spec/resources/locale_tests/en.yml
92
98
  - spec/rls-spec.rb
93
99
  - spec/spec_helper.rb
94
100
  has_rdoc: true
@@ -128,9 +134,7 @@ summary: "Manage Rails Locale YAML: Sort, remove duplicates, and create stubs fo
128
134
  test_files:
129
135
  - spec/resources/locale_fixtures/out.yml
130
136
  - spec/resources/locale_fixtures/src.yml
131
- - spec/resources/locale_tests/source.yml
132
- - spec/resources/locale_tests/target.yml
133
- - spec/resources/src/out.yml
134
- - spec/resources/src/src.yml
137
+ - spec/resources/locale_tests/en-target.yml
138
+ - spec/resources/locale_tests/en.yml
135
139
  - spec/rls-spec.rb
136
140
  - spec/spec_helper.rb
@@ -1,4 +0,0 @@
1
- source:
2
- text:
3
- source_only: Source Only
4
- both: Both
@@ -1,3 +0,0 @@
1
- target:
2
- text:
3
- both: Both