Half way there of moving android formatter the the modularized version

This commit is contained in:
Sebastian Ludwig 2015-08-24 09:05:26 +02:00
parent eab588735f
commit b06b4f6f6b
3 changed files with 53 additions and 58 deletions

View file

@ -115,15 +115,18 @@ module Twine
def format_file(lang, default_lang)
result = format_header(lang) + "\n"
sections = @strings.sections.map { |section| format_section(section, lang, default_lang) }
result += sections.join("\n")
result += format_sections(lang, default_lang)
end
def format_header(lang)
raise NotImplementedError.new("You must implement format_header in your formatter class.")
end
def format_sections(lang, default_lang)
sections = @strings.sections.map { |section| format_section(section, lang, default_lang) }
sections.join("\n")
end
def format_section_header(section)
end
@ -134,13 +137,13 @@ module Twine
unless rows.empty?
if section.name && section.name.length > 0
section_header = format_section_header(section)
result += "\n#{section_header}\n" if section_header
result += "\n#{section_header}" if section_header
end
end
rows.map! { |row| format_row(row, lang, default_lang) }
rows.compact! # remove nil entries
rows.map! { |row| "\n#{row}\n" } # wrap with newlines
rows.map! { |row| "\n#{row}" } # prepend newline
result += rows.join
end

View file

@ -91,65 +91,57 @@ module Twine
end
end
def write_file(path, lang)
default_lang = nil
if DEFAULT_LANG_CODES.has_key?(lang)
default_lang = DEFAULT_LANG_CODES[lang]
def format_header(lang)
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!-- Android Strings File -->\n<!-- Generated by Twine #{Twine::VERSION} -->\n<!-- Language: #{lang} -->"
end
def format_section_header(section)
"\t<!-- SECTION: #{section.name} -->"
end
def format_row(row, lang, default_lang)
result = ""
key = row.key
value = row.translated_string_for_lang(lang, default_lang)
if !value && @options[:include_untranslated]
value = row.translated_string_for_lang(@strings.language_codes[0])
end
File.open(path, 'w:UTF-8') do |f|
f.puts "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!-- Android Strings File -->\n<!-- Generated by Twine #{Twine::VERSION} -->\n<!-- Language: #{lang} -->"
f.write '<resources>'
@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 "\t<!-- SECTION: #{section_name} -->"
end
printed_section = true
end
key = row.key
if value # if values is nil, there was no appropriate translation, so let Android handle the defaulting
value = String.new(value) # use a copy to prevent modifying the original
value = row.translated_string_for_lang(lang, default_lang)
if !value && @options[:include_untranslated]
value = row.translated_string_for_lang(@strings.language_codes[0])
end
# Android enforces the following rules on the values
# 1) apostrophes and quotes must be escaped with a backslash
value.gsub!('\'', '\\\\\'')
value.gsub!('"', '\\\\"')
# 2) HTML escape the string
value = CGI.escapeHTML(value)
# 3) fix substitutions (e.g. %s/%@)
value = androidify_substitutions(value)
# 4) replace beginning and end spaces with \0020. Otherwise Android strips them.
value.gsub!(/\A *| *\z/) { |spaces| '\u0020' * spaces.length }
if value # if values is nil, there was no appropriate translation, so let Android handle the defaulting
value = String.new(value) # use a copy to prevent modifying the original
# Android enforces the following rules on the values
# 1) apostrophes and quotes must be escaped with a backslash
value.gsub!('\'', '\\\\\'')
value.gsub!('"', '\\\\"')
# 2) HTML escape the string
value = CGI.escapeHTML(value)
# 3) fix substitutions (e.g. %s/%@)
value = androidify_substitutions(value)
# 4) replace beginning and end spaces with \0020. Otherwise Android strips them.
value.gsub!(/\A *| *\z/) { |spaces| '\u0020' * spaces.length }
comment = row.comment
if comment
comment = comment.gsub('--', '—')
end
if comment && comment.length > 0
f.puts "\t<!-- #{comment} -->\n"
end
f.puts "\t<string name=\"#{key}\">#{value}</string>"
end
end
end
comment = row.comment
if comment
comment = comment.gsub('--', '—')
end
f.puts '</resources>'
if comment && comment.length > 0
result += "\t<!-- #{comment} -->\n"
end
result += "\t<string name=\"#{key}\">#{value}</string>"
end
end
def format_sections(lang, default_lang)
result = '<resources>'
result += super(lang, default_lang) + "\n"
result += '</resources>'
end
end
end
end

View file

@ -87,11 +87,11 @@ module Twine
end
def format_section_header(section)
"/********** #{section.name} **********/" if section.name && section.name.length > 0
"/********** #{section.name} **********/\n" if section.name && section.name.length > 0
end
def key_value_pattern
"\"%{key}\" = \"%{value}\";"
"\"%{key}\" = \"%{value}\";\n"
end
def format_comment(comment)