Skip to content

Commit

Permalink
fix: propagate isPlural correctly for PoMessage + typo in IcuUnescape…
Browse files Browse the repository at this point in the history
…r name
  • Loading branch information
Anty0 committed Dec 2, 2024
1 parent 6baf47d commit c6436d0
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.tolgee.formats.escaping
/**
* It escapes controlling characters in ICU message, so it's not interpreted when in comes from other formats
*/
class IcuUnescper(
class IcuUnescaper(
private val input: String,
private val isPlural: Boolean = false,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.tolgee.formats

import com.ibm.icu.text.PluralRules
import io.tolgee.formats.escaping.ForceIcuEscaper
import io.tolgee.formats.escaping.IcuUnescper
import io.tolgee.formats.escaping.IcuUnescaper
import io.tolgee.formats.escaping.PluralFormIcuEscaper
import io.tolgee.util.nullIfEmpty

Expand Down Expand Up @@ -257,7 +257,7 @@ fun String.forceEscapePluralForms(): MessageConvertorResult? {
*/
fun String.unescapePluralForms(): String? {
val forms = getPluralForms(this)
val unescaped = forms?.forms?.mapValues { IcuUnescper(it.value, isPlural = true).unescaped }
val unescaped = forms?.forms?.mapValues { IcuUnescaper(it.value, isPlural = true).unescaped }
return unescaped?.toIcuPluralString(optimize = false, argName = forms.argName)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.ibm.icu.text.PluralRules.FixedDecimal
import com.ibm.icu.util.ULocale
import io.tolgee.formats.FromIcuPlaceholderConvertor
import io.tolgee.formats.MessageConvertorFactory
import io.tolgee.formats.escaping.IcuUnescper
import io.tolgee.formats.escaping.IcuUnescaper
import io.tolgee.formats.getPluralDataOrNull
import io.tolgee.formats.getULocaleFromTag
import io.tolgee.formats.pluralData.PluralData
Expand Down Expand Up @@ -69,7 +69,7 @@ class IcuToPoMessageConvertor(
val plurals =
languagePluralData.examples.map {
val form = forms[it.plural] ?: OTHER_KEYWORD
it.plural to ((formsResult[form] ?: formsResult[OTHER_KEYWORD])?.forceUnescape() ?: "")
it.plural to ((formsResult[form] ?: formsResult[OTHER_KEYWORD])?.forceUnescapePluralForm() ?: "")
}.sortedBy { it.first }.map { it.second }.toList()

return plurals
Expand All @@ -93,9 +93,9 @@ class IcuToPoMessageConvertor(
}.toMap()
}

private fun String.forceUnescape(): String {
private fun String.forceUnescapePluralForm(): String {
if (!projectIcuPlaceholdersSupport) {
return IcuUnescper(this).unescaped
return IcuUnescaper(this, isPlural = true).unescaped
}
return this
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package io.tolgee.unit.formats.escaping

import io.tolgee.formats.escaping.IcuUnescper
import io.tolgee.formats.escaping.IcuUnescaper
import io.tolgee.testing.assert
import org.junit.jupiter.api.Test

class IcuUnescaperTest {
@Test
fun `it escapes`() {
IcuUnescper("'{hello}', my friend!").unescaped.assert.isEqualTo("{hello}, my friend!")
IcuUnescaper("'{hello}', my friend!").unescaped.assert.isEqualTo("{hello}, my friend!")
}

@Test
fun `it escapes apostrophes`() {
IcuUnescper(
IcuUnescaper(
"we are not entering escaped section: '''' " +
"so it doesn't ' have to be doubled. " +
"This sequence: '{' should be immediately closed",
Expand All @@ -25,7 +25,7 @@ class IcuUnescaperTest {

@Test
fun `it works for weird case`() {
IcuUnescper(
IcuUnescaper(
"'What ' complex '''' '{' string # ",
false,
).unescaped.assert.isEqualTo("'What ' complex '' { string # ")
Expand All @@ -34,29 +34,29 @@ class IcuUnescaperTest {
@Test
fun `removes the escape char on end of string`() {
val escaped = "Another ''''' more complex ' '''{ string }''' with many weird '} cases ''''}'"
IcuUnescper(escaped, false)
IcuUnescaper(escaped, false)
.unescaped.assert.isEqualTo("Another ''' more complex ' '{ string }' with many weird } cases ''}")
}

@Test
fun `it it escapes escaped`() {
IcuUnescper(
IcuUnescaper(
"'''{'",
false,
).unescaped.assert.isEqualTo("'{")
}

@Test
fun `it escapes plurals`() {
IcuUnescper(
IcuUnescaper(
"What a '#' plural form",
isPlural = true,
).unescaped.assert.isEqualTo("What a # plural form")
}

@Test
fun `it unescapes inner sequence correctly`() {
IcuUnescper(
IcuUnescaper(
"'{ '' 'lakjsa'.",
isPlural = true,
).unescaped.assert.isEqualTo("{ ' lakjsa'.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.tolgee.unit.formats.escaping

import io.tolgee.formats.escaping.ForceIcuEscaper
import io.tolgee.formats.escaping.IcuUnescper
import io.tolgee.formats.escaping.IcuUnescaper
import io.tolgee.testing.assert
import org.junit.jupiter.api.Test

Expand Down Expand Up @@ -36,7 +36,7 @@ class TwoWayEscapingTest {
plural: Boolean,
) {
val escaped = ForceIcuEscaper(string, plural).escaped
val unescaped = IcuUnescper(escaped, plural).unescaped
val unescaped = IcuUnescaper(escaped, plural).unescaped
unescaped.assert.describedAs(
"\n\nInput:\n$string \n\nEscaped:\n$escaped \n\nUnescpaed: \n$unescaped",
).isEqualTo(string)
Expand Down

0 comments on commit c6436d0

Please sign in to comment.