Completely migrated android formatter to modularized approach.

This commit is contained in:
Sebastian Ludwig 2015-08-24 09:05:33 +02:00
parent b06b4f6f6b
commit 3ebd9f35fd
3 changed files with 32 additions and 43 deletions

View file

@ -149,6 +149,9 @@ module Twine
def format_row(row, lang, default_lang)
value = row.translated_string_for_lang(lang, default_lang)
if value.nil? && @options[:include_untranslated]
value = row.translated_string_for_lang(@strings.language_codes[0])
end
return nil unless value
result = ""
@ -157,7 +160,7 @@ module Twine
result += comment + "\n" if comment
end
result += key_value_pattern % { key: format_key(row.key), value: format_value(value) }
result += key_value_pattern % { key: format_key(row.key.dup), value: format_value(value.dup) }
end
def format_comment(comment)
@ -168,11 +171,11 @@ module Twine
end
def format_key(key)
raise NotImplementedError.new("You must implement format_key in your formatter class.")
key
end
def format_value(value)
raise NotImplementedError.new("You must implement format_value in your formatter class.")
value
end
def write_file(path, lang)

View file

@ -95,45 +95,6 @@ module Twine
"<?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
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
result += "\t<!-- #{comment} -->\n"
end
result += "\t<string name=\"#{key}\">#{value}</string>"
end
end
def format_sections(lang, default_lang)
result = '<resources>'
@ -142,6 +103,31 @@ module Twine
result += '</resources>'
end
def format_section_header(section)
"\t<!-- SECTION: #{section.name} -->"
end
def format_comment(comment)
"\t<!-- #{comment.gsub('--', '—')} -->"
end
def key_value_pattern
"\t<string name=\"%{key}\">%{value}</string>"
end
def format_value(value)
# 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 }
end
end
end
end

View file

@ -87,7 +87,7 @@ module Twine
end
def format_section_header(section)
"/********** #{section.name} **********/\n" if section.name && section.name.length > 0
"/********** #{section.name} **********/\n"
end
def key_value_pattern