Skip to content

Commit

Permalink
Fix parsing more than 9 placeholders with order (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangl authored Jul 13, 2023
1 parent bc74550 commit ed31fba
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security
- No security issues fixed!

## [3.4.1] - 2023-07-13
### Fixed
- Fix parsing of texts with more than 9 placeholders.

## [3.4.0] - 2023-05-08
### Added
- Add new `unescapeHtmlTags` flag to enable or disable HTML unescaping from strings.
Expand Down Expand Up @@ -470,7 +474,8 @@ res_dir_path -> resDirPath
### Added
- Initial release.

[Unreleased]: https://github.com/hyperdevs-team/poeditor-android-gradle-plugin/compare/3.4.0...HEAD
[Unreleased]: https://github.com/hyperdevs-team/poeditor-android-gradle-plugin/compare/3.4.1...HEAD
[3.4.1]: https://github.com/hyperdevs-team/poeditor-android-gradle-plugin/compare/3.4.0...3.4.1
[3.4.0]: https://github.com/hyperdevs-team/poeditor-android-gradle-plugin/compare/3.3.0...3.4.0
[3.3.1]: https://github.com/hyperdevs-team/poeditor-android-gradle-plugin/compare/3.3.0...3.3.1
[3.3.0]: https://github.com/hyperdevs-team/poeditor-android-gradle-plugin/compare/3.2.0...3.3.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package com.hyperdevs.poeditor.gradle.xml

import com.hyperdevs.poeditor.gradle.ktx.toAndroidXmlString
import com.hyperdevs.poeditor.gradle.ktx.toStringsXmlDocument
import com.hyperdevs.poeditor.gradle.ktx.unescapeHtmlTags
import com.hyperdevs.poeditor.gradle.utils.ALL_REGEX_STRING
import org.w3c.dom.*

Expand All @@ -31,7 +30,7 @@ import org.w3c.dom.*
class XmlPostProcessor {
companion object {
private val DEFAULT_ENCODING = Charsets.UTF_8
private val VARIABLE_REGEX = Regex("""\{\d?\{(.*?)\}\}""")
private val VARIABLE_REGEX = Regex("""\{(\d*)\{(.*?)\}\}""")

private const val TAG_RESOURCES = "resources"
private const val TAG_STRING = "string"
Expand Down Expand Up @@ -82,9 +81,10 @@ class XmlPostProcessor {
// throw an exception

// If the placeholder contains an ordinal, use it: {2{pages_count}} -> %2$s
val match = matchResult.groupValues[0]
if (Character.isDigit(match[1])) {
"%${match[1]}\$s"
val fullMatch = matchResult.groupValues[0]
val placeholderVaraibleOrder = matchResult.groupValues[1]
if (placeholderVaraibleOrder.toIntOrNull() != null) {
"%$placeholderVaraibleOrder\$s"
} else { // If not, use "1" as the ordinal: {{pages_count}} -> %1$s
"%1\$s"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ class XmlPostProcessorTest {
xmlPostProcessor.formatTranslationString("Hello {{name}}. How are you {{name}}?"))
Assert.assertEquals("Hello %1\$s. This is your score: %2\$s",
xmlPostProcessor.formatTranslationString("Hello {1{name}}. This is your score: {2{score}}"))

Assert.assertEquals("Hello %1\$s %2\$s %3\$s %4\$s %5\$s %6\$s %7\$s %8\$s %9\$s %10\$s %11\$s",
xmlPostProcessor.formatTranslationString(
"Hello {1{name}} {2{name}} {3{name}} {4{name}} {5{name}} " +
"{6{name}} {7{name}} {8{name}} {9{name}} {10{name}} {11{name}}"
)
)
}

@Test
Expand Down

0 comments on commit ed31fba

Please sign in to comment.