forked from ruby-gettext/gettext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_msginit.rb
More file actions
466 lines (398 loc) · 13.1 KB
/
test_msginit.rb
File metadata and controls
466 lines (398 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# Copyright (C) 2012 Haruka Yoshihara <yoshihara@clear-code.com>
# Copyright (C) 2012-2023 Sutou Kouhei <kou@clear-code.com>
#
# License: Ruby's or LGPL
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
require "gettext/tools/msginit"
class TestToolsMsgInit < Test::Unit::TestCase
def setup
@msginit = GetText::Tools::MsgInit.new
stub(@msginit).read_translator_name {translator_name}
stub(@msginit).read_translator_email {translator_email}
@year = "2012"
@po_revision_date = "2012-09-11 13:19+0900"
@pot_create_date = "2012-08-24 11:35+0900"
stub(@msginit).year {@year}
stub(@msginit).revision_date {@po_revision_date}
Locale.current = "ja_JP.UTF-8"
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
yield
end
end
end
private
def current_locale
Locale.current.to_simple.to_s
end
def current_language
Locale.current.language
end
def current_charset
Locale.current.charset
end
def translator_name
"me"
end
def translator_email
"me@example.com"
end
def template_charset
"CHARSET"
end
def create_pot_file(path, options=nil)
options ||= {}
File.open(path, "w") do |pot_file|
pot_file.puts(pot_header(options))
end
end
def default_po_header_options
{
:package_name => default_package_name,
:first_translator_name => translator_name,
:translator_name => translator_name,
:translator_email => translator_email,
}
end
def pot_header(options)
options = default_po_header_options.merge(options)
package_name = options[:package_name] || default_package_name
have_plural_forms = options[:have_plural_forms] || true
charset = options[:charset] || "UTF-8"
header = <<EOF
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: #{package_name} VERSION\\n"
"POT-Creation-Date: #{@pot_create_date}\\n"
"PO-Revision-Date: #{@pot_create_date}\\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
"Language: \\n"
"Language-Team: LANGUAGE <LL@li.org>\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=#{charset}\\n"
"Content-Transfer-Encoding: 8bit\\n"
EOF
if have_plural_forms
header += "Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n"
end
header
end
def po_header(locale, language, options={})
options = default_po_header_options.merge(options)
package_name = options[:package_name]
first_translator_name = options[:first_translator_name] || "FIRST AUTHOR"
name = options[:translator_name] || "FULL NAME"
email = options[:translator_email] || "EMAIL@ADDRESS"
language_name = Locale::Info.get_language(language).name
charset = options[:charset] || "UTF-8"
plural_forms = @msginit.send(:plural_forms, language)
<<EOF
# #{language_name} translations for #{package_name} package.
# Copyright (C) #{@year} THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# #{first_translator_name} <#{email}>, #{@year}.
#
msgid ""
msgstr ""
"Project-Id-Version: #{package_name} VERSION\\n"
"POT-Creation-Date: #{@pot_create_date}\\n"
"PO-Revision-Date: #{@po_revision_date}\\n"
"Last-Translator: #{name} <#{email}>\\n"
"Language: #{locale}\\n"
"Language-Team: #{language_name}\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=#{charset}\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Plural-Forms: #{plural_forms}\\n"
EOF
end
def default_package_name
"PACKAGE"
end
class TestInput < self
def test_specify_option
pot_dir = "sub"
FileUtils.mkdir_p(pot_dir)
pot_file_path = File.join(pot_dir, "test.pot")
create_pot_file(pot_file_path)
@msginit.run("--input", pot_file_path)
po_file_path = "#{current_locale}.po"
assert_path_exist(po_file_path)
end
def test_find_pot
pot_in_current_directory = "test.pot"
create_pot_file(pot_in_current_directory)
@msginit.run
po_file_path = "#{current_locale}.po"
assert_path_exist(po_file_path)
end
def test_pot_not_found
pot_dir = "sub"
FileUtils.mkdir_p(pot_dir)
pot_file_path = File.join(pot_dir, "test.pot")
create_pot_file(pot_file_path)
assert_raise(GetText::Tools::MsgInit::ValidationError) do
@msginit.run
end
end
end
class TestOutput < self
def setup
super do
@pot_file_path = "test.pot"
create_pot_file(@pot_file_path)
yield
end
end
def test_default
@msginit.run("--input", @pot_file_path)
po_file_path = "#{current_locale}.po"
assert_path_exist(po_file_path)
end
def test_specify_option
po_dir = "sub"
FileUtils.mkdir_p(po_dir)
po_file_path = File.join(po_dir, "test.po")
@msginit.run("--input", @pot_file_path,
"--output", po_file_path)
assert_path_exist(po_file_path)
end
end
class TestLocale < self
def run_msginit(locale, pot_charset=nil)
create_pot_file("test.pot", charset: pot_charset)
po_file_path = "output.po"
@msginit.run("--output", po_file_path,
"--locale", locale)
File.read(po_file_path)
end
def test_language
locale = "en"
assert_equal(po_header(locale, locale),
run_msginit(locale))
end
def test_language_region
locale = "en_US"
language = "en"
assert_equal(po_header(locale, language),
run_msginit(locale))
end
def test_language_region_charset
locale = "en_US"
language = "en"
charset = "UTF-8"
assert_equal(po_header(locale, language),
run_msginit("#{locale}.#{charset}"))
end
def test_language_charset_with_template_charset
locale = "en"
assert_equal(po_header(locale, locale),
run_msginit(locale, template_charset))
end
def test_language_region_with_template_charset
locale = "en_US"
language = "en"
assert_equal(po_header(locale, language),
run_msginit(locale, template_charset))
end
def test_language_region_charset_with_template_charset
locale = "en_US"
language = "en"
charset = "UTF-8"
assert_equal(po_header(locale, language),
run_msginit("#{locale}.#{charset}", template_charset))
end
end
class TestCurrentCharset < self
def run_msginit(pot_charset)
create_pot_file("test.pot", charset: pot_charset)
po_file_path = "output.po"
@msginit.run("--output", po_file_path)
File.read(po_file_path)
end
def po_header(options)
super(current_locale, current_language, options)
end
def test_template_charset
assert_equal(po_header(charset: current_charset),
run_msginit(template_charset))
end
def test_no_template_charset
assert_equal(po_header(charset: "ASCII"),
run_msginit("ASCII"))
end
end
class TestTranslator < self
def run_msginit(*arguments)
create_pot_file("test.pot")
po_file_path = "output.po"
@msginit.run("--output", po_file_path,
*arguments)
File.read(po_file_path)
end
class TestChanged < self
def po_header(options)
super(current_locale, current_language, options)
end
def test_name
name = "Custom Translator"
assert_equal(po_header(:first_translator_name => name,
:translator_name => name),
run_msginit("--translator-name", name))
end
def test_email
email = "custom-translator@example.com"
assert_equal(po_header(:translator_email => email),
run_msginit("--translator-email", email))
end
end
class TestNotChanged < self
def no_translator_po_header
po_header(current_locale, current_language,
:first_translator_name => nil,
:translator_name => nil,
:translator_email => nil)
end
def test_no_name_no_email
stub(@msginit).read_translator_name {nil}
stub(@msginit).read_translator_email {nil}
assert_equal(no_translator_po_header,
run_msginit)
end
def test_no_name
stub(@msginit).read_translator_name {nil}
assert_equal(no_translator_po_header,
run_msginit)
end
def test_no_email
stub(@msginit).read_translator_email {nil}
assert_equal(no_translator_po_header,
run_msginit)
end
def test_no_translator
assert_equal(no_translator_po_header,
run_msginit("--no-translator"))
end
end
end
class TestPackage < self
def run_msginit
create_pot_file("test.pot")
po_file_path = "output.po"
@msginit.run("--output", po_file_path)
File.read(po_file_path)
end
class TestCustomPackageName < self
def default_po_header_options
super.merge(:package_name => "test-package")
end
def test_not_change
assert_equal(po_header(current_locale, current_language),
run_msginit)
end
end
end
class TestPluralForms < self
def setup
omit("Red Datasets is required") unless defined?(Datasets::CLDRPlurals)
super do
yield
end
end
def run_msginit(pot_header_options={})
create_pot_file("test.pot", pot_header_options)
po_file_path = "output.po"
@msginit.run("--output", po_file_path)
File.read(po_file_path)
end
class TestNoInPot < self
def test_no_plural_forms_in_pot
assert_equal(po_header(current_locale, current_language),
run_msginit(:have_plural_forms => false))
end
end
def test_ja
assert_equal("nplurals=1; plural=0;",
@msginit.__send__(:plural_forms, "ja"))
end
def test_en
assert_equal("nplurals=2; plural=n != 1;",
@msginit.__send__(:plural_forms, "en"))
end
def test_fr
assert_equal("nplurals=2; plural=n > 1;",
@msginit.__send__(:plural_forms, "fr"))
end
def test_lv
assert_equal("nplurals=3; " +
"plural=((n % 10) == 1) && ((n % 100) != 11) ? 0 : " +
"((n % 10) == 0) || ((n % 100) >= 11 && (n % 100) <= 19) " +
"? 1 : 2;",
@msginit.__send__(:plural_forms, "lv"))
end
def test_ga
assert_equal("nplurals=5; " +
"plural=(n == 1) ? 0 : (n == 2) ? 1 : " +
"(n >= 3 && n <= 6) ? 2 : (n >= 7 && n <= 10) ? 3 : 4;",
@msginit.__send__(:plural_forms, "ga"))
end
def test_ro
assert_equal("nplurals=3; " +
"plural=(n == 1) ? 0 : " +
"(n == 0) || (n != 1) && ((n % 100) >= 1 && (n % 100) <= 19) ? 1 : 2;",
@msginit.__send__(:plural_forms, "ro"))
end
def test_lt
assert_equal("nplurals=3; " +
"plural=((n % 10) == 1) && " +
"((n % 100) < 11 || (n % 100) > 19) ? 0 : " +
"((n % 10) >= 2 && (n % 10) <= 9) && " +
"((n % 100) < 11 || (n % 100) > 19) ? 1 : 2;",
@msginit.__send__(:plural_forms, "lt"))
end
def test_ru
assert_equal("nplurals=3; " +
"plural=((n % 10) == 1) && ((n % 100) != 11) ? 0 : " +
"((n % 10) >= 2 && (n % 10) <= 4) && " +
"((n % 100) < 12 || (n % 100) > 14) ? 1 : 2;",
@msginit.__send__(:plural_forms, "ru"))
end
def test_cs
assert_equal("nplurals=3; " +
"plural=(n == 1) ? 0 : (n >= 2 && n <= 4) ? 1 : 2;",
@msginit.__send__(:plural_forms, "cs"))
end
def test_pl
assert_equal("nplurals=3; " +
"plural=(n == 1) ? 0 : " +
"((n % 10) >= 2 && (n % 10) <= 4) && " +
"((n % 100) < 12 || (n % 100) > 14) ? 1 : 2;",
@msginit.__send__(:plural_forms, "pl"))
end
def test_sl
assert_equal("nplurals=4; " +
"plural=((n % 100) == 1) ? 0 : " +
"((n % 100) == 2) ? 1 : " +
"((n % 100) >= 3 && (n % 100) <= 4) ? 2 : 3;",
@msginit.__send__(:plural_forms, "sl"))
end
end
end