Skip to content

Commit

Permalink
Merge pull request #725 from icerockdev/#723-support-android-format-f…
Browse files Browse the repository at this point in the history
…or-strings-generator

#723 support android format for strings generator
  • Loading branch information
Alex009 authored May 30, 2024
2 parents 8f7f9e4 + b5db15a commit cf9b367
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 8 deletions.
2 changes: 1 addition & 1 deletion gradle/moko.versions.toml
Original file line number Diff line number Diff line change
@@ -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" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,7 +30,7 @@ internal class StringResourceGenerator(
val language: LanguageType = LanguageType.fromFileName(file.parentFile.name)
val strings: Map<KeyType, String> = loadLanguageStrings(file)
strings.map { (key: KeyType, text: String) ->
key to (language to text)
key to (language to text.removeAndroidMirroringFormat())
}
}.groupBy(
keySelector = { it.first },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,26 @@ 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 (it[0] == '@') {
replaceFirst("@", """\@""")
} else {
it
}
}
.replace("&quot;", "\\&quot;")
.replace("&apos;", "\\&apos;")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class XmlPluralsToPlatformTest {
fun simplePluralsAndroidTest(){
assertEquals(
expected = """%d count of \&quot;%s\&quot;""",
actual ="%d count of \"%s\"".convertXmlStringToAndroidLocalization(),
actual ="""%d count of "%s"""".convertXmlStringToAndroidLocalization(),
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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"""",
actual = TEXT_WITH_QUOTES.removeAndroidMirroringFormat()
)
}

@Test
fun quotesAndroidInOutFormat() {
assertEquals(
expected = """Same text with symbol\&apos;s @ ? somet\&apos;ing \&quot;word\&quot;""",
actual = TEXT_WITH_QUOTES
.removeAndroidMirroringFormat()
.convertXmlStringToAndroidLocalization()
)
}

private companion object {
private const val TEXT_WITH_QUOTES = """Same text with symbol's @ ? somet\'ing \"word""""

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,12 @@ class XmlStringsToPlatformTest {
actual = "\uD83D\uDE08".convertXmlStringToLocalization()
)
}

@Test
fun stringLikeAndroidLinkOnStringAndroidTest() {
assertEquals(
expected = """\@same text""",
actual = """@same text""".convertXmlStringToAndroidLocalization()
)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<string name="hello_world">Hello moko-resources World</string>
<string name="new_line_test">Text with
<string name="new_line_test">@Text with
newline w/o new line</string>
<string name="symbols_text">Text \n with " ' % @ * &amp; {} "same" inch's</string>
<string name="symbols_text">Text \n with " ' % @ * &amp; {} "same\" inch\'s</string>
<string name="font_style_text">Text with &lt;b&gt;bold&lt;/b&gt;, &lt;i&gt;italic&lt;/i&gt;, &lt;u&gt;underline&lt;/u&gt;</string>
</resources>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<string name="hello_world">Привет Мир moko-resources</string>
<string name="symbols_text">Текст \n с " ' % @ * &amp; {} "сколько-то" дюйм'ов</string>
<string name="symbols_text">Текст \n с " ' % @ * &amp; {} "сколько-то\" дюйм\'ов</string>
<string name="font_style_text">Текст с &lt;b&gt;жирный&lt;/b&gt;, &lt;i&gt;курсив&lt;/i&gt;, &lt;u&gt;подчеркивание&lt;/u&gt;</string>
</resources>

0 comments on commit cf9b367

Please sign in to comment.