From 94f7380a23c01d59b16f0bb0b526ee79c08d1588 Mon Sep 17 00:00:00 2001 From: Uli Luckas Date: Wed, 28 Aug 2024 12:34:12 +0200 Subject: [PATCH] Fix Android Escaping of "'" and "&" Inspired by PR from Falko Richter: https://github.com/netbe/Babelish/pull/133 --- lib/babelish/csv2android.rb | 9 +++++++ test/babelish/test_csv2android.rb | 27 +++++++++++++++++++ ...ata_with_characters_that_need_escaping.csv | 3 +++ ...ata_with_characters_that_need_escaping.xml | 5 ++++ 4 files changed, 44 insertions(+) create mode 100644 test/data/test_data_with_characters_that_need_escaping.csv create mode 100644 test/data/test_data_with_characters_that_need_escaping.xml diff --git a/lib/babelish/csv2android.rb b/lib/babelish/csv2android.rb index 5d13f08..7f1c602 100644 --- a/lib/babelish/csv2android.rb +++ b/lib/babelish/csv2android.rb @@ -20,6 +20,8 @@ def language_filepaths(language) def process_value(row_value, default_value) value = super(row_value, default_value) + value = escape_android_characters(value) + # if the value begins and ends with a quote we must leave them unescapted if value.size > 4 && value[0, 2] == "\\\"" && value[value.size - 2, value.size] == "\\\"" value[0, 2] = "\"" @@ -28,6 +30,13 @@ def process_value(row_value, default_value) value.to_utf8 end + # see https://developer.android.com/guide/topics/resources/string-resource#escaping_quotes + def escape_android_characters(value) + value.gsub!(/'/, "'" => "\\'") # \' should be the result... + value.gsub!(/&/, "&" => "&") + value + end + def get_row_format(row_key, row_value, comment = nil, indentation = 0) entry = comment.to_s.empty? ? "" : "\n\t\n" entry + "\t#{row_value}\n" diff --git a/test/babelish/test_csv2android.rb b/test/babelish/test_csv2android.rb index f9f7d4a..b034706 100644 --- a/test/babelish/test_csv2android.rb +++ b/test/babelish/test_csv2android.rb @@ -69,4 +69,31 @@ def test_converting_with_basename # clean up system("rm -rf ./values-en") end + + def test_android_character_escaping + converter = Babelish::CSV2Android.new( + "test/data/test_data_with_characters_that_need_escaping.csv", + { "English" => "en" }, + output_basename: "escaped_strings" + ) + converter.convert + exist = File.exist?("values-en/escaped_strings.xml") + assert exist, "the ouptut file does not exist" + + output = File.read("values-en/escaped_strings.xml") + expected_output = File.read( + "test/data/test_data_with_characters_that_need_escaping.xml" + ) + + assert_equal expected_output, output + + assert_true FileUtils.identical?( + "values-en/escaped_strings.xml", + "test/data/test_data_with_characters_that_need_escaping.xml" + ) + + # clean up + system("rm -rf ./values-en") + + end end diff --git a/test/data/test_data_with_characters_that_need_escaping.csv b/test/data/test_data_with_characters_that_need_escaping.csv new file mode 100644 index 0000000..944c212 --- /dev/null +++ b/test/data/test_data_with_characters_that_need_escaping.csv @@ -0,0 +1,3 @@ +variables,English +NEEDS_ESCAPING,' & +ANOTHER_STRING,hello diff --git a/test/data/test_data_with_characters_that_need_escaping.xml b/test/data/test_data_with_characters_that_need_escaping.xml new file mode 100644 index 0000000..d241099 --- /dev/null +++ b/test/data/test_data_with_characters_that_need_escaping.xml @@ -0,0 +1,5 @@ + + + \' & + hello +