Skip to content

Commit d78b163

Browse files
committed
msginit: Replace charset in Content-type with charset in locale
`getext` is generated with `charset=CHARSET`. Translation as is will result in an error because `CHARSET` is an invalid value. Changed so that `charset=CHARSET` is replaced by the locale's charset.
1 parent 17ad4cd commit d78b163

File tree

4 files changed

+70
-10
lines changed

4 files changed

+70
-10
lines changed

lib/gettext/po_parser.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#
1010
# DO NOT MODIFY!!!!
11-
# This file is automatically generated by Racc 1.7.1
11+
# This file is automatically generated by Racc 1.5.1
1212
# from Racc grammar file "".
1313
#
1414

@@ -164,7 +164,7 @@ def on_comment(comment)
164164
if comment.start_with?(POFormat::FLAG_MARK)
165165
content = comment[POFormat::FLAG_MARK.size..-1]
166166
flags = parse_flags_line(content)
167-
@fuzzy = true if flags.include?("fuzzy")
167+
@fuzzy = flags.include?("fuzzy")
168168
end
169169
if @data.instance_of?(PO)
170170
if comment == "#"
@@ -350,7 +350,6 @@ def clear
350350
racc_shift_n,
351351
racc_reduce_n,
352352
racc_use_result_var ]
353-
Ractor.make_shareable(Racc_arg) if defined?(Ractor)
354353

355354
Racc_token_to_s_table = [
356355
"$end",
@@ -372,7 +371,6 @@ def clear
372371
"plural_message",
373372
"msgstr_plural",
374373
"msgstr_plural_line" ]
375-
Ractor.make_shareable(Racc_token_to_s_table) if defined?(Ractor)
376374

377375
Racc_debug_parser = true
378376

lib/gettext/tools/msginit.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def initialize
6262
@output_file = nil
6363
@locale = nil
6464
@language = nil
65+
@charset = nil
6566
@entry = nil
6667
@comment = nil
6768
@translator = nil
@@ -180,6 +181,11 @@ def validate
180181
end
181182
@locale = language_tag.to_simple.to_s
182183
@language = language_tag.language
184+
if language_tag.respond_to?(:charset)
185+
@charset = language_tag.charset
186+
else
187+
@charset = Locale.charset
188+
end
183189

184190
@output_file ||= "#{@locale}.po"
185191
if File.exist?(@output_file)
@@ -280,6 +286,7 @@ def replace_entry
280286
replace_pot_revision_date
281287
replace_language
282288
replace_plural_forms
289+
replace_charset
283290
end
284291

285292
def replace_comment
@@ -325,6 +332,14 @@ def replace_plural_forms
325332
end
326333
end
327334

335+
CONTENT_TYPE_CHARSET = /(Content-Type: .+ charset=)CHARSET/
336+
337+
def replace_charset
338+
if CONTENT_TYPE_CHARSET =~ @entry
339+
@entry = @entry.gsub(CONTENT_TYPE_CHARSET, "\\1#{@charset}")
340+
end
341+
end
342+
328343
def plural_forms(language)
329344
converter = CLDRPluralsConverter.new(language)
330345
converter.convert

test/po/ja/_.po

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ msgstr ""
6969
msgid "#"
7070
msgstr ""
7171

72-
msgid "\taaa'bbb\\ccc"
73-
msgstr ""
72+
#, fuzzy
73+
msgid "\\taaa'bbb\\ccc"
74+
msgstr "AAA"
7475

7576
msgid ""
7677
"Here document1\n"

test/tools/test_msginit.rb

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def pot_header(options)
7777
options = default_po_header_options.merge(options)
7878
package_name = options[:package_name] || default_package_name
7979
have_plural_forms = options[:have_plural_forms] || true
80+
charset = options[:charset] || "UTF-8"
8081
header = <<EOF
8182
# SOME DESCRIPTIVE TITLE.
8283
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
@@ -93,7 +94,7 @@ def pot_header(options)
9394
"Language: \\n"
9495
"Language-Team: LANGUAGE <LL@li.org>\\n"
9596
"MIME-Version: 1.0\\n"
96-
"Content-Type: text/plain; charset=UTF-8\\n"
97+
"Content-Type: text/plain; charset=#{charset}\\n"
9798
"Content-Transfer-Encoding: 8bit\\n"
9899
EOF
99100
if have_plural_forms
@@ -109,6 +110,7 @@ def po_header(locale, language, options={})
109110
name = options[:translator_name] || "FULL NAME"
110111
email = options[:translator_email] || "EMAIL@ADDRESS"
111112
language_name = Locale::Info.get_language(language).name
113+
charset = options[:charset] || "UTF-8"
112114
plural_forms = @msginit.send(:plural_forms, language)
113115

114116
<<EOF
@@ -126,7 +128,7 @@ def po_header(locale, language, options={})
126128
"Language: #{locale}\\n"
127129
"Language-Team: #{language_name}\\n"
128130
"MIME-Version: 1.0\\n"
129-
"Content-Type: text/plain; charset=UTF-8\\n"
131+
"Content-Type: text/plain; charset=#{charset}\\n"
130132
"Content-Transfer-Encoding: 8bit\\n"
131133
"Plural-Forms: #{plural_forms}\\n"
132134
EOF
@@ -196,8 +198,8 @@ def test_specify_option
196198
end
197199

198200
class TestLocale < self
199-
def run_msginit(locale)
200-
create_pot_file("test.pot")
201+
def run_msginit(locale, charset = "UTF-8")
202+
create_pot_file("test.pot", :charset => charset)
201203
po_file_path = "output.po"
202204
@msginit.run("--output", po_file_path,
203205
"--locale", locale)
@@ -224,6 +226,50 @@ def test_language_region_charset
224226
assert_equal(po_header(locale, language),
225227
run_msginit("#{locale}.#{charset}"))
226228
end
229+
230+
def test_language_charset_with_replace_content_type
231+
locale = "en"
232+
assert_equal(po_header(locale, locale),
233+
run_msginit(locale, "CHARSET"))
234+
end
235+
236+
def test_language_region_with_replace_content_type
237+
locale = "en_US"
238+
language = "en"
239+
assert_equal(po_header(locale, language),
240+
run_msginit(locale, "CHARSET"))
241+
end
242+
243+
def test_language_region_charset_with_replace_content_type
244+
locale = "en_US"
245+
language = "en"
246+
charset = "UTF-8"
247+
assert_equal(po_header(locale, language),
248+
run_msginit("#{locale}.#{charset}", "CHARSET"))
249+
end
250+
end
251+
252+
class TestCurrentCharset < self
253+
def run_msginit(charset)
254+
create_pot_file("test.pot", :charset => charset)
255+
po_file_path = "output.po"
256+
@msginit.run("--output", po_file_path)
257+
File.read(po_file_path)
258+
end
259+
260+
def po_header(options)
261+
super(current_locale, current_language, options)
262+
end
263+
264+
def test_change
265+
assert_equal(po_header(:charset => "UTF-8"),
266+
run_msginit("CHARSET"))
267+
end
268+
269+
def test_not_change
270+
assert_equal(po_header(:charset => "ASCII"),
271+
run_msginit("ASCII"))
272+
end
227273
end
228274

229275
class TestTranslator < self

0 commit comments

Comments
 (0)