diff options
-rw-r--r-- | lib/twine/formatters/android.rb | 42 | ||||
-rw-r--r-- | test/test_formatters.rb | 16 |
2 files changed, 31 insertions, 27 deletions
diff --git a/lib/twine/formatters/android.rb b/lib/twine/formatters/android.rb index 439affc..acc5da8 100644 --- a/lib/twine/formatters/android.rb +++ b/lib/twine/formatters/android.rb @@ -68,35 +68,23 @@ module Twine end def read(io, lang) - resources_regex = /<resources(?:[^>]*)>(.*)<\/resources>/m - key_regex = /<string name="(\w+)">/ - comment_regex = /<!-- (.*) -->/ - value_regex = /<string name="\w+">(.*)<\/string>/ - key = nil - value = nil + document = REXML::Document.new io, :compress_whitespace => %w{ string } + comment = nil + document.root.children.each do |child| + if child.is_a? REXML::Comment + content = child.string.strip + comment = content if content.length > 0 and not content.start_with?("SECTION:") + elsif child.is_a? REXML::Element + next unless child.name == 'string' - content_match = resources_regex.match(io.read) - if content_match - for line in content_match[1].split(/\r?\n/) - key_match = key_regex.match(line) - if key_match - key = key_match[1] - value_match = value_regex.match(line) - value = value_match ? value_match[1] : "" - - set_translation_for_key(key, lang, value) - if comment and comment.length > 0 and !comment.start_with?("SECTION:") - set_comment_for_key(key, comment) - end - comment = nil - end - - comment_match = comment_regex.match(line) - if comment_match - comment = comment_match[1] - end - end + key = child.attributes['name'] + + set_translation_for_key(key, lang, child.text) + set_comment_for_key(key, comment) if comment + + comment = nil + end end end diff --git a/test/test_formatters.rb b/test/test_formatters.rb index 851d514..a8611a9 100644 --- a/test/test_formatters.rb +++ b/test/test_formatters.rb @@ -47,6 +47,22 @@ class TestAndroidFormatter < FormatterTest assert_file_contents_read_correctly end + def test_read_multiline_translation + content = <<-EOCONTENT + <?xml version="1.0" encoding="utf-8"?> + <resources> + <string name="foo">This is + a string</string> + </resources> + EOCONTENT + + io = StringIO.new(content) + + @formatter.read io, 'en' + + assert_equal 'This is\n a string', @empty_twine_file.definitions_by_key["foo"].translations['en'] + end + def test_set_translation_converts_leading_spaces @formatter.set_translation_for_key 'key1', 'en', "\u0020value" assert_equal ' value', @empty_twine_file.definitions_by_key['key1'].translations['en'] |