Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
62 changes: 58 additions & 4 deletions test/tools/test_msginit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def current_language
Locale.current.language
end

def current_charset
Locale.current.charset
end

def translator_name
"me"
end
Expand All @@ -57,6 +61,10 @@ def translator_email
"me@example.com"
end

def template_charset
"CHARSET"
end

def create_pot_file(path, options=nil)
options ||= {}
File.open(path, "w") do |pot_file|
Expand All @@ -77,6 +85,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 +102,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 +118,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 +136,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 +206,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 +234,50 @@ def test_language_region_charset
assert_equal(po_header(locale, language),
run_msginit("#{locale}.#{charset}"))
end

def test_language_charset_with_template_charset
locale = "en"
assert_equal(po_header(locale, locale),
run_msginit(locale, template_charset))
end

def test_language_region_with_template_charset
locale = "en_US"
language = "en"
assert_equal(po_header(locale, language),
run_msginit(locale, template_charset))
end

def test_language_region_charset_with_template_charset
locale = "en_US"
language = "en"
charset = "UTF-8"
assert_equal(po_header(locale, language),
run_msginit("#{locale}.#{charset}", template_charset))
end
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_template_charset
assert_equal(po_header(charset: current_charset),
run_msginit(template_charset))
end

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

class TestTranslator < self
Expand Down
Loading