Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid escaping hash characters outside of plurals #2731

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fun convertMessage(
return message.toConvertorResult()
}
if (!convertPlaceholders || convertorFactory == null) {
return message.escapeIcu(true).toConvertorResult()
return message.escapeIcu(isInPlural).toConvertorResult()
}

val convertor = convertorFactory()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,50 @@ class PoToICUConverterTest {
)
}

@Test
fun `php plurals with hashtags works`() {
val result =
getPhpConvertor().convert(
rawData =
mapOf(
0 to "Petr má jeden znak #.",
1 to "Petr má %d znaky #.",
2 to "Petr má %d znaků #.",
),
languageTag = "cs",
convertPlaceholders = true,
).message
assertThat(result).isEqualTo(
"{0, plural,\n" +
"one {Petr má jeden znak '#'.}\n" +
"few {Petr má # znaky '#'.}\n" +
"other {Petr má # znaků '#'.}\n" +
"}",
)
}

@Test
fun `php plurals with hashtags and disabled placeholders works`() {
val result =
getPhpConvertor().convert(
rawData =
mapOf(
0 to "Petr má jeden znak #.",
1 to "Petr má %d znaky #.",
2 to "Petr má %d znaků #.",
),
languageTag = "cs",
convertPlaceholders = false,
).message
assertThat(result).isEqualTo(
"{value, plural,\n" +
"one {Petr má jeden znak '#'.}\n" +
"few {Petr má %d znaky '#'.}\n" +
"other {Petr má %d znaků '#'.}\n" +
"}",
)
}

private fun getPhpConvertor() = PoToIcuMessageConvertor { PhpToIcuPlaceholderConvertor() }

@Test
Expand All @@ -48,6 +92,24 @@ class PoToICUConverterTest {
assertThat(result).isEqualTo("hello this is string {0}, this is digit {1, number}")
}

@Test
fun `php message with hashtag works`() {
val result =
getPhpConvertor().convert("hello this is hashtag # and it should not be escaped", "en").message
assertThat(result).isEqualTo("hello this is hashtag # and it should not be escaped")
}

@Test
fun `php message with hashtag and disabled placeholders works`() {
val result =
getPhpConvertor().convert(
"hello this is hashtag # and it should not be escaped",
"en",
convertPlaceholders = false,
).message
assertThat(result).isEqualTo("hello this is hashtag # and it should not be escaped")
}

@Test
fun testPhpMessageEscapes() {
val result =
Expand Down
Loading