From 3ebd9f35fd425a49e46e4c09249139cb820e1d28 Mon Sep 17 00:00:00 2001 From: Sebastian Ludwig Date: Mon, 24 Aug 2015 09:05:33 +0200 Subject: [PATCH] Completely migrated android formatter to modularized approach. --- lib/twine/formatters/abstract.rb | 9 +++-- lib/twine/formatters/android.rb | 64 +++++++++++++------------------- lib/twine/formatters/apple.rb | 2 +- 3 files changed, 32 insertions(+), 43 deletions(-) diff --git a/lib/twine/formatters/abstract.rb b/lib/twine/formatters/abstract.rb index 17a2115..818495f 100644 --- a/lib/twine/formatters/abstract.rb +++ b/lib/twine/formatters/abstract.rb @@ -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) diff --git a/lib/twine/formatters/android.rb b/lib/twine/formatters/android.rb index d300a28..c3cf504 100644 --- a/lib/twine/formatters/android.rb +++ b/lib/twine/formatters/android.rb @@ -95,45 +95,6 @@ module Twine "\n\n\n" end - def format_section_header(section) - "\t" - 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\n" - end - result += "\t#{value}" - end - end - def format_sections(lang, default_lang) result = '' @@ -142,6 +103,31 @@ module Twine result += '' end + def format_section_header(section) + "\t" + end + + def format_comment(comment) + "\t" + end + + def key_value_pattern + "\t%{value}" + 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 diff --git a/lib/twine/formatters/apple.rb b/lib/twine/formatters/apple.rb index 9ff0b86..2bc63d0 100644 --- a/lib/twine/formatters/apple.rb +++ b/lib/twine/formatters/apple.rb @@ -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