Skip to content

Commit 5ce2459

Browse files
authored
fix: Fix message format conversions (tolgee#2490)
1 parent 22d1923 commit 5ce2459

28 files changed

+161
-68
lines changed

backend/data/src/main/kotlin/io/tolgee/formats/BaseIcuMessageConvertor.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,7 @@ class BaseIcuMessageConvertor(
151151
form: String?,
152152
) {
153153
val formPlaceholderConvertor = getFormPlaceholderConvertor(form)
154-
if (keepEscaping) {
155-
val convertedPatternString = formPlaceholderConvertor.convertText(node.patternString)
156-
addToResult(convertedPatternString, form)
157-
return
158-
}
159-
val convertedText = formPlaceholderConvertor.convertText(node.text)
154+
val convertedText = formPlaceholderConvertor.convertText(node, keepEscaping)
160155
addToResult(convertedText, form)
161156
}
162157

backend/data/src/main/kotlin/io/tolgee/formats/ExportMessageFormat.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ enum class ExportMessageFormat(val paramConvertorFactory: () -> FromIcuPlacehold
1313
JAVA_STRING_FORMAT(paramConvertorFactory = { IcuToJavaPlaceholderConvertor() }),
1414
APPLE_SPRINTF(paramConvertorFactory = { IcuToApplePlaceholderConvertor() }),
1515
RUBY_SPRINTF(paramConvertorFactory = { IcuToRubyPlaceholderConvertor() }),
16-
ICU(paramConvertorFactory = { NoOpFromIcuPlaceholderConvertor() }),
16+
ICU(paramConvertorFactory = { IcuToIcuPlaceholderConvertor() }),
1717
// PYTHON_SPRINTF,
1818
}

backend/data/src/main/kotlin/io/tolgee/formats/FromIcuPlaceholderConvertor.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ interface FromIcuPlaceholderConvertor {
66
/**
77
* This method is called on the text parts (not argument parts) of the message
88
*/
9-
fun convertText(string: String): String
9+
fun convertText(
10+
node: MessagePatternUtil.TextNode,
11+
keepEscaping: Boolean,
12+
): String
1013

1114
/**
1215
* How to # in ICU plural form
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.tolgee.formats
2+
3+
class IcuToIcuPlaceholderConvertor : FromIcuPlaceholderConvertor {
4+
override fun convert(node: MessagePatternUtil.ArgNode): String {
5+
return node.patternString
6+
}
7+
8+
override fun convertText(
9+
node: MessagePatternUtil.TextNode,
10+
keepEscaping: Boolean,
11+
): String {
12+
return node.patternString
13+
}
14+
15+
override fun convertReplaceNumber(
16+
node: MessagePatternUtil.MessageContentsNode,
17+
argName: String?,
18+
): String = node.patternString
19+
}

backend/data/src/main/kotlin/io/tolgee/formats/MessageConvertorFactory.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package io.tolgee.formats
22

33
class MessageConvertorFactory(
44
private val message: String,
5-
private val forceIsPlural: Boolean? = null,
5+
private val forceIsPlural: Boolean,
66
private val isProjectIcuPlaceholdersEnabled: Boolean = false,
77
private val paramConvertorFactory: () -> FromIcuPlaceholderConvertor,
88
) {
@@ -11,9 +11,18 @@ class MessageConvertorFactory(
1111
message = message,
1212
argumentConvertorFactory = getParamConvertorFactory(),
1313
forceIsPlural = forceIsPlural,
14+
keepEscaping = keepEscaping,
1415
)
1516
}
1617

18+
private val keepEscaping by lazy {
19+
if (forceIsPlural) {
20+
return@lazy false
21+
}
22+
23+
return@lazy !isProjectIcuPlaceholdersEnabled
24+
}
25+
1726
private fun getParamConvertorFactory() =
1827
if (isProjectIcuPlaceholdersEnabled) {
1928
paramConvertorFactory

backend/data/src/main/kotlin/io/tolgee/formats/MessagePatternUtil.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,13 @@ object MessagePatternUtil {
396396
override fun toString(): String {
397397
return "«$text»"
398398
}
399+
400+
fun getText(keepEscaping: Boolean): String {
401+
if (keepEscaping) {
402+
return this.patternString
403+
}
404+
return this.text
405+
}
399406
}
400407

401408
/**

backend/data/src/main/kotlin/io/tolgee/formats/NoOpFromIcuPlaceholderConvertor.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ class NoOpFromIcuPlaceholderConvertor : FromIcuPlaceholderConvertor {
55
return node.patternString
66
}
77

8-
override fun convertText(string: String): String {
9-
return string
8+
override fun convertText(
9+
node: MessagePatternUtil.TextNode,
10+
keepEscaping: Boolean,
11+
): String {
12+
return node.getText(keepEscaping)
1013
}
1114

1215
override fun convertReplaceNumber(

backend/data/src/main/kotlin/io/tolgee/formats/android/out/IcuToJavaMessageConvertor.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ import io.tolgee.formats.paramConvertors.out.IcuToJavaPlaceholderConvertor
66

77
class IcuToJavaMessageConvertor(
88
private val message: String,
9-
private val forceIsPlural: Boolean? = null,
9+
private val forceIsPlural: Boolean,
1010
private val isProjectIcuPlaceholdersEnabled: Boolean,
1111
) {
1212
fun convert(): PossiblePluralConversionResult {
13-
return MessageConvertorFactory(message, forceIsPlural, isProjectIcuPlaceholdersEnabled) {
13+
return MessageConvertorFactory(
14+
message = message,
15+
forceIsPlural = forceIsPlural,
16+
isProjectIcuPlaceholdersEnabled = isProjectIcuPlaceholdersEnabled,
17+
) {
1418
IcuToJavaPlaceholderConvertor()
1519
}.create().convert()
1620
}

backend/data/src/main/kotlin/io/tolgee/formats/apple/out/IcuToAppleMessageConvertor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import io.tolgee.formats.paramConvertors.out.IcuToApplePlaceholderConvertor
66

77
class IcuToAppleMessageConvertor(
88
private val message: String,
9-
private val forceIsPlural: Boolean?,
9+
private val forceIsPlural: Boolean,
1010
private val isProjectIcuPlaceholdersEnabled: Boolean = true,
1111
) {
1212
fun convert(): PossiblePluralConversionResult {

backend/data/src/main/kotlin/io/tolgee/formats/flutter/out/FlutterArbFromIcuPlaceholderConvertor.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ class FlutterArbFromIcuPlaceholderConvertor : FromIcuPlaceholderConvertor {
88
return "{${node.name}}"
99
}
1010

11-
override fun convertText(string: String): String {
12-
return string
11+
override fun convertText(
12+
node: MessagePatternUtil.TextNode,
13+
keepEscaping: Boolean,
14+
): String {
15+
return node.getText(keepEscaping)
1316
}
1417

1518
override fun convertReplaceNumber(

0 commit comments

Comments
 (0)