gettext_i18n_rails_js 0.0.9 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +9 -0
- data/LICENSE +22 -0
- data/README.md +93 -28
- data/lib/gettext_i18n_rails_js.rb +45 -3
- data/lib/gettext_i18n_rails_js/engine.rb +25 -3
- data/lib/gettext_i18n_rails_js/parser.rb +33 -0
- data/lib/gettext_i18n_rails_js/parser/base.rb +111 -0
- data/lib/gettext_i18n_rails_js/parser/handlebars.rb +84 -0
- data/lib/gettext_i18n_rails_js/parser/javascript.rb +100 -0
- data/lib/gettext_i18n_rails_js/version.rb +38 -1
- data/lib/tasks/gettext_i18n_rails_js_tasks.rake +152 -6
- data/spec/fixtures/example.coffee +15 -0
- data/spec/fixtures/example.handlebars +11 -0
- data/spec/fixtures/example.js +12 -0
- data/spec/gettext_i18n_rails_js/parser/handlebars_spec.rb +301 -0
- data/spec/gettext_i18n_rails_js/parser/javascript_spec.rb +341 -0
- data/spec/gettext_i18n_rails_js_spec.rb +32 -0
- data/spec/spec_helper.rb +58 -0
- data/spec/support/with_file.rb +34 -0
- metadata +107 -73
- data/MIT-LICENSE +0 -20
- data/Rakefile +0 -27
- data/lib/assets/javascripts/gettext/all.js +0 -21
- data/lib/assets/javascripts/gettext/jed.js +0 -1011
- data/lib/gettext_i18n_rails_js/handlebars_parser.rb +0 -65
- data/lib/gettext_i18n_rails_js/js_and_coffee_parser.rb +0 -74
- data/lib/gettext_i18n_rails_js/tasks.rb +0 -54
@@ -0,0 +1,341 @@
|
|
1
|
+
# -*- coding: UTF-8 -*-
|
2
|
+
#
|
3
|
+
# Copyright (c) 2012-2015 Dropmysite.com <https://dropmyemail.com>
|
4
|
+
# Copyright (c) 2015 Webhippie <http://www.webhippie.de>
|
5
|
+
#
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
7
|
+
# a copy of this software and associated documentation files (the
|
8
|
+
# "Software"), to deal in the Software without restriction, including
|
9
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
10
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
12
|
+
# the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be
|
15
|
+
# included in all copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
20
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
21
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
22
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
23
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
24
|
+
#
|
25
|
+
|
26
|
+
require "spec_helper"
|
27
|
+
|
28
|
+
describe GettextI18nRailsJs::Parser::Javascript do
|
29
|
+
let(:parser) { GettextI18nRailsJs::Parser::Javascript }
|
30
|
+
|
31
|
+
describe "#target?" do
|
32
|
+
it "targets .js" do
|
33
|
+
expect(parser.target?("foo/bar/xxx.js")).to be_truthy
|
34
|
+
end
|
35
|
+
|
36
|
+
it "targets .coffee" do
|
37
|
+
expect(parser.target?("foo/bar/xxx.coffee")).to be_truthy
|
38
|
+
end
|
39
|
+
|
40
|
+
it "does not target cows" do
|
41
|
+
expect(parser.target?("foo/bar/xxx.cows")).to be_falsey
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "#parse" do
|
46
|
+
it "finds plural messages" do
|
47
|
+
content = <<-'EOF'
|
48
|
+
bla = n__("xxxx", "yyyy", "zzzz", some_count)
|
49
|
+
EOF
|
50
|
+
|
51
|
+
with_file content do |path|
|
52
|
+
expect(parser.parse(path, [])).to(
|
53
|
+
eq(
|
54
|
+
[
|
55
|
+
["xxxx\000yyyy\000zzzz", "#{path}:1"]
|
56
|
+
]
|
57
|
+
)
|
58
|
+
)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
it "finds namespaced messages" do
|
63
|
+
content = <<-'EOF'
|
64
|
+
bla = __("xxxx", "yyyy")
|
65
|
+
EOF
|
66
|
+
|
67
|
+
with_file content do |path|
|
68
|
+
expect(parser.parse(path, [])).to(
|
69
|
+
eq(
|
70
|
+
[
|
71
|
+
["xxxx\004yyyy", "#{path}:1"]
|
72
|
+
]
|
73
|
+
)
|
74
|
+
)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
it "finds simple messages" do
|
79
|
+
content = <<-'EOF'
|
80
|
+
foo = __("xxxx")
|
81
|
+
EOF
|
82
|
+
|
83
|
+
with_file content do |path|
|
84
|
+
expect(parser.parse(path, [])).to(
|
85
|
+
eq(
|
86
|
+
[
|
87
|
+
["xxxx", "#{path}:1"]
|
88
|
+
]
|
89
|
+
)
|
90
|
+
)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
it "finds messages with newlines/tabs" do
|
95
|
+
content = <<-'EOF'
|
96
|
+
bla = __("xxxx\n\tfoo")
|
97
|
+
EOF
|
98
|
+
|
99
|
+
with_file content do |path|
|
100
|
+
expect(parser.parse(path, [])).to(
|
101
|
+
eq(
|
102
|
+
[
|
103
|
+
["xxxx\\n\\tfoo", "#{path}:1"]
|
104
|
+
]
|
105
|
+
)
|
106
|
+
)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
it "finds messages with newlines/tabs (single quotes)" do
|
111
|
+
content = <<-'EOF'
|
112
|
+
bla = __('xxxx\n\tfoo')
|
113
|
+
EOF
|
114
|
+
|
115
|
+
with_file content do |path|
|
116
|
+
expect(parser.parse(path, [])).to(
|
117
|
+
eq(
|
118
|
+
[
|
119
|
+
["xxxx\\n\\tfoo", "#{path}:1"]
|
120
|
+
]
|
121
|
+
)
|
122
|
+
)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
it "finds interpolated multi-line messages" do
|
127
|
+
content = <<-'EOF'
|
128
|
+
""" Parser should grab
|
129
|
+
#{ __("This") } __("known bug")
|
130
|
+
"""
|
131
|
+
EOF
|
132
|
+
|
133
|
+
with_file content do |path|
|
134
|
+
expect(parser.parse(path, [])).to(
|
135
|
+
eq(
|
136
|
+
[
|
137
|
+
["This", "#{path}:2"],
|
138
|
+
["known bug", "#{path}:2"]
|
139
|
+
]
|
140
|
+
)
|
141
|
+
)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
it "finds strings that use some templating" do
|
146
|
+
content = <<-'EOF'
|
147
|
+
__("hello {yourname}")
|
148
|
+
EOF
|
149
|
+
|
150
|
+
with_file content do |path|
|
151
|
+
expect(parser.parse(path, [])).to(
|
152
|
+
eq(
|
153
|
+
[
|
154
|
+
["hello {yourname}", "#{path}:1"]
|
155
|
+
]
|
156
|
+
)
|
157
|
+
)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
it "finds strings that use escaped strings" do
|
162
|
+
content = <<-'EOF'
|
163
|
+
__("hello \"dude\"") + __('how is it \'going\' ')
|
164
|
+
EOF
|
165
|
+
|
166
|
+
with_file content do |path|
|
167
|
+
expect(parser.parse(path, [])).to(
|
168
|
+
eq(
|
169
|
+
[
|
170
|
+
["hello \\\"dude\\\"", "#{path}:1"],
|
171
|
+
["how is it \\'going\\' ", "#{path}:1"]
|
172
|
+
]
|
173
|
+
)
|
174
|
+
)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
it "does not capture a false positive" do
|
179
|
+
content = <<-'EOF'
|
180
|
+
bla = should_not_be_registered__("xxxx", "yyyy")
|
181
|
+
EOF
|
182
|
+
|
183
|
+
with_file content do |path|
|
184
|
+
expect(parser.parse(path, [])).to(
|
185
|
+
eq(
|
186
|
+
[]
|
187
|
+
)
|
188
|
+
)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
it "does not find nonstring messages" do
|
193
|
+
content = <<-'EOF'
|
194
|
+
bla = __(bar)
|
195
|
+
EOF
|
196
|
+
|
197
|
+
with_file content do |path|
|
198
|
+
expect(parser.parse(path, [])).to(
|
199
|
+
eq(
|
200
|
+
[]
|
201
|
+
)
|
202
|
+
)
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
it "does not parse internal parentheses" do
|
207
|
+
content = <<-'EOF'
|
208
|
+
bla = __("some text (great) and parentheses()") + __('foobar')
|
209
|
+
EOF
|
210
|
+
|
211
|
+
with_file content do |path|
|
212
|
+
expect(parser.parse(path, [])).to(
|
213
|
+
eq(
|
214
|
+
[
|
215
|
+
["some text (great) and parentheses()", "#{path}:1"],
|
216
|
+
["foobar", "#{path}:1"]
|
217
|
+
]
|
218
|
+
)
|
219
|
+
)
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
it "does not parse internal functions" do
|
224
|
+
content = <<-'EOF'
|
225
|
+
bla = n__("items (single)", "i (more)", item.count()) + __('foobar')
|
226
|
+
EOF
|
227
|
+
|
228
|
+
with_file content do |path|
|
229
|
+
expect(parser.parse(path, [])).to(
|
230
|
+
eq(
|
231
|
+
[
|
232
|
+
["items (single)\000i (more)", "#{path}:1"],
|
233
|
+
["foobar", "#{path}:1"]
|
234
|
+
]
|
235
|
+
)
|
236
|
+
)
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
describe "parses javascript files" do
|
242
|
+
let(:example) do
|
243
|
+
File.expand_path(
|
244
|
+
"../../../fixtures/example.js",
|
245
|
+
__FILE__
|
246
|
+
)
|
247
|
+
end
|
248
|
+
|
249
|
+
let(:parsed_example) do
|
250
|
+
parser.parse(example, [])
|
251
|
+
end
|
252
|
+
|
253
|
+
it "parses all translations" do
|
254
|
+
expect(parsed_example).to(
|
255
|
+
eq(
|
256
|
+
[
|
257
|
+
["json", "#{example}:2"],
|
258
|
+
["item\000items", "#{example}:3"],
|
259
|
+
["Hello {yourname}", "#{example}:6"],
|
260
|
+
["new-trans", "#{example}:9"],
|
261
|
+
["namespaced\004trans", "#{example}:10"],
|
262
|
+
["Hello\\nBuddy", "#{example}:11"]
|
263
|
+
]
|
264
|
+
)
|
265
|
+
)
|
266
|
+
end
|
267
|
+
|
268
|
+
it "accepts changing the translate method" do
|
269
|
+
parser.gettext_function = "gettext"
|
270
|
+
|
271
|
+
content = <<-'EOF'
|
272
|
+
var string = \"this\" + gettext('json') + 'should be translated';
|
273
|
+
EOF
|
274
|
+
|
275
|
+
with_file content do |path|
|
276
|
+
expect(parser.parse(path, [])).to(
|
277
|
+
eq(
|
278
|
+
[
|
279
|
+
["json", "#{path}:1"]
|
280
|
+
]
|
281
|
+
)
|
282
|
+
)
|
283
|
+
end
|
284
|
+
|
285
|
+
parser.gettext_function = "__"
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
describe "parses coffee files" do
|
290
|
+
let(:example) do
|
291
|
+
File.expand_path(
|
292
|
+
"../../../fixtures/example.coffee",
|
293
|
+
__FILE__
|
294
|
+
)
|
295
|
+
end
|
296
|
+
|
297
|
+
let(:parsed_example) do
|
298
|
+
parser.parse(example, [])
|
299
|
+
end
|
300
|
+
|
301
|
+
it "parses all translations" do
|
302
|
+
expect(parsed_example).to(
|
303
|
+
eq(
|
304
|
+
[
|
305
|
+
["json", "#{example}:2"],
|
306
|
+
["item\000items", "#{example}:3"],
|
307
|
+
["Hello {yourname}", "#{example}:5"],
|
308
|
+
["new-trans", "#{example}:8"],
|
309
|
+
["namespaced\004trans", "#{example}:9"],
|
310
|
+
["Hello\\nBuddy", "#{example}:11"],
|
311
|
+
["Multi-line", "#{example}:14"]
|
312
|
+
]
|
313
|
+
)
|
314
|
+
)
|
315
|
+
end
|
316
|
+
|
317
|
+
it "accepts changing the translate method" do
|
318
|
+
parser.gettext_function = "gettext"
|
319
|
+
|
320
|
+
content = <<-'EOF'
|
321
|
+
string = \"this\" + gettext('json')
|
322
|
+
|
323
|
+
object =
|
324
|
+
one: ngettext('new-trans')
|
325
|
+
EOF
|
326
|
+
|
327
|
+
with_file content do |path|
|
328
|
+
expect(parser.parse(path, [])).to(
|
329
|
+
eq(
|
330
|
+
[
|
331
|
+
["json", "#{path}:1"],
|
332
|
+
["new-trans", "#{path}:4"]
|
333
|
+
]
|
334
|
+
)
|
335
|
+
)
|
336
|
+
end
|
337
|
+
|
338
|
+
parser.gettext_function = "__"
|
339
|
+
end
|
340
|
+
end
|
341
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# -*- coding: UTF-8 -*-
|
2
|
+
#
|
3
|
+
# Copyright (c) 2012-2015 Dropmysite.com <https://dropmyemail.com>
|
4
|
+
# Copyright (c) 2015 Webhippie <http://www.webhippie.de>
|
5
|
+
#
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
7
|
+
# a copy of this software and associated documentation files (the
|
8
|
+
# "Software"), to deal in the Software without restriction, including
|
9
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
10
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
12
|
+
# the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be
|
15
|
+
# included in all copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
20
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
21
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
22
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
23
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
24
|
+
#
|
25
|
+
|
26
|
+
require "spec_helper"
|
27
|
+
|
28
|
+
describe GettextI18nRailsJs do
|
29
|
+
it "has a valid version" do
|
30
|
+
expect(GettextI18nRailsJs::Version.to_s).to match(/^\d+\.\d+\.\d+$/)
|
31
|
+
end
|
32
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# -*- coding: UTF-8 -*-
|
2
|
+
#
|
3
|
+
# Copyright (c) 2012-2015 Dropmysite.com <https://dropmyemail.com>
|
4
|
+
# Copyright (c) 2015 Webhippie <http://www.webhippie.de>
|
5
|
+
#
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
7
|
+
# a copy of this software and associated documentation files (the
|
8
|
+
# "Software"), to deal in the Software without restriction, including
|
9
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
10
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
12
|
+
# the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be
|
15
|
+
# included in all copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
20
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
21
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
22
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
23
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
24
|
+
#
|
25
|
+
|
26
|
+
require "simplecov"
|
27
|
+
|
28
|
+
if ENV["CODECLIMATE_REPO_TOKEN"]
|
29
|
+
require "coveralls"
|
30
|
+
require "codeclimate-test-reporter"
|
31
|
+
|
32
|
+
Coveralls.wear!
|
33
|
+
CodeClimate::TestReporter.start
|
34
|
+
|
35
|
+
SimpleCov.start do
|
36
|
+
add_filter "/spec"
|
37
|
+
|
38
|
+
formatter SimpleCov::Formatter::MultiFormatter[
|
39
|
+
SimpleCov::Formatter::HTMLFormatter,
|
40
|
+
CodeClimate::TestReporter::Formatter
|
41
|
+
]
|
42
|
+
end
|
43
|
+
else
|
44
|
+
SimpleCov.start do
|
45
|
+
add_filter "/spec"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
require "gettext_i18n_rails_js"
|
50
|
+
require "rspec"
|
51
|
+
|
52
|
+
Dir[File.expand_path("../support/**/*.rb", __FILE__)].each do |file|
|
53
|
+
require file
|
54
|
+
end
|
55
|
+
|
56
|
+
RSpec.configure do |config|
|
57
|
+
config.mock_with :rspec
|
58
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# -*- coding: UTF-8 -*-
|
2
|
+
#
|
3
|
+
# Copyright (c) 2012-2015 Dropmysite.com <https://dropmyemail.com>
|
4
|
+
# Copyright (c) 2015 Webhippie <http://www.webhippie.de>
|
5
|
+
#
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
7
|
+
# a copy of this software and associated documentation files (the
|
8
|
+
# "Software"), to deal in the Software without restriction, including
|
9
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
10
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
12
|
+
# the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be
|
15
|
+
# included in all copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
20
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
21
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
22
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
23
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
24
|
+
#
|
25
|
+
|
26
|
+
def with_file(content)
|
27
|
+
require "tempfile"
|
28
|
+
|
29
|
+
Tempfile.open("gettext_i18n_rails_js") do |f|
|
30
|
+
f.write(content)
|
31
|
+
f.close
|
32
|
+
yield f.path
|
33
|
+
end
|
34
|
+
end
|