From 196a1d81dc3ae5df70f6b26f9e961ae96c647e4b Mon Sep 17 00:00:00 2001 From: Konstantin Kolchurin Date: Mon, 27 May 2024 17:41:43 +0700 Subject: [PATCH 1/8] #723 android format adaptation --- gradle/moko.versions.toml | 2 +- .../plural/PluralResourceGenerator.kt | 3 ++- .../string/StringResourceGenerator.kt | 3 ++- .../dev/icerock/gradle/utils/StringExt.kt | 18 +++++++++++++++++- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/gradle/moko.versions.toml b/gradle/moko.versions.toml index d83767d1..f4effae7 100644 --- a/gradle/moko.versions.toml +++ b/gradle/moko.versions.toml @@ -1,5 +1,5 @@ [versions] -resourcesVersion = "0.24.0-beta-5" +resourcesVersion = "0.24.0-beta-6" [libraries] resources = { module = "dev.icerock.moko:resources", version.ref = "resourcesVersion" } diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/plural/PluralResourceGenerator.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/plural/PluralResourceGenerator.kt index d25a58c8..b1c242c7 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/plural/PluralResourceGenerator.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/plural/PluralResourceGenerator.kt @@ -11,6 +11,7 @@ import dev.icerock.gradle.generator.generateKey import dev.icerock.gradle.generator.localization.LanguageType import dev.icerock.gradle.metadata.resource.PluralMetadata import dev.icerock.gradle.utils.processXmlTextContent +import dev.icerock.gradle.utils.removeAndroidMirroringFormat import org.w3c.dom.Document import org.w3c.dom.Element import org.w3c.dom.Node @@ -79,7 +80,7 @@ internal class PluralResourceGenerator( val item: Node = itemNodes.item(j) val quantity: String = item.attributes.getNamedItem("quantity").textContent.trim() - val value: String = item.textContent + val value: String = item.textContent.removeAndroidMirroringFormat() pluralMap[quantity] = value.processXmlTextContent(strictLineBreaks) } diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/string/StringResourceGenerator.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/string/StringResourceGenerator.kt index a79792c4..cf27c9df 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/string/StringResourceGenerator.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/string/StringResourceGenerator.kt @@ -11,6 +11,7 @@ import dev.icerock.gradle.generator.generateKey import dev.icerock.gradle.generator.localization.LanguageType import dev.icerock.gradle.metadata.resource.StringMetadata import dev.icerock.gradle.utils.processXmlTextContent +import dev.icerock.gradle.utils.removeAndroidMirroringFormat import org.w3c.dom.Document import org.w3c.dom.Node import org.w3c.dom.NodeList @@ -29,7 +30,7 @@ internal class StringResourceGenerator( val language: LanguageType = LanguageType.fromFileName(file.parentFile.name) val strings: Map = loadLanguageStrings(file) strings.map { (key: KeyType, text: String) -> - key to (language to text) + key to (language to text.removeAndroidMirroringFormat()) } }.groupBy( keySelector = { it.first }, diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt index 803b37fa..2feb46ac 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt @@ -49,15 +49,31 @@ internal fun String.remove(char: String): String { internal val String.flatName: String get() = this.remove('.') +internal fun String.removeAndroidMirroringFormat(): String { + // Remove android format from string + return replace("""\'""","'") + .replace("""\"""","\"") +} + internal fun String.convertXmlStringToAndroidLocalization(): String { // Android resources should comply with requirements: - // https://developer.android.com/guide/topics/resources/string-resource + // https://developer.android.com/guide/topics/resources/string-resource#escaping_quotes return StringEscapeUtils .unescapeXml(this) .replace("\n", "\\n") .let { StringEscapeUtils.escapeXml11(it) } + .let { + if (this[0] == '@') { + replace("@", """\@""") + } else { + this + } + } + .replace("\"", """\"""") + .replace("\'", """\'""") .replace(""", "\\"") .replace("'", "\\'") + .replace("&", "&") } internal fun String.convertXmlStringToLocalization(): String { From 94113a81b0b9f3649c7f85007be9ea496370b066 Mon Sep 17 00:00:00 2001 From: Konstantin Kolchurin Date: Mon, 27 May 2024 17:41:59 +0700 Subject: [PATCH 2/8] #723 fix sample string in sample --- .../shared/src/commonMain/moko-resources/base/strings.xml | 4 ++-- .../shared/src/commonMain/moko-resources/ru/strings.xml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/compose-resources-gallery/shared/src/commonMain/moko-resources/base/strings.xml b/samples/compose-resources-gallery/shared/src/commonMain/moko-resources/base/strings.xml index b7308d59..9fb36169 100644 --- a/samples/compose-resources-gallery/shared/src/commonMain/moko-resources/base/strings.xml +++ b/samples/compose-resources-gallery/shared/src/commonMain/moko-resources/base/strings.xml @@ -1,8 +1,8 @@ Hello moko-resources World - Text with + @Text with newline w/o new line - Text \n with " ' % @ * & {} "same" inch's + Text \n with " ' % @ * & {} "same\" inch\'s Text with <b>bold</b>, <i>italic</i>, <u>underline</u> diff --git a/samples/compose-resources-gallery/shared/src/commonMain/moko-resources/ru/strings.xml b/samples/compose-resources-gallery/shared/src/commonMain/moko-resources/ru/strings.xml index e331bdf2..24f0ad22 100644 --- a/samples/compose-resources-gallery/shared/src/commonMain/moko-resources/ru/strings.xml +++ b/samples/compose-resources-gallery/shared/src/commonMain/moko-resources/ru/strings.xml @@ -1,6 +1,6 @@ Привет Мир moko-resources - Текст \n с " ' % @ * & {} "сколько-то" дюйм'ов + Текст \n с " ' % @ * & {} "сколько-то\" дюйм\'ов Текст с <b>жирный</b>, <i>курсив</i>, <u>подчеркивание</u> From e85c924e59b11f51efec744eae9b7fc192868314 Mon Sep 17 00:00:00 2001 From: Konstantin Kolchurin Date: Mon, 27 May 2024 17:42:36 +0700 Subject: [PATCH 3/8] #723 add string like android link to string from resource in test --- .../stringsGenerator/XmlStringsToPlatformTest.kt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/resources-generator/src/test/kotlin/dev/icerock/gradle/generator/stringsGenerator/XmlStringsToPlatformTest.kt b/resources-generator/src/test/kotlin/dev/icerock/gradle/generator/stringsGenerator/XmlStringsToPlatformTest.kt index a60ae22a..df66f22e 100644 --- a/resources-generator/src/test/kotlin/dev/icerock/gradle/generator/stringsGenerator/XmlStringsToPlatformTest.kt +++ b/resources-generator/src/test/kotlin/dev/icerock/gradle/generator/stringsGenerator/XmlStringsToPlatformTest.kt @@ -122,4 +122,12 @@ class XmlStringsToPlatformTest { actual = "\uD83D\uDE08".convertXmlStringToLocalization() ) } + + @Test + fun stringLikeAndroidLinkOnStringAndroidTest() { + assertEquals( + expected = """\@same text""", + actual = """@same text""".convertXmlStringToAndroidLocalization() + ) + } } From 006b8ca11b8b9d928dd7775d65b61c7adf96a02d Mon Sep 17 00:00:00 2001 From: Konstantin Kolchurin Date: Mon, 27 May 2024 17:44:45 +0700 Subject: [PATCH 4/8] #723 add tests for android format parsing --- .../XmlStringsAndroidFormatTest.kt | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 resources-generator/src/test/kotlin/dev/icerock/gradle/generator/stringsGenerator/XmlStringsAndroidFormatTest.kt diff --git a/resources-generator/src/test/kotlin/dev/icerock/gradle/generator/stringsGenerator/XmlStringsAndroidFormatTest.kt b/resources-generator/src/test/kotlin/dev/icerock/gradle/generator/stringsGenerator/XmlStringsAndroidFormatTest.kt new file mode 100644 index 00000000..15b20e76 --- /dev/null +++ b/resources-generator/src/test/kotlin/dev/icerock/gradle/generator/stringsGenerator/XmlStringsAndroidFormatTest.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2024 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. + */ + +package dev.icerock.gradle.generator.stringsGenerator + +import dev.icerock.gradle.utils.convertXmlStringToAndroidLocalization +import dev.icerock.gradle.utils.removeAndroidMirroringFormat +import kotlin.test.Test +import kotlin.test.assertEquals + +class XmlStringsAndroidFormatTest { + @Test + fun quotesAndroidFormat() { + assertEquals( + expected = """Same text with symbol's @ ? somet'ing "word" ad asd""", + actual = """Same text with symbol's @ ? somet\'ing \"word" ad asd""".removeAndroidMirroringFormat() + ) + } + + @Test + fun quotesAndroidInOutFormat() { + assertEquals( + expected = """Same text with symbol\'s @ ? somet\'ing \"word\"""", + actual = """Same text with symbol's @ ? somet\'ing \"word"""" + .removeAndroidMirroringFormat() + .convertXmlStringToAndroidLocalization() + ) + } +} From ce54b4bf3d071967331ed16cac1eef7c5f3cff6f Mon Sep 17 00:00:00 2001 From: Konstantin Kolchurin Date: Mon, 27 May 2024 17:53:07 +0700 Subject: [PATCH 5/8] #723 reformat --- .../src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt index 2feb46ac..f66ca972 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt @@ -51,8 +51,8 @@ internal val String.flatName: String internal fun String.removeAndroidMirroringFormat(): String { // Remove android format from string - return replace("""\'""","'") - .replace("""\"""","\"") + return replace("""\'""", "'") + .replace("""\"""", "\"") } internal fun String.convertXmlStringToAndroidLocalization(): String { From c2eb3447f5c1ffec85deb803f2d8f6cba881c726 Mon Sep 17 00:00:00 2001 From: Konstantin Kolchurin Date: Mon, 27 May 2024 20:41:55 +0700 Subject: [PATCH 6/8] #723 fix tests --- .../kotlin/dev/icerock/gradle/utils/StringExt.kt | 5 ++--- .../pluralsGenerator/XmlPluralsToPlatformTest.kt | 2 +- .../stringsGenerator/XmlStringsAndroidFormatTest.kt | 13 +++++++++---- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt index f66ca972..a24be9fa 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt @@ -63,17 +63,16 @@ internal fun String.convertXmlStringToAndroidLocalization(): String { .replace("\n", "\\n") .let { StringEscapeUtils.escapeXml11(it) } .let { - if (this[0] == '@') { + if (it[0] == '@') { replace("@", """\@""") } else { - this + it } } .replace("\"", """\"""") .replace("\'", """\'""") .replace(""", "\\"") .replace("'", "\\'") - .replace("&", "&") } internal fun String.convertXmlStringToLocalization(): String { diff --git a/resources-generator/src/test/kotlin/dev/icerock/gradle/generator/pluralsGenerator/XmlPluralsToPlatformTest.kt b/resources-generator/src/test/kotlin/dev/icerock/gradle/generator/pluralsGenerator/XmlPluralsToPlatformTest.kt index e0a348c7..0468e615 100644 --- a/resources-generator/src/test/kotlin/dev/icerock/gradle/generator/pluralsGenerator/XmlPluralsToPlatformTest.kt +++ b/resources-generator/src/test/kotlin/dev/icerock/gradle/generator/pluralsGenerator/XmlPluralsToPlatformTest.kt @@ -23,7 +23,7 @@ class XmlPluralsToPlatformTest { fun simplePluralsAndroidTest(){ assertEquals( expected = """%d count of \"%s\"""", - actual ="%d count of \"%s\"".convertXmlStringToAndroidLocalization(), + actual ="""%d count of "%s"""".convertXmlStringToAndroidLocalization(), ) } diff --git a/resources-generator/src/test/kotlin/dev/icerock/gradle/generator/stringsGenerator/XmlStringsAndroidFormatTest.kt b/resources-generator/src/test/kotlin/dev/icerock/gradle/generator/stringsGenerator/XmlStringsAndroidFormatTest.kt index 15b20e76..91289160 100644 --- a/resources-generator/src/test/kotlin/dev/icerock/gradle/generator/stringsGenerator/XmlStringsAndroidFormatTest.kt +++ b/resources-generator/src/test/kotlin/dev/icerock/gradle/generator/stringsGenerator/XmlStringsAndroidFormatTest.kt @@ -13,18 +13,23 @@ class XmlStringsAndroidFormatTest { @Test fun quotesAndroidFormat() { assertEquals( - expected = """Same text with symbol's @ ? somet'ing "word" ad asd""", - actual = """Same text with symbol's @ ? somet\'ing \"word" ad asd""".removeAndroidMirroringFormat() + expected = """Same text with symbol's @ ? somet'ing "word"""", + actual = TEXT_WITH_QUOTES.removeAndroidMirroringFormat() ) } @Test fun quotesAndroidInOutFormat() { assertEquals( - expected = """Same text with symbol\'s @ ? somet\'ing \"word\"""", - actual = """Same text with symbol's @ ? somet\'ing \"word"""" + expected = """Same text with symbol\'s @ ? somet\'ing \"word\"""", + actual = TEXT_WITH_QUOTES .removeAndroidMirroringFormat() .convertXmlStringToAndroidLocalization() ) } + + private companion object { + private const val TEXT_WITH_QUOTES = """Same text with symbol's @ ? somet\'ing \"word"""" + + } } From 55c3907d3c2f5803e3735ad98ed1da24c5cab6a9 Mon Sep 17 00:00:00 2001 From: Konstantin Kolchurin Date: Mon, 27 May 2024 20:45:06 +0700 Subject: [PATCH 7/8] #723 fix replace @ in android string --- .../src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt index a24be9fa..e701c415 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt @@ -64,7 +64,7 @@ internal fun String.convertXmlStringToAndroidLocalization(): String { .let { StringEscapeUtils.escapeXml11(it) } .let { if (it[0] == '@') { - replace("@", """\@""") + replaceFirst("@", """\@""") } else { it } From b5db15aba1fb841e6c737458131644a93d1872b4 Mon Sep 17 00:00:00 2001 From: Konstantin Kolchurin Date: Tue, 28 May 2024 21:34:41 +0700 Subject: [PATCH 8/8] #723 remove useless replaces --- .../src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt index e701c415..89bc16fa 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/utils/StringExt.kt @@ -69,8 +69,6 @@ internal fun String.convertXmlStringToAndroidLocalization(): String { it } } - .replace("\"", """\"""") - .replace("\'", """\'""") .replace(""", "\\"") .replace("'", "\\'") }