@@ -30,7 +30,7 @@ import javax.xml.parsers.DocumentBuilderFactory
3030class XmlPostProcessor {
3131 companion object {
3232 private val DEFAULT_ENCODING = Charsets .UTF_8
33- private val PLACEHOLDER_REGEX = Regex (""" \{\d?\{(.*?)\}\}""" )
33+ private val VARIABLE_REGEX = Regex (""" \{\d?\{(.*?)\}\}""" )
3434 }
3535
3636 /* *
@@ -48,7 +48,11 @@ class XmlPostProcessor {
4848 * Format variables and texts to conform to Android strings.xml format.
4949 */
5050 fun formatTranslationXml (translationXmlString : String ): String {
51- val placeholderTransform : (MatchResult ) -> CharSequence = { matchResult ->
51+ // We need to check for variables to see if we have to escape percent symbols: if we find variables, we have to
52+ // escape them
53+ val containsVariables = translationXmlString.contains(VARIABLE_REGEX )
54+
55+ val placeholderTransform: (MatchResult ) -> CharSequence = { matchResult ->
5256 // TODO: if the string has multiple variables but any of them has no order number,
5357 // throw an exception
5458 // If the placeholder contains an ordinal, use it: {2{pages_count}} -> %2$s
@@ -61,12 +65,12 @@ class XmlPostProcessor {
6165 }
6266
6367 return translationXmlString
64- // Replace % with %%
65- .replace(" %" , " %%" )
68+ // Replace % with %% if variables are found
69+ .let { if (containsVariables) it. replace(" %" , " %%" ) else it }
6670 // Replace < with < and > with >
6771 .replace(" <" , " <" ).replace(" >" , " >" )
6872 // Replace placeholders from {{variable}} to %1$s format.
69- .replace(PLACEHOLDER_REGEX , placeholderTransform)
73+ .replace(VARIABLE_REGEX , placeholderTransform)
7074 }
7175
7276 /* *
0 commit comments