simple_po_parser 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.rspec +3 -0
- data/.travis.yml +8 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +58 -0
- data/LICENSE.txt +22 -0
- data/README.md +30 -0
- data/Rakefile +68 -0
- data/lib/simple_po_parser/error.rb +13 -0
- data/lib/simple_po_parser/parser.rb +352 -0
- data/lib/simple_po_parser/tokenizer.rb +21 -0
- data/lib/simple_po_parser/version.rb +3 -0
- data/lib/simple_po_parser.rb +21 -0
- data/simple_po_parser.gemspec +26 -0
- data/spec/simple_po_parser/fixtures/complex_entry.po +21 -0
- data/spec/simple_po_parser/fixtures/header.po +7 -0
- data/spec/simple_po_parser/fixtures/simple_entry.po +6 -0
- data/spec/simple_po_parser/parser_spec.rb +49 -0
- data/spec/spec_helper.rb +24 -0
- data/spec/utils/random_pofile_generator.rb +175 -0
- data/test/benchmark.po +683 -0
- metadata +114 -0
@@ -0,0 +1,49 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe SimplePoParser::Parser do
|
4
|
+
let (:po_header) { File.read(File.expand_path("fixtures/header.po", __dir__))}
|
5
|
+
let(:po_complex_message) { File.read(File.expand_path("fixtures/complex_entry.po", __dir__))}
|
6
|
+
let(:po_simple_message) { File.read(File.expand_path("fixtures/simple_entry.po", __dir__))}
|
7
|
+
|
8
|
+
it "parses the PO header" do
|
9
|
+
expected_result = {
|
10
|
+
:translator_comment => ["PO Header entry", ""],
|
11
|
+
:flag => ["fuzzy"],
|
12
|
+
:msgid => [""],
|
13
|
+
:msgstr => ["", "Project-Id-Version: simple_po_parser 1\\n", "Report-Msgid-Bugs-To: me\\n"]
|
14
|
+
}
|
15
|
+
expect(SimplePoParser::Parser.parse(po_header)).to eq(expected_result)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "parses the simple entry as expected" do
|
19
|
+
expected_result = {
|
20
|
+
:translator_comment => ["translator-comment"],
|
21
|
+
:extracted_comment => ["extract"],
|
22
|
+
:reference => ["reference1"],
|
23
|
+
:msgctxt => ["Context"],
|
24
|
+
:msgid => ["msgid"],
|
25
|
+
:msgstr => ["translated"]
|
26
|
+
}
|
27
|
+
expect(SimplePoParser::Parser.parse(po_simple_message)).to eq(expected_result)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "parses the complex entry as expected" do
|
31
|
+
expected_result = {
|
32
|
+
:translator_comment => ["translator-comment", ""],
|
33
|
+
:extracted_comment => ["extract"],
|
34
|
+
:reference => ["reference1", "reference2"],
|
35
|
+
:flag => ["flag"],
|
36
|
+
:previous_msgctxt => ["previous context"],
|
37
|
+
:previous_msgid => ["", "multiline\\n", "previous messageid"],
|
38
|
+
:previous_msgid_plural => ["previous msgid_plural"],
|
39
|
+
:msgctxt => ["Context"],
|
40
|
+
:msgid => ["msgid"],
|
41
|
+
:msgid_plural => ["", "multiline msgid_plural\\n", ""],
|
42
|
+
"msgstr[0]" => ["msgstr 0"],
|
43
|
+
"msgstr[1]" => ["", "msgstr 1 multiline 1\\n", "msgstr 1 line 2\\n"],
|
44
|
+
"msgstr[2]" => ["msgstr 2"]
|
45
|
+
}
|
46
|
+
expect(SimplePoParser::Parser.parse(po_complex_message)).to eq(expected_result)
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
require 'simple_po_parser'
|
9
|
+
require 'coveralls'
|
10
|
+
require 'awesome_print'
|
11
|
+
|
12
|
+
Coveralls.wear!
|
13
|
+
|
14
|
+
RSpec.configure do |config|
|
15
|
+
config.raise_errors_for_deprecations!
|
16
|
+
config.run_all_when_everything_filtered = true
|
17
|
+
config.filter_run :focus
|
18
|
+
|
19
|
+
# Run specs in random order to surface order dependencies. If you find an
|
20
|
+
# order dependency and want to debug it, you can fix the order by providing
|
21
|
+
# the seed, which is printed after each run.
|
22
|
+
# --seed 1234
|
23
|
+
config.order = 'random'
|
24
|
+
end
|
@@ -0,0 +1,175 @@
|
|
1
|
+
module SimplePoParser
|
2
|
+
module RandomPoFileGenerator
|
3
|
+
require 'securerandom'
|
4
|
+
def self.generate_file(file_path, length = 1000, obsoletes = 10)
|
5
|
+
header = "# PO benchmark file header
|
6
|
+
#
|
7
|
+
#, fuzzy
|
8
|
+
msgid \"\"
|
9
|
+
msgstr \"\"
|
10
|
+
\"Project-Id-Version: somehwat master\\n\"
|
11
|
+
\"Report-Msgid-Bugs-To: \\n\"
|
12
|
+
\"Last-Translator: last t <last.transh@e.mail>\\n\"
|
13
|
+
\"Language-Team: team\\n\"
|
14
|
+
\"Content-Type: text/plain; charset=UTF-8\\n\"
|
15
|
+
\"MIME-Version: 1.0\\n\"
|
16
|
+
\"Content-Transfer-Encoding: 8bit\\n\"
|
17
|
+
\"Plural-Forms: nplurals=1; plural=0;\\n\""
|
18
|
+
|
19
|
+
@random = Random.new
|
20
|
+
File.open(file_path, 'w+') do |file|
|
21
|
+
file.write header
|
22
|
+
file.write "\n\n"
|
23
|
+
for i in 0..length-obsoletes do
|
24
|
+
file.write generate_random_message
|
25
|
+
end
|
26
|
+
|
27
|
+
for i in 0...obsoletes do
|
28
|
+
file.write generate_random_message(true)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def self.generate_random_message(obsolete = false)
|
37
|
+
random_message = ""
|
38
|
+
|
39
|
+
untranslated_chance = 0.05
|
40
|
+
fuzzy_chance = 0.1
|
41
|
+
plural_chance = 0.1
|
42
|
+
multiline_chance = 0.1
|
43
|
+
translator_comment_chance = 0.2
|
44
|
+
extracted_comment_chance = 0.05
|
45
|
+
msgctxt_chance = 0.9
|
46
|
+
reference_chance = 0.9
|
47
|
+
multiple_reference_chance = 0.1
|
48
|
+
|
49
|
+
plural = @random.rand < plural_chance ? true : false
|
50
|
+
untranslated = @random.rand < untranslated_chance ? true : false
|
51
|
+
fuzzy = !untranslated && @random.rand < fuzzy_chance ? true : false
|
52
|
+
multiline = @random.rand < multiline_chance ? true : false
|
53
|
+
translator_comment = @random.rand < translator_comment_chance ? true : false
|
54
|
+
extracted_comment = @random.rand < extracted_comment_chance ? true : false
|
55
|
+
reference = @random.rand < reference_chance ? true : false
|
56
|
+
multiple_reference = @random.rand < multiple_reference_chance ? true : false
|
57
|
+
with_msgctxt = @random.rand < msgctxt_chance ? true : false
|
58
|
+
|
59
|
+
msgctxt = with_msgctxt ? SecureRandom.base64((@random.rand*70.0).ceil) : nil
|
60
|
+
if multiline
|
61
|
+
lines = (@random.rand*4.0).ceil
|
62
|
+
msgid = []
|
63
|
+
msgstr = []
|
64
|
+
for i in 0..lines
|
65
|
+
msgid[i] = SecureRandom.base64((@random.rand*70.0).ceil)
|
66
|
+
msgstr[i] = SecureRandom.base64((@random.rand*70.0).ceil)
|
67
|
+
end
|
68
|
+
else
|
69
|
+
msgid = SecureRandom.base64((@random.rand*150.0).ceil)
|
70
|
+
msgstr = SecureRandom.base64((@random.rand*150.0).ceil)
|
71
|
+
end
|
72
|
+
if plural
|
73
|
+
if multiline
|
74
|
+
msgid_plural = []
|
75
|
+
msgstr_plural = []
|
76
|
+
for i in 0..lines
|
77
|
+
msgid_plural[i] = SecureRandom.base64((@random.rand*70.0).ceil)
|
78
|
+
msgstr_plural[i] = SecureRandom.base64((@random.rand*70.0).ceil)
|
79
|
+
end
|
80
|
+
else
|
81
|
+
msgid_plural = SecureRandom.base64((@random.rand*150.0).ceil)
|
82
|
+
msgstr_plural = SecureRandom.base64((@random.rand*70.0).ceil)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
random_message += "# #{SecureRandom.base64((@random.rand*50.0).to_i)}\n" if translator_comment
|
87
|
+
random_message += "#. #{SecureRandom.base64((@random.rand*50.0).to_i)}\n" if extracted_comment
|
88
|
+
random_message += "#: #{SecureRandom.base64((@random.rand*50.0).to_i)}\n" if reference
|
89
|
+
if multiple_reference
|
90
|
+
references = (@random.rand*3.0).ceil
|
91
|
+
for i in 0..references
|
92
|
+
random_message += "#: #{SecureRandom.base64((@random.rand*50.0).to_i)}\n"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
if fuzzy
|
96
|
+
random_message += "#, fuzzy\n"
|
97
|
+
random_message += "##{"~" if obsolete}| msgctxt \"#{msgctxt}\"\n" if msgctxt
|
98
|
+
if msgid.is_a?(Array)
|
99
|
+
random_message += "##{"~" if obsolete}| msgid \"\"\n"
|
100
|
+
msgid.each do |line|
|
101
|
+
random_message += "##{"~" if obsolete}| \"#{line}\\n\"\n"
|
102
|
+
end
|
103
|
+
else
|
104
|
+
random_message += "##{"~" if obsolete}| msgid \"#{msgid}\"\n"
|
105
|
+
end
|
106
|
+
if plural
|
107
|
+
if msgid_plural.is_a?(Array)
|
108
|
+
random_message += "##{"~" if obsolete}| msgid_plural \"\"\n"
|
109
|
+
msgid_plural.each do |line|
|
110
|
+
random_message += "##{"~" if obsolete}| \"#{line}\\n\"\n"
|
111
|
+
end
|
112
|
+
else
|
113
|
+
random_message += "##{"~" if obsolete}| msgid_plural \"#{msgid_plural}\"\n"
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
random_message += "#{"#~ " if obsolete}msgctxt \"#{msgctxt}\"\n" if msgctxt
|
118
|
+
if msgid.is_a?(Array)
|
119
|
+
random_message += "#{"#~ " if obsolete}msgid \"\"\n"
|
120
|
+
msgid.each do |line|
|
121
|
+
random_message += "#{"#~ " if obsolete}\"#{line}\\n\"\n"
|
122
|
+
end
|
123
|
+
else
|
124
|
+
random_message += "#{"#~ " if obsolete}msgid \"#{msgid}\"\n"
|
125
|
+
end
|
126
|
+
if plural
|
127
|
+
if msgid_plural.is_a?(Array)
|
128
|
+
random_message += "#{"#~ " if obsolete}msgid_plural \"\"\n"
|
129
|
+
msgid_plural.each do |line|
|
130
|
+
random_message += "#{"#~ " if obsolete}\"#{line}\\n\"\n"
|
131
|
+
end
|
132
|
+
else
|
133
|
+
random_message += "#{"#~ " if obsolete}msgid_plural \"#{msgid_plural}\"\n"
|
134
|
+
end
|
135
|
+
if untranslated
|
136
|
+
random_message += "#{"#~ " if obsolete}msgstr[0] \"\"\n"
|
137
|
+
random_message += "#{"#~ " if obsolete}msgstr[1] \"\"\n"
|
138
|
+
else
|
139
|
+
if msgstr.is_a?(Array)
|
140
|
+
random_message += "#{"#~ " if obsolete}msgstr[0] \"\"\n"
|
141
|
+
msgstr.each do |line|
|
142
|
+
random_message += "#{"#~ " if obsolete}\"#{line}\\n\"\n"
|
143
|
+
end
|
144
|
+
else
|
145
|
+
random_message += "#{"#~ " if obsolete}msgstr[0] \"#{msgstr}\"\n"
|
146
|
+
end
|
147
|
+
if msgstr_plural.is_a?(Array)
|
148
|
+
random_message += "#{"#~ " if obsolete}msgstr[1] \"\"\n"
|
149
|
+
msgstr_plural.each do |line|
|
150
|
+
random_message += "#{"#~ " if obsolete}\"#{line}\\n\"\n"
|
151
|
+
end
|
152
|
+
else
|
153
|
+
random_message += "#{"#~ " if obsolete}msgstr[1] \"#{msgstr}\"\n"
|
154
|
+
end
|
155
|
+
end
|
156
|
+
else
|
157
|
+
if untranslated
|
158
|
+
random_message += "#{"#~ " if obsolete}msgstr \"\"\n"
|
159
|
+
else
|
160
|
+
if msgstr.is_a?(Array)
|
161
|
+
random_message += "#{"#~ " if obsolete}msgstr \"\"\n"
|
162
|
+
msgstr.each do |line|
|
163
|
+
random_message += "#{"#~ " if obsolete}\"#{line}\\n\"\n"
|
164
|
+
end
|
165
|
+
else
|
166
|
+
random_message += "#{"#~ " if obsolete}msgstr \"#{msgstr}\"\n"
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
random_message += "\n"
|
171
|
+
random_message
|
172
|
+
end
|
173
|
+
|
174
|
+
end
|
175
|
+
end
|