i18n_yaml_sorter_2 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +261 -0
- data/lib/i18n_yaml_sorter/sorter.rb +29 -32
- data/lib/i18n_yaml_sorter/version.rb +1 -1
- data/spec/i18n_yaml_sorter_spec.rb +9 -0
- data/spec/in.yml +24 -31
- data/spec/in_W.yml +65 -0
- data/spec/out.yml +0 -2
- data/spec/out_W.yml +60 -0
- data/spec/out_simple.yml +0 -2
- metadata +5 -4
- data/README.rdoc +0 -236
- data/spec/test_i18n_yaml_sorter.rb +0 -48
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dabdfcfde3af00bec4db5273ca3feaf0ca76ee04
|
4
|
+
data.tar.gz: decc4727fecc6f94ccc7c853f20fc6b7fee93d48
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d94cc79a5073493ace8019cd8e5a97b2a72b978ff8a7c4d486d1268b965a63d26295061d977372bc547636d11b029238d70e2fa15c336f42c1fd00c3dc32e7ca
|
7
|
+
data.tar.gz: 1a2c0dc543663bb18e26099767d2ba3f99259a0200d318f97751f8660a7779c20acae5ed11292595845574745d36eca59e3115b3556445cac8be80961630dc8d
|
data/README.md
ADDED
@@ -0,0 +1,261 @@
|
|
1
|
+
# i18n_yaml_sorter
|
2
|
+
|
3
|
+
A simple YAML Hash deep sorter that will not mess with the way your strings
|
4
|
+
and text values were written. Made exclusively to sort the YAML Hash keys
|
5
|
+
commonly used in the i18n gem and Rails' apps. It will not sort arrays, YAML
|
6
|
+
objects, etc.
|
7
|
+
|
8
|
+
It doesn't parse the YAML and dumps it again, it just sorts the lines, it's
|
9
|
+
pure and simple magic! That way, it guarantees you can keep using your nice
|
10
|
+
YAML constructs to create large text values, as we usually do in i18n files.
|
11
|
+
|
12
|
+
It includes a simple TextMate bundle and a rake task for Rails, so you can
|
13
|
+
keep all those .yml files sorted in no time.
|
14
|
+
|
15
|
+
## Motivation
|
16
|
+
|
17
|
+
As your rails i18n app grows, it becomes very boring to keep all locale files
|
18
|
+
in sync. You have to add all keys in the same order, in different languages,
|
19
|
+
or you will get yourself into a mess and will lose precious time comparing the
|
20
|
+
files in different locales and looking for the appropriate translation keys
|
21
|
+
whenever you want to change something or see if a key is missing.
|
22
|
+
|
23
|
+
If you try deserializing/serializing the yml files using the YAML parser, to
|
24
|
+
get them in the same order, you will figure that your strings in the YAMLs
|
25
|
+
will be "standarized" to whatever the YAML generator prefers (eg.: strings in
|
26
|
+
double quotes, character escapes, etc). It will also mess up your indentation
|
27
|
+
(and use its defaults) and remove your comments.
|
28
|
+
|
29
|
+
i18n_yaml_sorter to the rescue! Add it to TextMate and you will be able to
|
30
|
+
sort your yaml file in nanoseconds. Your file will look pristine, like it was
|
31
|
+
human edited.
|
32
|
+
|
33
|
+
Tested in ruby-1.8.7-p334 and ruby-1.9.2-p290, should work everywhere since it
|
34
|
+
is so simple. Rake task works in Rails 3.1, not sure about older versions
|
35
|
+
though (patch updates are welcome, if you find it doesn't).
|
36
|
+
|
37
|
+
## Simple Example
|
38
|
+
This:
|
39
|
+
|
40
|
+
pt-BR:
|
41
|
+
# Note how this is a nice way of inputing
|
42
|
+
# paragraphs of text in YAML.
|
43
|
+
apples: >
|
44
|
+
Maçãs são boas,
|
45
|
+
só não coma
|
46
|
+
seus iPods!
|
47
|
+
grapes: Não comemos elas.
|
48
|
+
bananas: |
|
49
|
+
Bananas são "legais":
|
50
|
+
- Elas são <b> doces </b>.
|
51
|
+
isto: não é chave
|
52
|
+
|
53
|
+
Por isto todos gostam de bananas!
|
54
|
+
en-US:
|
55
|
+
# Note that our comments are important:
|
56
|
+
# Don't let your yaml sorter delete them!
|
57
|
+
grapes: We dont' eat them.
|
58
|
+
bananas: |
|
59
|
+
Bananas are "nice":
|
60
|
+
- They are <b> sweet </b>.
|
61
|
+
this: not a key
|
62
|
+
|
63
|
+
That is why everyone like bananas!
|
64
|
+
apples: >
|
65
|
+
Apples are fine,
|
66
|
+
just don't eat your
|
67
|
+
iPods!
|
68
|
+
|
69
|
+
Becomes:
|
70
|
+
|
71
|
+
en-US:
|
72
|
+
# Note that our comments are important:
|
73
|
+
# Don't let your yaml sorter delete them!
|
74
|
+
apples: >
|
75
|
+
Apples are fine,
|
76
|
+
just don't eat your
|
77
|
+
iPods!
|
78
|
+
bananas: |
|
79
|
+
Bananas are "nice":
|
80
|
+
- They are <b> sweet </b>.
|
81
|
+
this: not a key
|
82
|
+
|
83
|
+
That is why everyone like bananas!
|
84
|
+
grapes: We dont' eat them.
|
85
|
+
pt-BR:
|
86
|
+
# Note how this is a nice way of inputing
|
87
|
+
# paragraphs of text in YAML.
|
88
|
+
apples: >
|
89
|
+
Maçãs são boas,
|
90
|
+
só não coma
|
91
|
+
seus iPods!
|
92
|
+
bananas: |
|
93
|
+
Bananas são "legais":
|
94
|
+
- Elas são <b> doces </b>.
|
95
|
+
isto: não é chave
|
96
|
+
|
97
|
+
Por isto todos gostam de bananas!
|
98
|
+
grapes: Não comemos elas.
|
99
|
+
|
100
|
+
What if you use some method that employ's Ruby's YAML library to help do the
|
101
|
+
task for you? You would get an output like that (note it removes your
|
102
|
+
comments):
|
103
|
+
|
104
|
+
en-US:
|
105
|
+
apples: Apples are fine, just don't eat your iPods!
|
106
|
+
bananas: |
|
107
|
+
Bananas are "nice":
|
108
|
+
- They are <b> sweet </b>.
|
109
|
+
this: not a key
|
110
|
+
|
111
|
+
That is why everyone like bananas!
|
112
|
+
|
113
|
+
grapes: We dont' eat them.
|
114
|
+
pt-BR:
|
115
|
+
apples: "Ma\xC3\xA7\xC3\xA3s s\xC3\xA3o boas, s\xC3\xB3 n\xC3\xA3o coma seus iPods!\n"
|
116
|
+
bananas: "Bananas s\xC3\xA3o \"legais\":\n - Elas s\xC3\xA3o <b> doces </b>.\n isto: n\xC3\xA3o \xC3\xA9 chave\n\n\
|
117
|
+
Por isto todos gostam de bananas!\n"
|
118
|
+
grapes: "N\xC3\xA3o comemos elas."
|
119
|
+
|
120
|
+
## More complex example
|
121
|
+
|
122
|
+
This:
|
123
|
+
|
124
|
+
b_two:
|
125
|
+
a_1: Simple most common text
|
126
|
+
b_two: |
|
127
|
+
This is the best way of
|
128
|
+
inputing large chunks of text
|
129
|
+
in the YAML files.
|
130
|
+
|
131
|
+
Note that this format keeps blank
|
132
|
+
lines in the same indentation.
|
133
|
+
d_four: "We can also
|
134
|
+
use the boring \"
|
135
|
+
across multiple lines
|
136
|
+
but have to escape then."
|
137
|
+
e_five: Or you can do it
|
138
|
+
like that as well, it also works.
|
139
|
+
c_three:
|
140
|
+
a: "Marcelo"
|
141
|
+
d_4: Rafael
|
142
|
+
# Your comments will be untouched
|
143
|
+
# but will be bound to the element
|
144
|
+
# on top of them (d_4 here).
|
145
|
+
"b": Bernardo
|
146
|
+
c_3:
|
147
|
+
unify: Luiz
|
148
|
+
klass: Lucas
|
149
|
+
a_one: >
|
150
|
+
This is another way
|
151
|
+
of inputing text. It
|
152
|
+
will squish whitespace
|
153
|
+
when deserialized
|
154
|
+
(like HTML does).
|
155
|
+
|
156
|
+
Becomes:
|
157
|
+
|
158
|
+
a_one: >
|
159
|
+
This is another way
|
160
|
+
of inputing text. It
|
161
|
+
will squish whitespace
|
162
|
+
when deserialized
|
163
|
+
(like HTML does).
|
164
|
+
b_two:
|
165
|
+
a_1: Simple most common text
|
166
|
+
b_two: |
|
167
|
+
This is the best way of
|
168
|
+
inputing large chunks of text
|
169
|
+
in the YAML files.
|
170
|
+
|
171
|
+
Note that this format keeps blank
|
172
|
+
lines in the same indentation.
|
173
|
+
d_four: "We can also
|
174
|
+
use the boring \"
|
175
|
+
across multiple lines
|
176
|
+
but have to escape then."
|
177
|
+
c_three:
|
178
|
+
a: "Marcelo"
|
179
|
+
"b": Bernardo
|
180
|
+
c_3:
|
181
|
+
klass: Lucas
|
182
|
+
unify: Luiz
|
183
|
+
d_4: Rafael
|
184
|
+
# Your comments will be untouched
|
185
|
+
# but will be bound to the element
|
186
|
+
# on top of them (d_4 here).
|
187
|
+
e_five: Or you can do it
|
188
|
+
like that as well, it also works.
|
189
|
+
|
190
|
+
## Installing
|
191
|
+
|
192
|
+
Easy, just install the gem:
|
193
|
+
|
194
|
+
$ gem install i18n_yaml_sorter
|
195
|
+
|
196
|
+
Then the `sort_yaml` command line tool will be available. If you prefer using
|
197
|
+
Ruby (in your rakes, etc), use the simple `I18nYamlSorter::Sort` class.
|
198
|
+
|
199
|
+
## Textmate Bundle
|
200
|
+
|
201
|
+
Run this command in the Terminal to install it:
|
202
|
+
|
203
|
+
$ sort_yaml -i
|
204
|
+
|
205
|
+
A TextMate bundle, named "Yaml Sort" will be installed in your user home path.
|
206
|
+
Press "Shift+Command+S" or use the Bundles menu to invoke it. The opened yaml
|
207
|
+
file (or just the part of it that is selected) will be sorted. To edit the
|
208
|
+
selected part of the file, make sure it is valid YAML by itself, or your yaml
|
209
|
+
file might be corrupted (you can always Undo if you mess up).
|
210
|
+
|
211
|
+
## Rails Rake Taks
|
212
|
+
|
213
|
+
Declare it as a dependency in your app Gemfile, under the development group:
|
214
|
+
|
215
|
+
gem 'i18n_yaml_sorter', :group => :development
|
216
|
+
|
217
|
+
Run bundle install under your Rails' app:
|
218
|
+
|
219
|
+
$ bundle install
|
220
|
+
|
221
|
+
Now run the rake task under your Rails' app to sort all the i18n files in your
|
222
|
+
`config/locales` dir:
|
223
|
+
|
224
|
+
$rake i18n:sort
|
225
|
+
|
226
|
+
## Command line Input / Output
|
227
|
+
|
228
|
+
`sort_yaml` will operate on STDIN and STDOUT, so sorting an existing yaml file
|
229
|
+
should be as easy as:
|
230
|
+
|
231
|
+
$ sort_yaml < in.yml > out.yml
|
232
|
+
|
233
|
+
TODO: Add command line arguments parsing and options, so you can, for
|
234
|
+
instance, sort a whole dir of yaml files.
|
235
|
+
|
236
|
+
## Development
|
237
|
+
|
238
|
+
$rake spec # run tests
|
239
|
+
$rake release # build installable/publishable gem
|
240
|
+
|
241
|
+
## Future improvements (Forks Welcome!)
|
242
|
+
|
243
|
+
* Make `sort_yaml` smart, take directories, etc
|
244
|
+
* Refactoring: internal code is still a bit ugly, but works
|
245
|
+
|
246
|
+
|
247
|
+
## Note on Patches/Pull Requests
|
248
|
+
|
249
|
+
* Fork the project.
|
250
|
+
* Make your feature addition or bug fix.
|
251
|
+
* Add tests for it. This is important so I don't break it in a future
|
252
|
+
version unintentionally.
|
253
|
+
* Commit, do not mess with rakefile, version, or history. (if you want to
|
254
|
+
have your own version, that is fine but bump version in a commit by itself
|
255
|
+
I can ignore when I pull)
|
256
|
+
* Send me a pull request. Bonus points for topic branches.
|
257
|
+
|
258
|
+
|
259
|
+
## Copyright
|
260
|
+
|
261
|
+
Copyright (c) 2010-2011 Bernardo de Pádua. MIT License (See LICENCE).
|
@@ -9,14 +9,14 @@ module I18nYamlSorter
|
|
9
9
|
@current_array_index = 0
|
10
10
|
sorted_yaml_from_blocks_array
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
private
|
14
|
-
|
14
|
+
|
15
15
|
def break_blocks_into_array
|
16
16
|
array = []
|
17
|
-
|
17
|
+
|
18
18
|
loop do
|
19
|
-
|
19
|
+
|
20
20
|
maybe_next_line = @read_line_again || @io_input.gets || break
|
21
21
|
@read_line_again = nil
|
22
22
|
maybe_next_line.chomp!
|
@@ -26,28 +26,28 @@ module I18nYamlSorter
|
|
26
26
|
|
27
27
|
#Does it look like a key: value line?
|
28
28
|
key_value_parse = maybe_next_line.match(/^(\s*)(["']?[\w\-]+["']?)(: )(\s*)(\S.*\S)(\s*)$/)
|
29
|
-
if key_value_parse
|
29
|
+
if key_value_parse
|
30
30
|
array << maybe_next_line.concat("\n") #yes, it is the beginning of a key:value block
|
31
|
-
|
31
|
+
|
32
32
|
#Special cases when it should add extra lines to the array element (multi line quoted strings)
|
33
|
-
|
33
|
+
|
34
34
|
#Is the value surrounded by quotes?
|
35
35
|
starts_with_quote = key_value_parse[5].match(/^["']/)[0] rescue nil
|
36
36
|
ends_with_quote = key_value_parse[5].match(/[^\\](["'])$/)[1] rescue nil
|
37
|
-
if starts_with_quote and !(starts_with_quote == ends_with_quote)
|
38
|
-
|
37
|
+
if starts_with_quote and !(starts_with_quote == ends_with_quote)
|
38
|
+
|
39
39
|
loop do #Append next lines until we find the closing quote
|
40
40
|
content_line = @io_input.gets || break
|
41
41
|
content_line.chomp!
|
42
42
|
array.last << content_line.concat("\n")
|
43
43
|
break if content_line.match(/[^\\][#{starts_with_quote}]\s*$/)
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
end # if starts_with_quote
|
47
|
-
|
47
|
+
|
48
48
|
next
|
49
|
-
end # if key_value_parse
|
50
|
-
|
49
|
+
end # if key_value_parse
|
50
|
+
|
51
51
|
# Is it a | or > string alue?
|
52
52
|
is_special_string = maybe_next_line.match(/^(\s*)(["']?[\w\-]+["']?)(: )(\s*)([|>])(\s*)$/)
|
53
53
|
if is_special_string
|
@@ -65,18 +65,18 @@ module I18nYamlSorter
|
|
65
65
|
break
|
66
66
|
end
|
67
67
|
end
|
68
|
-
|
68
|
+
|
69
69
|
next
|
70
70
|
end #if is_special_string
|
71
|
-
|
71
|
+
|
72
72
|
# Is it the begining of a multi level hash?
|
73
|
-
is_start_of_hash = maybe_next_line.match(/^(\s*)(["']?[\w\-]+["']?)(:)(\s*)$/)
|
73
|
+
is_start_of_hash = maybe_next_line.match(/^(\s*)(["']?[\w\W\-]+["']?)(:)(\s*)$/)
|
74
74
|
if is_start_of_hash
|
75
75
|
array << maybe_next_line.concat("\n")
|
76
76
|
next
|
77
|
-
end
|
78
|
-
|
79
|
-
#If we got here and nothing was done, this line
|
77
|
+
end
|
78
|
+
|
79
|
+
#If we got here and nothing was done, this line
|
80
80
|
# should probably be merged with the previous one.
|
81
81
|
if array.last
|
82
82
|
array.last << maybe_next_line.concat("\n")
|
@@ -84,34 +84,31 @@ module I18nYamlSorter
|
|
84
84
|
array << maybe_next_line.concat("\n")
|
85
85
|
end
|
86
86
|
end #loop
|
87
|
-
|
88
|
-
#debug:
|
89
|
-
#puts array.join("$$$$$$$$$$$$$$$$$$$$$$\n")
|
90
|
-
|
87
|
+
|
91
88
|
array
|
92
89
|
end
|
93
90
|
|
94
91
|
def sorted_yaml_from_blocks_array(current_block = nil)
|
95
|
-
|
92
|
+
|
96
93
|
unless current_block
|
97
94
|
current_block = @array[@current_array_index]
|
98
95
|
@current_array_index += 1
|
99
96
|
end
|
100
|
-
|
97
|
+
|
101
98
|
out_array = []
|
102
|
-
current_match = current_block.match(/^(\s*)(["']?[\w\-]+["']?)(:)/)
|
99
|
+
current_match = current_block.match(/^(\s*)(["']?[\w\W\-]+["']?)(:)/)
|
103
100
|
current_level = current_match[1] rescue ''
|
104
101
|
current_key = current_match[2].downcase.tr(%q{"'}, "") rescue ''
|
105
102
|
out_array << [current_key, current_block]
|
106
|
-
|
103
|
+
|
107
104
|
loop do
|
108
105
|
next_block = @array[@current_array_index] || break
|
109
106
|
@current_array_index += 1
|
110
|
-
|
111
|
-
current_match = next_block.match(/^(\s*)(["']?[\w\-]+["']?)(:)/) || next
|
107
|
+
|
108
|
+
current_match = next_block.match(/^(\s*)(["']?[\w\W\-]+["']?)(:)/) || next
|
112
109
|
current_key = current_match[2].downcase.tr(%q{"'}, "")
|
113
110
|
next_level = current_match[1]
|
114
|
-
|
111
|
+
|
115
112
|
if current_level.size < next_level.size
|
116
113
|
out_array.last.last << sorted_yaml_from_blocks_array(next_block)
|
117
114
|
elsif current_level.size == next_level.size
|
@@ -121,8 +118,8 @@ module I18nYamlSorter
|
|
121
118
|
break
|
122
119
|
end
|
123
120
|
end
|
124
|
-
|
121
|
+
|
125
122
|
return out_array.sort.map(&:last).join
|
126
123
|
end
|
127
124
|
end
|
128
|
-
end
|
125
|
+
end
|
@@ -45,4 +45,13 @@ describe I18nYamlSorter do
|
|
45
45
|
expect(expected_out.read).to eq(output)
|
46
46
|
end
|
47
47
|
end
|
48
|
+
|
49
|
+
it 'test_should_sort_complex_sample_file_with_special_characters' do
|
50
|
+
open(File.dirname(__FILE__) + '/in_W.yml') do |file|
|
51
|
+
sorter = I18nYamlSorter::Sorter.new(file)
|
52
|
+
open(File.dirname(__FILE__) + '/out_W.yml') do |expected_out|
|
53
|
+
expect(expected_out.read).to eq(sorter.sort)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
48
57
|
end
|
data/spec/in.yml
CHANGED
@@ -1,15 +1,5 @@
|
|
1
1
|
# encoding: UTF-8
|
2
|
-
|
3
|
-
m_13: >
|
4
|
-
Não deve dar problema aqui!
|
5
|
-
Não deve dar problema aqui!
|
6
|
-
Não deve dar problema aqui!
|
7
|
-
Pois às vezes dá.
|
8
|
-
|
9
|
-
o_15: |
|
10
|
-
Não deve dar problema aqui!
|
11
|
-
Pois às vezes dá.
|
12
|
-
|
2
|
+
a_one: José # Comentado
|
13
3
|
b_two:
|
14
4
|
a_1: Joãozinho
|
15
5
|
b_two: |
|
@@ -17,50 +7,53 @@ b_two:
|
|
17
7
|
por exemplo: João,
|
18
8
|
que: gosta de nos dar milhões
|
19
9
|
em dinheiro todos os dias.
|
20
|
-
|
21
10
|
Linhas em Branco com tabulação
|
22
11
|
nos strings devem ser preservadas.
|
23
|
-
|
12
|
+
c_three:
|
13
|
+
a: "Marcelo"
|
14
|
+
"b": Bernardo
|
15
|
+
c_3:
|
16
|
+
klass: Lucas
|
17
|
+
unify: Luiz
|
18
|
+
d_4: Rafael
|
19
|
+
# Comentário de muitas
|
20
|
+
# linhas (goes with d_4)
|
24
21
|
d_four: "Somos chatos
|
25
22
|
gostamos de várias linhas\"
|
26
23
|
e ainda usamos escaping
|
27
24
|
para fuder"
|
28
25
|
e_five: Prefirimos strings sem
|
29
26
|
nenhum tipo de especificação.
|
30
|
-
c_three:
|
31
|
-
a: "Marcelo"
|
32
|
-
d_4: Rafael
|
33
|
-
# Comentário de muitas
|
34
|
-
# linhas (goes with d_4)
|
35
|
-
"b": Bernardo
|
36
|
-
c_3:
|
37
|
-
unify: Luiz
|
38
|
-
klass: Lucas
|
39
27
|
f_six: Dalton
|
40
|
-
a_one: José # Comentado
|
41
28
|
c_three: Gustavo
|
42
29
|
# Comentário (goes with c_three)
|
43
|
-
|
30
|
+
d_four: Oliveira
|
44
31
|
e_five:
|
45
|
-
b_two: Catarina
|
46
32
|
a_one: Rodrigo
|
47
33
|
# Comentário (goes with a_one)
|
34
|
+
b_two: Catarina
|
35
|
+
f_six: Thiago
|
48
36
|
"g-seven": Marcelo
|
49
|
-
|
50
|
-
k_eleven: Ronaldo
|
51
|
-
"l_other123123": Emilio
|
52
|
-
|
37
|
+
h_eight: "Jonivildo"
|
53
38
|
i_nine: Prestes
|
54
|
-
|
55
39
|
j_ten: >
|
56
40
|
Pior quando resolvemos escrever
|
57
41
|
assim, impossível aturar!
|
42
|
+
k_eleven: Ronaldo
|
43
|
+
"l_other123123": Emilio
|
44
|
+
m_13: >
|
45
|
+
Não deve dar problema aqui!
|
46
|
+
Não deve dar problema aqui!
|
47
|
+
Não deve dar problema aqui!
|
48
|
+
Pois às vezes dá.
|
58
49
|
n_14: >
|
59
50
|
Não deve dar problema aqui!
|
60
51
|
Pois às vezes dá.
|
61
52
|
o_15: |
|
62
53
|
Não deve dar problema aqui!
|
63
54
|
Pois às vezes dá.
|
55
|
+
o_15: |
|
56
|
+
Não deve dar problema aqui!
|
57
|
+
Pois às vezes dá.
|
64
58
|
p_16: Não deve dar problema aqui!
|
65
59
|
Pois às vezes dá.
|
66
|
-
h_eight: "Jonivildo"
|
data/spec/in_W.yml
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
m_13: >
|
4
|
+
Não deve dar problema aqui!
|
5
|
+
Não deve dar problema aqui!
|
6
|
+
Não deve dar problema aqui!
|
7
|
+
Pois às vezes dá.
|
8
|
+
|
9
|
+
o_15: |
|
10
|
+
Não deve dar problema aqui!
|
11
|
+
Pois às vezes dá.
|
12
|
+
|
13
|
+
b_(two)²:
|
14
|
+
a_1: Joãozinho
|
15
|
+
b_two: |
|
16
|
+
Nos somos legais, como
|
17
|
+
por exemplo: João,
|
18
|
+
que: gosta de nos dar milhões
|
19
|
+
em dinheiro todos os dias.
|
20
|
+
Linhas em Branco com tabulação
|
21
|
+
nos strings devem ser preservadas.
|
22
|
+
d_four: "Somos chatos
|
23
|
+
gostamos de várias linhas\"
|
24
|
+
e ainda usamos escaping
|
25
|
+
para fuder"
|
26
|
+
e_five: Prefirimos strings sem
|
27
|
+
nenhum tipo de especificação.
|
28
|
+
c_three:
|
29
|
+
a: "Marcelo"
|
30
|
+
g_se/ ven:
|
31
|
+
d_4: Rafael
|
32
|
+
# Comentário de muitas
|
33
|
+
# linhas (goes with d_4)
|
34
|
+
"b": Bernardo
|
35
|
+
c_3:
|
36
|
+
unify: Luiz
|
37
|
+
klass: Lucas
|
38
|
+
f_six: Dalton
|
39
|
+
a_one: José # Comentado
|
40
|
+
c_three: Gustavo
|
41
|
+
# Comentário (goes with c_three)
|
42
|
+
f_six: Thiago
|
43
|
+
e_five:
|
44
|
+
b_two: Catarina
|
45
|
+
a_one: Rodrigo
|
46
|
+
# Comentário (goes with a_one)
|
47
|
+
"g-seven": Marcelo
|
48
|
+
d_four: Oliveira
|
49
|
+
k_eleven: Ronaldo
|
50
|
+
"l_other123123": Emilio
|
51
|
+
|
52
|
+
i_nine: Prestes
|
53
|
+
|
54
|
+
j_ten: >
|
55
|
+
Pior quando resolvemos escrever
|
56
|
+
assim, impossível aturar!
|
57
|
+
n_14: >
|
58
|
+
Não deve dar problema aqui!
|
59
|
+
Pois às vezes dá.
|
60
|
+
o_15: |
|
61
|
+
Não deve dar problema aqui!
|
62
|
+
Pois às vezes dá.
|
63
|
+
p_16: Não deve dar problema aqui!
|
64
|
+
Pois às vezes dá.
|
65
|
+
h_eight: "Jonivildo"
|
data/spec/out.yml
CHANGED
data/spec/out_W.yml
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
a_one: José # Comentado
|
3
|
+
b_(two)²:
|
4
|
+
a_1: Joãozinho
|
5
|
+
b_two: |
|
6
|
+
Nos somos legais, como
|
7
|
+
por exemplo: João,
|
8
|
+
que: gosta de nos dar milhões
|
9
|
+
em dinheiro todos os dias.
|
10
|
+
Linhas em Branco com tabulação
|
11
|
+
nos strings devem ser preservadas.
|
12
|
+
c_three:
|
13
|
+
a: "Marcelo"
|
14
|
+
d_four: "Somos chatos
|
15
|
+
gostamos de várias linhas\"
|
16
|
+
e ainda usamos escaping
|
17
|
+
para fuder"
|
18
|
+
e_five: Prefirimos strings sem
|
19
|
+
nenhum tipo de especificação.
|
20
|
+
f_six: Dalton
|
21
|
+
g_se/ ven:
|
22
|
+
"b": Bernardo
|
23
|
+
c_3:
|
24
|
+
klass: Lucas
|
25
|
+
unify: Luiz
|
26
|
+
d_4: Rafael
|
27
|
+
# Comentário de muitas
|
28
|
+
# linhas (goes with d_4)
|
29
|
+
c_three: Gustavo
|
30
|
+
# Comentário (goes with c_three)
|
31
|
+
d_four: Oliveira
|
32
|
+
e_five:
|
33
|
+
a_one: Rodrigo
|
34
|
+
# Comentário (goes with a_one)
|
35
|
+
b_two: Catarina
|
36
|
+
f_six: Thiago
|
37
|
+
"g-seven": Marcelo
|
38
|
+
h_eight: "Jonivildo"
|
39
|
+
i_nine: Prestes
|
40
|
+
j_ten: >
|
41
|
+
Pior quando resolvemos escrever
|
42
|
+
assim, impossível aturar!
|
43
|
+
k_eleven: Ronaldo
|
44
|
+
"l_other123123": Emilio
|
45
|
+
m_13: >
|
46
|
+
Não deve dar problema aqui!
|
47
|
+
Não deve dar problema aqui!
|
48
|
+
Não deve dar problema aqui!
|
49
|
+
Pois às vezes dá.
|
50
|
+
n_14: >
|
51
|
+
Não deve dar problema aqui!
|
52
|
+
Pois às vezes dá.
|
53
|
+
o_15: |
|
54
|
+
Não deve dar problema aqui!
|
55
|
+
Pois às vezes dá.
|
56
|
+
o_15: |
|
57
|
+
Não deve dar problema aqui!
|
58
|
+
Pois às vezes dá.
|
59
|
+
p_16: Não deve dar problema aqui!
|
60
|
+
Pois às vezes dá.
|
data/spec/out_simple.yml
CHANGED
@@ -9,7 +9,6 @@ en-US:
|
|
9
9
|
Bananas are "nice":
|
10
10
|
- They are <b> sweet </b>.
|
11
11
|
this: not a key
|
12
|
-
|
13
12
|
That is why everyone like bananas!
|
14
13
|
grapes: We dont' eat them.
|
15
14
|
pt-BR:
|
@@ -23,6 +22,5 @@ pt-BR:
|
|
23
22
|
Bananas são "legais":
|
24
23
|
- Elas são <b> doces </b>.
|
25
24
|
isto: não é chave
|
26
|
-
|
27
25
|
Por isto todos gostam de bananas!
|
28
26
|
grapes: Não comemos elas.
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: i18n_yaml_sorter_2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bernardo de Pádua
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2018-04-
|
12
|
+
date: 2018-04-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -66,7 +66,7 @@ extensions: []
|
|
66
66
|
extra_rdoc_files: []
|
67
67
|
files:
|
68
68
|
- LICENSE
|
69
|
-
- README.
|
69
|
+
- README.md
|
70
70
|
- Rakefile
|
71
71
|
- bin/sort_yaml
|
72
72
|
- lib/i18n_yaml_sorter.rb
|
@@ -77,13 +77,14 @@ files:
|
|
77
77
|
- spec/helper.rb
|
78
78
|
- spec/i18n_yaml_sorter_spec.rb
|
79
79
|
- spec/in.yml
|
80
|
+
- spec/in_W.yml
|
80
81
|
- spec/in_rails.yml
|
81
82
|
- spec/in_simple.yml
|
82
83
|
- spec/out.yml
|
84
|
+
- spec/out_W.yml
|
83
85
|
- spec/out_rails.yml
|
84
86
|
- spec/out_simple.yml
|
85
87
|
- spec/spec_helper.rb
|
86
|
-
- spec/test_i18n_yaml_sorter.rb
|
87
88
|
homepage: https://github.com/compwron/i18n_yaml_sorter
|
88
89
|
licenses:
|
89
90
|
- MIT
|
data/README.rdoc
DELETED
@@ -1,236 +0,0 @@
|
|
1
|
-
= i18n_yaml_sorter
|
2
|
-
|
3
|
-
|
4
|
-
A simple YAML Hash deep sorter that will not mess with the way your strings and text values were written. Made exclusively to sort the YAML Hash keys commonly used in the i18n gem and Rails' apps. It will not sort arrays, YAML objects, etc.
|
5
|
-
|
6
|
-
It doesn't parse the YAML and dumps it again, it just sorts the lines, it's pure and simple magic! That way, it guarantees you can keep using your nice YAML constructs to create large text values, as we usually do in i18n files.
|
7
|
-
|
8
|
-
It includes a simple TextMate bundle and a rake task for Rails, so you can keep all those .yml files sorted in no time.
|
9
|
-
|
10
|
-
== Motivation
|
11
|
-
|
12
|
-
As your rails i18n app grows, it becomes very boring to keep all locale files in sync. You have to add all keys in the same order, in different languages, or you will get yourself into a mess and will lose precious time comparing the files in different locales and looking for the appropriate translation keys whenever you want to change something or see if a key is missing.
|
13
|
-
|
14
|
-
If you try deserializing/serializing the yml files using the YAML parser, to get them in the same order, you will figure that your strings in the YAMLs will be "standarized" to whatever the YAML generator prefers (eg.: strings in double quotes, character escapes, etc). It will also mess up your indentation (and use its defaults) and remove your comments.
|
15
|
-
|
16
|
-
i18n_yaml_sorter to the rescue! Add it to TextMate and you will be able to sort your yaml file in nanoseconds. Your file will look pristine, like it was human edited.
|
17
|
-
|
18
|
-
Tested in ruby-1.8.7-p334 and ruby-1.9.2-p290, should work everywhere since it is so simple. Rake task works in Rails 3.1, not sure about older versions though (patch updates are welcome, if you find it doesn't).
|
19
|
-
|
20
|
-
== Simple Example
|
21
|
-
This:
|
22
|
-
|
23
|
-
pt-BR:
|
24
|
-
# Note how this is a nice way of inputing
|
25
|
-
# paragraphs of text in YAML.
|
26
|
-
apples: >
|
27
|
-
Maçãs são boas,
|
28
|
-
só não coma
|
29
|
-
seus iPods!
|
30
|
-
grapes: Não comemos elas.
|
31
|
-
bananas: |
|
32
|
-
Bananas são "legais":
|
33
|
-
- Elas são <b> doces </b>.
|
34
|
-
isto: não é chave
|
35
|
-
|
36
|
-
Por isto todos gostam de bananas!
|
37
|
-
en-US:
|
38
|
-
# Note that our comments are important:
|
39
|
-
# Don't let your yaml sorter delete them!
|
40
|
-
grapes: We dont' eat them.
|
41
|
-
bananas: |
|
42
|
-
Bananas are "nice":
|
43
|
-
- They are <b> sweet </b>.
|
44
|
-
this: not a key
|
45
|
-
|
46
|
-
That is why everyone like bananas!
|
47
|
-
apples: >
|
48
|
-
Apples are fine,
|
49
|
-
just don't eat your
|
50
|
-
iPods!
|
51
|
-
|
52
|
-
|
53
|
-
Becomes:
|
54
|
-
|
55
|
-
en-US:
|
56
|
-
# Note that our comments are important:
|
57
|
-
# Don't let your yaml sorter delete them!
|
58
|
-
apples: >
|
59
|
-
Apples are fine,
|
60
|
-
just don't eat your
|
61
|
-
iPods!
|
62
|
-
bananas: |
|
63
|
-
Bananas are "nice":
|
64
|
-
- They are <b> sweet </b>.
|
65
|
-
this: not a key
|
66
|
-
|
67
|
-
That is why everyone like bananas!
|
68
|
-
grapes: We dont' eat them.
|
69
|
-
pt-BR:
|
70
|
-
# Note how this is a nice way of inputing
|
71
|
-
# paragraphs of text in YAML.
|
72
|
-
apples: >
|
73
|
-
Maçãs são boas,
|
74
|
-
só não coma
|
75
|
-
seus iPods!
|
76
|
-
bananas: |
|
77
|
-
Bananas são "legais":
|
78
|
-
- Elas são <b> doces </b>.
|
79
|
-
isto: não é chave
|
80
|
-
|
81
|
-
Por isto todos gostam de bananas!
|
82
|
-
grapes: Não comemos elas.
|
83
|
-
|
84
|
-
|
85
|
-
What if you use some method that employ's Ruby's YAML library to help
|
86
|
-
do the task for you? You would get an output like that (note it removes your comments):
|
87
|
-
|
88
|
-
en-US:
|
89
|
-
apples: Apples are fine, just don't eat your iPods!
|
90
|
-
bananas: |
|
91
|
-
Bananas are "nice":
|
92
|
-
- They are <b> sweet </b>.
|
93
|
-
this: not a key
|
94
|
-
|
95
|
-
That is why everyone like bananas!
|
96
|
-
|
97
|
-
grapes: We dont' eat them.
|
98
|
-
pt-BR:
|
99
|
-
apples: "Ma\xC3\xA7\xC3\xA3s s\xC3\xA3o boas, s\xC3\xB3 n\xC3\xA3o coma seus iPods!\n"
|
100
|
-
bananas: "Bananas s\xC3\xA3o \"legais\":\n - Elas s\xC3\xA3o <b> doces </b>.\n isto: n\xC3\xA3o \xC3\xA9 chave\n\n\
|
101
|
-
Por isto todos gostam de bananas!\n"
|
102
|
-
grapes: "N\xC3\xA3o comemos elas."
|
103
|
-
|
104
|
-
|
105
|
-
== More complex example
|
106
|
-
|
107
|
-
This:
|
108
|
-
|
109
|
-
b_two:
|
110
|
-
a_1: Simple most common text
|
111
|
-
b_two: |
|
112
|
-
This is the best way of
|
113
|
-
inputing large chunks of text
|
114
|
-
in the YAML files.
|
115
|
-
|
116
|
-
Note that this format keeps blank
|
117
|
-
lines in the same indentation.
|
118
|
-
d_four: "We can also
|
119
|
-
use the boring \"
|
120
|
-
across multiple lines
|
121
|
-
but have to escape then."
|
122
|
-
e_five: Or you can do it
|
123
|
-
like that as well, it also works.
|
124
|
-
c_three:
|
125
|
-
a: "Marcelo"
|
126
|
-
d_4: Rafael
|
127
|
-
# Your comments will be untouched
|
128
|
-
# but will be bound to the element
|
129
|
-
# on top of them (d_4 here).
|
130
|
-
"b": Bernardo
|
131
|
-
c_3:
|
132
|
-
unify: Luiz
|
133
|
-
klass: Lucas
|
134
|
-
a_one: >
|
135
|
-
This is another way
|
136
|
-
of inputing text. It
|
137
|
-
will squish whitespace
|
138
|
-
when deserialized
|
139
|
-
(like HTML does).
|
140
|
-
|
141
|
-
|
142
|
-
Becomes:
|
143
|
-
|
144
|
-
a_one: >
|
145
|
-
This is another way
|
146
|
-
of inputing text. It
|
147
|
-
will squish whitespace
|
148
|
-
when deserialized
|
149
|
-
(like HTML does).
|
150
|
-
b_two:
|
151
|
-
a_1: Simple most common text
|
152
|
-
b_two: |
|
153
|
-
This is the best way of
|
154
|
-
inputing large chunks of text
|
155
|
-
in the YAML files.
|
156
|
-
|
157
|
-
Note that this format keeps blank
|
158
|
-
lines in the same indentation.
|
159
|
-
d_four: "We can also
|
160
|
-
use the boring \"
|
161
|
-
across multiple lines
|
162
|
-
but have to escape then."
|
163
|
-
c_three:
|
164
|
-
a: "Marcelo"
|
165
|
-
"b": Bernardo
|
166
|
-
c_3:
|
167
|
-
klass: Lucas
|
168
|
-
unify: Luiz
|
169
|
-
d_4: Rafael
|
170
|
-
# Your comments will be untouched
|
171
|
-
# but will be bound to the element
|
172
|
-
# on top of them (d_4 here).
|
173
|
-
e_five: Or you can do it
|
174
|
-
like that as well, it also works.
|
175
|
-
|
176
|
-
== Installing
|
177
|
-
|
178
|
-
Easy, just install the gem:
|
179
|
-
|
180
|
-
$ gem install i18n_yaml_sorter
|
181
|
-
|
182
|
-
Then the +sort_yaml+ command line tool will be available. If you prefer using Ruby (in your rakes, etc), use the simple +I18nYamlSorter::Sort+ class.
|
183
|
-
|
184
|
-
== Textmate Bundle
|
185
|
-
|
186
|
-
Run this command in the Terminal to install it:
|
187
|
-
|
188
|
-
$ sort_yaml -i
|
189
|
-
|
190
|
-
A TextMate bundle, named "Yaml Sort" will be installed in your user home path. Press "Shift+Command+S" or use the Bundles menu to invoke it. The opened yaml file (or just the part of it that is selected) will be sorted. To edit the selected part of the file, make sure it is valid YAML by itself, or your yaml file might be corrupted (you can always Undo if you mess up).
|
191
|
-
|
192
|
-
== Rails Rake Taks
|
193
|
-
|
194
|
-
Declare it as a dependency in your app Gemfile, under the development group:
|
195
|
-
|
196
|
-
gem 'i18n_yaml_sorter', :group => :development
|
197
|
-
|
198
|
-
Run bundle install under your Rails' app:
|
199
|
-
|
200
|
-
$ bundle install
|
201
|
-
|
202
|
-
Now run the rake task under your Rails' app to sort all the i18n files in your +config/locales+ dir:
|
203
|
-
|
204
|
-
$rake i18n:sort
|
205
|
-
|
206
|
-
== Command line Input / Output
|
207
|
-
|
208
|
-
+sort_yaml+ will operate on STDIN and STDOUT, so sorting an existing yaml file should be as easy as:
|
209
|
-
|
210
|
-
$ sort_yaml < in.yml > out.yml
|
211
|
-
|
212
|
-
TODO: Add command line arguments parsing and options, so you can, for instance, sort a whole dir of yaml files.
|
213
|
-
|
214
|
-
== Development
|
215
|
-
|
216
|
-
$rake spec # run tests
|
217
|
-
$rake release # build installable/publishable gem
|
218
|
-
|
219
|
-
== Future improvements (Forks Welcome!)
|
220
|
-
|
221
|
-
* Make +sort_yaml+ smart, take directories, etc
|
222
|
-
* Refactoring: internal code is still a bit ugly, but works
|
223
|
-
|
224
|
-
== Note on Patches/Pull Requests
|
225
|
-
|
226
|
-
* Fork the project.
|
227
|
-
* Make your feature addition or bug fix.
|
228
|
-
* Add tests for it. This is important so I don't break it in a
|
229
|
-
future version unintentionally.
|
230
|
-
* Commit, do not mess with rakefile, version, or history.
|
231
|
-
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
232
|
-
* Send me a pull request. Bonus points for topic branches.
|
233
|
-
|
234
|
-
== Copyright
|
235
|
-
|
236
|
-
Copyright (c) 2010-2011 Bernardo de Pádua. MIT License (See LICENCE).
|
@@ -1,48 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/helper'
|
2
|
-
|
3
|
-
class TestI18nYamlSorter < Test::Unit::TestCase
|
4
|
-
def test_should_sort_complex_sample_file
|
5
|
-
open(File.dirname(__FILE__) + '/in.yml') do |file|
|
6
|
-
sorter = I18nYamlSorter::Sorter.new(file)
|
7
|
-
open(File.dirname(__FILE__) + '/out.yml') do |expected_out|
|
8
|
-
assert_equal expected_out.read, sorter.sort
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_should_rails_i18n_default_file
|
14
|
-
open(File.dirname(__FILE__) + '/in_rails.yml') do |file|
|
15
|
-
sorter = I18nYamlSorter::Sorter.new(file)
|
16
|
-
open(File.dirname(__FILE__) + '/out_rails.yml') do |expected_out|
|
17
|
-
assert_equal expected_out.read, sorter.sort
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_should_sort_simple_text_file
|
23
|
-
open(File.dirname(__FILE__) + '/in_simple.yml') do |file|
|
24
|
-
sorter = I18nYamlSorter::Sorter.new(file)
|
25
|
-
open(File.dirname(__FILE__) + '/out_simple.yml') do |expected_out|
|
26
|
-
assert_equal expected_out.read, sorter.sort
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_should_not_alter_the_serialized_yaml
|
32
|
-
#ordering should'n t change a thing, since hashes don't have order in Ruby
|
33
|
-
open(File.dirname(__FILE__) + '/in.yml') do |file|
|
34
|
-
sorter = I18nYamlSorter::Sorter.new(file)
|
35
|
-
present = YAML::load(file.read)
|
36
|
-
file.rewind
|
37
|
-
future = YAML::load(sorter.sort)
|
38
|
-
assert_equal present, future
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_command_line_should_work_in_stdin
|
43
|
-
output = `#{File.dirname(__FILE__)}/../bin/sort_yaml < #{File.dirname(__FILE__)}/in.yml`
|
44
|
-
open(File.dirname(__FILE__) + '/out.yml') do |expected_out|
|
45
|
-
assert_equal expected_out.read, output
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|