Fix leading and trailing spaces in android xml files.

This commit is contained in:
Sebastian Celis 2014-03-26 14:04:36 -05:00
parent a0a8496664
commit 0b52d8a346
5 changed files with 45 additions and 0 deletions

View file

@ -71,6 +71,7 @@ module Twine
value.gsub!('\\\'', '\'')
value.gsub!('\\"', '"')
value = iosify_substitutions(value)
value.gsub!(/(\\u0020)*|(\\u0020)*\z/) { |spaces| ' ' * (spaces.length / 6) }
else
value = ""
end
@ -129,6 +130,8 @@ module Twine
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

8
test/fixtures/en-3.xml vendored Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Android Strings File -->
<!-- Generated by Twine 0.5.0 -->
<!-- Language: en -->
<resources>
<!-- SECTION: My Strings -->
<string name="string_with_spaces">\u0020string with spaces\u0020\u0020</string>
</resources>

9
test/fixtures/test-output-10.txt vendored Normal file
View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Android Strings File -->
<!-- Generated by Twine 0.5.0 -->
<!-- Language: en -->
<resources>
<!-- SECTION: My Strings -->
<!-- String ends with space -->
<string name="key with space ">string with space\u0020</string>
</resources>

9
test/fixtures/test-output-11.txt vendored Normal file
View file

@ -0,0 +1,9 @@
[[Uncategorized]]
[string_with_spaces]
en = ` string with spaces `
[[My Strings]]
[key with space ]
en = `string with space `
tags = tag1
comment = String ends with space

View file

@ -52,6 +52,14 @@ class TwineTest < Test::Unit::TestCase
end
end
def test_generate_string_file_7
Dir.mktmpdir do |dir|
output_path = File.join(dir, 'en.xml')
Twine::Runner.run(%W(generate-string-file test/fixtures/strings-2.txt #{output_path} -t tag1))
assert_equal(ERB.new(File.read('test/fixtures/test-output-10.txt')).result, File.read(output_path))
end
end
def test_consume_string_file_1
Dir.mktmpdir do |dir|
output_path = File.join(dir, 'strings.txt')
@ -92,6 +100,14 @@ class TwineTest < Test::Unit::TestCase
end
end
def test_consume_string_file_6
Dir.mktmpdir do |dir|
output_path = File.join(dir, 'strings.txt')
Twine::Runner.run(%W(consume-string-file test/fixtures/strings-2.txt test/fixtures/en-3.xml -o #{output_path} -l en -a))
assert_equal(File.read('test/fixtures/test-output-11.txt'), File.read(output_path))
end
end
def test_generate_report_1
Twine::Runner.run(%w(generate-report test/fixtures/strings-1.txt))
end