sy18nc 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  [![Build Status](https://travis-ci.org/michaldarda/sy18nc.png?branch=master)](https://travis-ci.org/michaldarda/sy18nc)
2
+ [![Gem Version](https://badge.fury.io/rb/sy18nc.png)](http://badge.fury.io/rb/sy18nc)
2
3
 
3
4
  # Sy18nc
4
5
 
@@ -9,7 +10,7 @@ Simple tool to synchronize Rails .ymls with locales.
9
10
  Add this line to your application's Gemfile:
10
11
 
11
12
  group :development do
12
- gem 'sy18nc', '~> 0.1.0'
13
+ gem 'sy18nc', '~> 0.2.1'
13
14
  end
14
15
 
15
16
  And then execute:
@@ -108,6 +109,10 @@ Please post any issues via Github Issues. If you want to contribute, see [Contri
108
109
 
109
110
  # Changelog
110
111
 
112
+ ## 0.2.1
113
+ - Synchronized already synchronized locales already marked as fixme (when key in base locale is changing)
114
+ - Fixed bug with creating translations from scratch
115
+
111
116
  ## 0.1.0
112
117
  - Creating translations from scratch
113
118
  - Deleting keys which are not present in base
@@ -7,7 +7,7 @@ class Hash
7
7
  self_value = self[other_key]
8
8
  self[other_key] = if self_value.is_a?(Hash) && other_value.is_a?(Hash)
9
9
  self_value.sy18nc_deep_merge!(other_value)
10
- elsif self_value.nil?
10
+ elsif self_value.nil? || self_value.sy18nc_marked_as_fixme?
11
11
  other_value.sy18nc_mark_fixme!
12
12
  else
13
13
  self_value
@@ -16,6 +16,16 @@ class Hash
16
16
  self
17
17
  end
18
18
 
19
+ def sy18nc_deep_delete_unused!(other_hash)
20
+ each_pair do |k, v|
21
+ if other_hash[k].nil?
22
+ delete(k)
23
+ elsif self[k].is_a?(Hash)
24
+ self[k].sy18nc_deep_delete_unused!(other_hash[k])
25
+ end
26
+ end
27
+ end
28
+
19
29
  # Appends the val to the every string in nested hash
20
30
  def sy18nc_append!(val)
21
31
  self.each_pair do |key, value|
@@ -1,7 +1,11 @@
1
1
  class String
2
+ def sy18nc_marked_as_fixme?
3
+ self =~ /g FIXME/
4
+ end
5
+
2
6
  def sy18nc_mark_fixme!
3
7
  # skip if it is already marked or its an alias
4
- if self =~ /g FIXME/ || self =~ /\*([a-z]*(\_)?)*/
8
+ if sy18nc_marked_as_fixme? || self =~ /\*([a-z]*(\_)?)*/
5
9
  return self
6
10
  end
7
11
 
@@ -4,6 +4,13 @@ module Sy18nc
4
4
  class Locale
5
5
  attr_reader :name, :hash
6
6
 
7
+ # a little helper, move to separate module
8
+ def nested_hash(keys)
9
+ head, *tail = keys
10
+ return {} if head.nil?
11
+ { head => nested_hash(tail) }
12
+ end
13
+
7
14
  def initialize(file)
8
15
  # locale does not exists
9
16
  unless File.exists?(File.expand_path(file))
@@ -13,10 +20,7 @@ module Sy18nc
13
20
  f = File.new(file, "w+")
14
21
 
15
22
  # uses fetched keys before to create a skeleton of locale
16
- locale_skeleton = tname.inject({}) do |result, k|
17
- result[k] = {}
18
- result
19
- end
23
+ locale_skeleton = nested_hash(tname.reverse)
20
24
 
21
25
  f.write(YAML.dump(locale_skeleton))
22
26
  f.close
@@ -49,6 +53,7 @@ module Sy18nc
49
53
 
50
54
  def synchronize(other)
51
55
  body.sy18nc_deep_merge!(other.body)
56
+ body.sy18nc_deep_delete_unused!(other.body)
52
57
  end
53
58
 
54
59
  def to_yaml
@@ -1,3 +1,3 @@
1
1
  module Sy18nc
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -134,7 +134,23 @@ describe Hash do
134
134
  other_hash.sy18nc_deep_merge!(@hash).should eql(result)
135
135
  end
136
136
 
137
- it "deep merges hashes with nested arrays" do
137
+ it "deep deletes unused" do
138
+ hash1 = {
139
+ :key1 => {
140
+ :key3 => {}
141
+ }
142
+ }
143
+
144
+ hash2 = {
145
+ :key1 => {
146
+ :key2 => 1,
147
+ :key3 => {
148
+ :key4 => 1
149
+ }
150
+ }
151
+ }
152
+
153
+ hash2.sy18nc_deep_delete_unused!(hash1).should eql(hash1)
138
154
  end
139
155
 
140
156
  describe String do
@@ -55,7 +55,6 @@ en:
55
55
  many: "%{count} Websites"
56
56
  other: "%{count} Websites"
57
57
  omniauth_callbacks:
58
- empty_key:
59
58
  success: "Successfully authorized from %{kind} account"
60
59
  user:
61
60
  failure: "Unexpected Authorization failure, please try again."
@@ -3,3 +3,4 @@ en:
3
3
  promo:
4
4
  link1: Hello
5
5
  link2: Hello
6
+ link3: Hello
@@ -28,7 +28,6 @@ tr:
28
28
  other: "%{count} Websites" # FIXME
29
29
  omniauth_callbacks:
30
30
  success: "Successfully authorized from %{kind} account"
31
- empty_key:
32
31
  user:
33
32
  failure: "Unexpected Authorization failure, please try again." # FIXME
34
33
  range:
@@ -2,3 +2,4 @@
2
2
  ru:
3
3
  promo:
4
4
  link1: Birbevoon
5
+ link3: Whatevervalue # FIXME
@@ -57,6 +57,7 @@ en:
57
57
  promo:
58
58
  link1: "Hello"
59
59
  link2: "Hello"
60
+ link3: "Hello"
60
61
  ])
61
62
  end
62
63
 
@@ -67,6 +68,7 @@ en:
67
68
  ru:
68
69
  promo:
69
70
  link1: "Birbevoon"
71
+ link3: "Hello" # FIXME
70
72
  link2: "Hello" # FIXME
71
73
  ])
72
74
  @locale.to_yaml.lines.count.should eql(russian_locale.to_yaml.lines.count)
@@ -80,6 +82,13 @@ ru:
80
82
  devise_tr.to_yaml.should eql(File.read(File.expand_path("spec/fixtures/results/devise.tr.yml")))
81
83
  end
82
84
 
85
+ it "deletes unused keys" do
86
+ locale1 = Sy18nc::Locale.new("spec/fixtures/devise.en.yml")
87
+ locale2 = Sy18nc::Locale.new("spec/fixtures/devise.tr.yml")
88
+
89
+ locale1.synchronize(locale2)
90
+ end
91
+
83
92
  def cleanup
84
93
  %x[rm en.yml]
85
94
  %x[rm en.yml.bak]
@@ -35,6 +35,7 @@ describe Sy18nc::Synchronizer do
35
35
  ru:
36
36
  promo:
37
37
  link1: "Birbevoon"
38
+ link3: "Hello" # FIXME
38
39
  link2: "Hello" # FIXME
39
40
  ]
40
41
  end
@@ -54,6 +55,7 @@ ru:
54
55
  ru:
55
56
  promo:
56
57
  link1: "Birbevoon"
58
+ link3: "Hello" # FIXME
57
59
  link2: "Hello" # FIXME
58
60
  ]
59
61
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sy18nc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-22 00:00:00.000000000 Z
12
+ date: 2013-11-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: psych