Skip to content

Commit 60c9fea

Browse files
author
Adrián García
authored
Fix percent symbols being escaped when variables are present (#20)
1 parent 891f3bd commit 60c9fea

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3030
### Removed
3131
- No removed features!
3232
### Fixed
33-
- No fixed issues!
33+
- Fix percent symbols being escaped when variables are present.
3434
### Security
3535
- No security issues fixed!
3636

src/main/kotlin/com/bq/poeditor/gradle/xml/XmlPostProcessor.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import javax.xml.parsers.DocumentBuilderFactory
3030
class 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 &lt; with < and &gt; with >
6771
.replace("&lt;", "<").replace("&gt;", ">")
6872
// Replace placeholders from {{variable}} to %1$s format.
69-
.replace(PLACEHOLDER_REGEX, placeholderTransform)
73+
.replace(VARIABLE_REGEX, placeholderTransform)
7074
}
7175

7276
/**

src/test/kotlin/com/bq/poeditor/gradle/xml/XmlPostProcessorTest.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,17 @@ class XmlPostProcessorTest {
4343
@Test
4444
fun `Postprocessing percentage symbol in strings works`() {
4545
// Test % is changed to %%
46-
Assert.assertEquals("Hello my friend. I love you 100%%.",
46+
Assert.assertEquals("Hello my friend. I love you 100%.",
4747
xmlPostProcessor.formatTranslationXml("Hello my friend. I love you 100%."))
4848
}
4949

50+
@Test
51+
fun `Postprocessing percentage symbol in text with variables works`() {
52+
// Test % is not changed if variables are present
53+
Assert.assertEquals("Hello %1\$s. I love you 100%%.",
54+
xmlPostProcessor.formatTranslationXml("Hello {{friend}}. I love you 100%."))
55+
}
56+
5057
@Test
5158
fun `Postprocessing line breaks keeps them`() {
5259
// Test \n is maintained

0 commit comments

Comments
 (0)