@@ -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"
98107EOF
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"
132142EOF
@@ -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