Adapted Gettext formatter to the modular style.
This commit is contained in:
parent
a35107ea99
commit
a7ac82aab7
2 changed files with 31 additions and 39 deletions
|
@ -60,50 +60,43 @@ module Twine
|
|||
end
|
||||
end
|
||||
|
||||
def write_file(path, lang)
|
||||
default_lang = @strings.language_codes[0]
|
||||
encoding = @options[:output_encoding] || 'UTF-8'
|
||||
File.open(path, "w:#{encoding}") do |f|
|
||||
f.puts "msgid \"\"\nmsgstr \"\"\n\"Language: #{lang}\\n\"\n\"X-Generator: Twine #{Twine::VERSION}\\n\"\n\n"
|
||||
@strings.sections.each do |section|
|
||||
printed_section = false
|
||||
section.rows.each do |row|
|
||||
if row.matches_tags?(@options[:tags], @options[:untagged])
|
||||
if !printed_section
|
||||
f.puts ''
|
||||
if section.name && section.name.length > 0
|
||||
section_name = section.name.gsub('--', '—')
|
||||
f.puts "# SECTION: #{section_name}"
|
||||
end
|
||||
printed_section = true
|
||||
end
|
||||
def format_file(strings, lang)
|
||||
@default_lang = strings.language_codes[0]
|
||||
super
|
||||
end
|
||||
|
||||
basetrans = row.translated_string_for_lang(default_lang)
|
||||
def format_header(lang)
|
||||
"msgid \"\"\nmsgstr \"\"\n\"Language: #{lang}\\n\"\n\"X-Generator: Twine #{Twine::VERSION}\\n\"\n"
|
||||
end
|
||||
|
||||
if basetrans
|
||||
key = row.key
|
||||
key = key.gsub('"', '\\\\"')
|
||||
def format_section_header(section)
|
||||
"# SECTION: #{section.name}"
|
||||
end
|
||||
|
||||
comment = row.comment
|
||||
if comment
|
||||
comment = comment.gsub('"', '\\\\"')
|
||||
end
|
||||
def format_row(row, lang)
|
||||
return nil unless row.translated_string_for_lang(@default_lang)
|
||||
|
||||
if comment && comment.length > 0
|
||||
f.print "#. \"#{comment}\"\n"
|
||||
end
|
||||
value = row.translated_string_for_lang(lang)
|
||||
|
||||
f.print "msgctxt \"#{key}\"\nmsgid \"#{basetrans}\"\n"
|
||||
value = row.translated_string_for_lang(lang)
|
||||
if value
|
||||
value = value.gsub('"', '\\\\"')
|
||||
end
|
||||
f.print "msgstr \"#{value}\"\n\n"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return nil unless value
|
||||
|
||||
result = ""
|
||||
if row.comment
|
||||
comment = format_comment(row.comment)
|
||||
result += comment + "\n" if comment
|
||||
end
|
||||
|
||||
result += "msgctxt \"#{format_key(row.key.dup)}\"\n"
|
||||
result += "msgid \"#{format_value(row.translations[@default_lang])}\"\n"
|
||||
result += "msgstr \"#{format_value(value)}\"\n"
|
||||
end
|
||||
|
||||
def format_comment(comment)
|
||||
"#. \"#{escape_quotes(comment)}\""
|
||||
end
|
||||
|
||||
def escape_quotes(text)
|
||||
text.gsub('"', '\\\\"')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
1
test/fixtures/formatter_gettext.po
vendored
1
test/fixtures/formatter_gettext.po
vendored
|
@ -24,4 +24,3 @@ msgstr "value3-english"
|
|||
msgctxt "key4"
|
||||
msgid "value4-english"
|
||||
msgstr "value4-english"
|
||||
|
||||
|
|
Reference in a new issue