Skip to content

Commit 8b183c3

Browse files
authored
msginit: Replace charset in Content-type with charset in locale (#113)
`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 8b183c3

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

lib/gettext/tools/msginit.rb

Lines changed: 13 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,12 @@ def replace_plural_forms
325332
end
326333
end
327334

335+
CONTENT_TYPE_CHARSET = /^(Content-Type:.+ charset=)CHARSET/
336+
337+
def replace_charset
338+
@entry = @entry.gsub(CONTENT_TYPE_CHARSET, "\\1#{@charset}")
339+
end
340+
328341
def plural_forms(language)
329342
converter = CLDRPluralsConverter.new(language)
330343
converter.convert

test/tools/test_msginit.rb

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def current_language
4949
Locale.current.language
5050
end
5151

52+
def current_charset
53+
Locale.current.charset
54+
end
55+
5256
def translator_name
5357
"me"
5458
end
@@ -57,6 +61,10 @@ def translator_email
5761
"me@example.com"
5862
end
5963

64+
def template_charset
65+
"CHARSET"
66+
end
67+
6068
def create_pot_file(path, options=nil)
6169
options ||= {}
6270
File.open(path, "w") do |pot_file|
@@ -77,6 +85,7 @@ def pot_header(options)
7785
options = default_po_header_options.merge(options)
7886
package_name = options[:package_name] || default_package_name
7987
have_plural_forms = options[:have_plural_forms] || true
88+
charset = options[:charset] || "UTF-8"
8089
header = <<EOF
8190
# SOME DESCRIPTIVE TITLE.
8291
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
@@ -93,7 +102,7 @@ def pot_header(options)
93102
"Language: \\n"
94103
"Language-Team: LANGUAGE <LL@li.org>\\n"
95104
"MIME-Version: 1.0\\n"
96-
"Content-Type: text/plain; charset=UTF-8\\n"
105+
"Content-Type: text/plain; charset=#{charset}\\n"
97106
"Content-Transfer-Encoding: 8bit\\n"
98107
EOF
99108
if have_plural_forms
@@ -109,6 +118,7 @@ def po_header(locale, language, options={})
109118
name = options[:translator_name] || "FULL NAME"
110119
email = options[:translator_email] || "EMAIL@ADDRESS"
111120
language_name = Locale::Info.get_language(language).name
121+
charset = options[:charset] || "UTF-8"
112122
plural_forms = @msginit.send(:plural_forms, language)
113123

114124
<<EOF
@@ -126,7 +136,7 @@ def po_header(locale, language, options={})
126136
"Language: #{locale}\\n"
127137
"Language-Team: #{language_name}\\n"
128138
"MIME-Version: 1.0\\n"
129-
"Content-Type: text/plain; charset=UTF-8\\n"
139+
"Content-Type: text/plain; charset=#{charset}\\n"
130140
"Content-Transfer-Encoding: 8bit\\n"
131141
"Plural-Forms: #{plural_forms}\\n"
132142
EOF
@@ -196,8 +206,8 @@ def test_specify_option
196206
end
197207

198208
class TestLocale < self
199-
def run_msginit(locale)
200-
create_pot_file("test.pot")
209+
def run_msginit(locale, pot_charset=nil)
210+
create_pot_file("test.pot", charset: pot_charset)
201211
po_file_path = "output.po"
202212
@msginit.run("--output", po_file_path,
203213
"--locale", locale)
@@ -224,6 +234,50 @@ def test_language_region_charset
224234
assert_equal(po_header(locale, language),
225235
run_msginit("#{locale}.#{charset}"))
226236
end
237+
238+
def test_language_charset_with_template_charset
239+
locale = "en"
240+
assert_equal(po_header(locale, locale),
241+
run_msginit(locale, template_charset))
242+
end
243+
244+
def test_language_region_with_template_charset
245+
locale = "en_US"
246+
language = "en"
247+
assert_equal(po_header(locale, language),
248+
run_msginit(locale, template_charset))
249+
end
250+
251+
def test_language_region_charset_with_template_charset
252+
locale = "en_US"
253+
language = "en"
254+
charset = "UTF-8"
255+
assert_equal(po_header(locale, language),
256+
run_msginit("#{locale}.#{charset}", template_charset))
257+
end
258+
end
259+
260+
class TestCurrentCharset < self
261+
def run_msginit(pot_charset)
262+
create_pot_file("test.pot", charset: pot_charset)
263+
po_file_path = "output.po"
264+
@msginit.run("--output", po_file_path)
265+
File.read(po_file_path)
266+
end
267+
268+
def po_header(options)
269+
super(current_locale, current_language, options)
270+
end
271+
272+
def test_template_charset
273+
assert_equal(po_header(charset: current_charset),
274+
run_msginit(template_charset))
275+
end
276+
277+
def test_no_template_charset
278+
assert_equal(po_header(charset: "ASCII"),
279+
run_msginit("ASCII"))
280+
end
227281
end
228282

229283
class TestTranslator < self

0 commit comments

Comments
 (0)