Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/gettext/tools/msginit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def initialize
@output_file = nil
@locale = nil
@language = nil
@charset = nil
@entry = nil
@comment = nil
@translator = nil
Expand Down Expand Up @@ -180,6 +181,11 @@ def validate
end
@locale = language_tag.to_simple.to_s
@language = language_tag.language
if language_tag.respond_to?(:charset)
@charset = language_tag.charset
else
@charset = Locale.charset
end

@output_file ||= "#{@locale}.po"
if File.exist?(@output_file)
Expand Down Expand Up @@ -280,6 +286,7 @@ def replace_entry
replace_pot_revision_date
replace_language
replace_plural_forms
replace_charset
end

def replace_comment
Expand Down Expand Up @@ -325,6 +332,12 @@ def replace_plural_forms
end
end

CONTENT_TYPE_CHARSET = /^(Content-Type:.+ charset=)CHARSET/

def replace_charset
@entry = @entry.gsub(CONTENT_TYPE_CHARSET, "\\1#{@charset}")
end

def plural_forms(language)
converter = CLDRPluralsConverter.new(language)
converter.convert
Expand Down
54 changes: 50 additions & 4 deletions test/tools/test_msginit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ 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
Expand All @@ -93,7 +94,7 @@ def pot_header(options)
"Language: \\n"
"Language-Team: LANGUAGE <LL@li.org>\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Type: text/plain; charset=#{charset}\\n"
"Content-Transfer-Encoding: 8bit\\n"
EOF
if have_plural_forms
Expand All @@ -109,6 +110,7 @@ def po_header(locale, language, options={})
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
Expand All @@ -126,7 +128,7 @@ def po_header(locale, language, options={})
"Language: #{locale}\\n"
"Language-Team: #{language_name}\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Type: text/plain; charset=#{charset}\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Plural-Forms: #{plural_forms}\\n"
EOF
Expand Down Expand Up @@ -196,8 +198,8 @@ def test_specify_option
end

class TestLocale < self
def run_msginit(locale)
create_pot_file("test.pot")
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)
Expand All @@ -224,6 +226,50 @@ def test_language_region_charset
assert_equal(po_header(locale, language),
run_msginit("#{locale}.#{charset}"))
end

def test_language_charset_with_replace_content_type
locale = "en"
assert_equal(po_header(locale, locale),
run_msginit(locale, "CHARSET"))
end

def test_language_region_with_replace_content_type
locale = "en_US"
language = "en"
assert_equal(po_header(locale, language),
run_msginit(locale, "CHARSET"))
end

def test_language_region_charset_with_replace_content_type
locale = "en_US"
language = "en"
charset = "UTF-8"
assert_equal(po_header(locale, language),
run_msginit("#{locale}.#{charset}", "CHARSET"))
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need them?
It seems that existing tests cover these cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test was to check that charset is replaced as expected, even when --locale is specified.
This is because the value of language_tag changes with or without its option.

if @locale.nil?
language_tag = Locale.current
else
language_tag = Locale::Tag.parse(@locale)
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

Let's clarify it by test name and variable name:

-test_XXX_with_replace_content_type
+test_XXX_with_template_charset
-run_msginit("XXX", "CHARSET")
+template_charset = "CHARSET"
+run_msginit("XXX", template_charset)

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_change
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_change
def test_template_charset

assert_equal(po_header(charset: "UTF-8"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert_equal(po_header(charset: "UTF-8"),
assert_equal(po_header(charset: Locale.current.charset),

We may want to define current_charset like other current_* methods.

run_msginit("CHARSET"))
end

def test_not_change
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_not_change
def test_not_template_charset

assert_equal(po_header(charset: "ASCII"),
run_msginit("ASCII"))
end
end

class TestTranslator < self
Expand Down