Skip to content

Commit

Permalink
Merge pull request #711 from icerockdev/#710-plurals-generation-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex009 authored May 17, 2024
2 parents 61b3e46 + 82a0edd commit 5496aa8
Show file tree
Hide file tree
Showing 20 changed files with 198 additions and 32 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-4"
resourcesVersion = "0.24.0-beta-5"

[libraries]
resources = { module = "dev.icerock.moko:resources", version.ref = "resourcesVersion" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal class JsAssetResourceGenerator(
return CodeBlock.of(
"AssetResource(originalPath = js(%S) as String, rawPath = %S)",
requireDeclaration,
metadata.pathRelativeToBase
metadata.pathRelativeToBase.invariantSeparatorsPath
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import dev.icerock.gradle.generator.addAppleContainerBundleInitializerProperty
import dev.icerock.gradle.generator.addValuesFunction
import dev.icerock.gradle.generator.localization.LanguageType
import dev.icerock.gradle.metadata.resource.PluralMetadata
import dev.icerock.gradle.utils.convertXmlStringToLocalization
import dev.icerock.gradle.utils.convertXmlStringToApplePluralLocalization
import java.io.File

internal class ApplePluralResourceGenerator(
Expand Down Expand Up @@ -121,7 +121,7 @@ internal class ApplePluralResourceGenerator(

private fun String.escapeFormatArguments(): String {
return this
.convertXmlStringToLocalization()
.convertXmlStringToApplePluralLocalization()
.replace(Regex("%(((?:\\.|\\d|\\$)*)[abcdefs])"), "%%$1")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,33 @@ internal val String.flatName: String
get() = this.remove('.')

internal fun String.convertXmlStringToAndroidLocalization(): String {
return StringEscapeUtils.unescapeXml(this).let {
// Add mirroring for newline, without this android does flat string line
it.replace("\n", "\\n")
}.let {
StringEscapeUtils.escapeXml11(it)
}
// Android resources should comply with requirements:
// https://developer.android.com/guide/topics/resources/string-resource
return StringEscapeUtils
.unescapeXml(this)
.replace("\n", "\\n")
.let { StringEscapeUtils.escapeXml11(it) }
.replace(""", "\\"")
.replace("'", "\\'")
}

internal fun String.convertXmlStringToLocalization(): String {
return StringEscapeUtils.unescapeXml(this).let {
val jsonPrimitive = JsonPrimitive(it)
// Usage of inner encode mechanism of Koltinx.Serialization
val stringValue: String = jsonPrimitive.toString()
return StringEscapeUtils
.unescapeXml(this)
.let { value ->
val jsonPrimitive = JsonPrimitive(value)
// Usage of inner encode mechanism of Koltinx.Serialization
val stringValue: String = jsonPrimitive.toString()

// Remove symbol ["] from start and end of string
stringValue.substring(1, stringValue.length - 1)
}
}

// Remove symbol ["] from start and end of string
stringValue.substring(1, stringValue.length - 1)
}
internal fun String.convertXmlStringToApplePluralLocalization(): String {
return StringEscapeUtils
.unescapeXml(this)
.let { StringEscapeUtils.escapeXml11(it) }
}

internal val String.withoutAppearance: String
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2024 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.gradle.generator.pluralsGenerator

import dev.icerock.gradle.utils.convertXmlStringToAndroidLocalization
import dev.icerock.gradle.utils.convertXmlStringToApplePluralLocalization
import dev.icerock.gradle.utils.convertXmlStringToLocalization
import kotlin.test.Test
import kotlin.test.assertEquals

class XmlPluralsToPlatformRuTest {
@Test
fun simplePluralsAppleTest(){
assertEquals(
expected = """%d число "%s"""",
actual ="%d число \"%s\"".convertXmlStringToApplePluralLocalization(),
)
}

@Test
fun simplePluralsAndroidTest(){
assertEquals(
expected = """%d число \"%s\"""",
actual ="%d число \"%s\"".convertXmlStringToAndroidLocalization(),
)
}

@Test
fun simplePluralsOtherPlatformsTest() {
assertEquals(
expected = """%d число \"%s\"""",
actual ="%d число \"%s\"".convertXmlStringToLocalization(),
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2024 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.gradle.generator.pluralsGenerator

import dev.icerock.gradle.utils.convertXmlStringToAndroidLocalization
import dev.icerock.gradle.utils.convertXmlStringToApplePluralLocalization
import dev.icerock.gradle.utils.convertXmlStringToLocalization
import kotlin.test.Test
import kotlin.test.assertEquals

class XmlPluralsToPlatformTest {
@Test
fun simplePluralsAppleTest(){
assertEquals(
expected = """%d count of "%s"""",
actual ="%d count of \"%s\"".convertXmlStringToApplePluralLocalization(),
)
}

@Test
fun simplePluralsAndroidTest(){
assertEquals(
expected = """%d count of \"%s\"""",
actual ="%d count of \"%s\"".convertXmlStringToAndroidLocalization(),
)
}

@Test
fun simplePluralsOtherPlatformsTest() {
assertEquals(
expected = """%d count of \"%s\"""",
actual ="%d count of \"%s\"".convertXmlStringToLocalization(),
)
}

@Test
fun pluralWithNewLineAppleTest() {
assertEquals(
expected = """%d count
|of tests""".trimMargin(),
actual ="%d count\nof tests".convertXmlStringToApplePluralLocalization(),
)
}

@Test
fun pluralWithNewLineAndroidTest() {
assertEquals(
expected = """%d count\nof tests""",
actual ="%d count\nof tests".convertXmlStringToAndroidLocalization(),
)
}

@Test
fun pluralWithNewLineOtherPlatformTest() {
assertEquals(
expected = """%d count\nof tests""",
actual ="%d count\nof tests".convertXmlStringToLocalization(),
)
}

@Test
fun separateSymbolsApplePluralsTest() {
assertEquals(
expected = """" ' % @ * & {}""",
actual = "\" ' % @ * & {}".convertXmlStringToApplePluralLocalization()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class XmlStringsToPlatformRuTest {
@Test
fun textWithApostropheAndroid() {
assertEquals(
expected = """Я'ж купил новый 27 дюйм'ов монитор""",
expected = """Я\'ж купил новый 27 дюйм\'ов монитор""",
actual = "Я'ж купил новый 27 дюйм'ов монитор".convertXmlStringToAndroidLocalization()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package dev.icerock.gradle.generator.stringsGenerator

import dev.icerock.gradle.utils.convertXmlStringToAndroidLocalization
import dev.icerock.gradle.utils.convertXmlStringToApplePluralLocalization
import dev.icerock.gradle.utils.convertXmlStringToLocalization
import kotlin.test.Test
import kotlin.test.assertEquals
Expand All @@ -29,7 +30,7 @@ class XmlStringsToPlatformTest {
@Test
fun separateSymbolsAndroid() {
assertEquals(
expected = """" ' % @ * & {}""",
expected = """\" \' % @ * & {}""",
actual = "\" ' % @ * & {}".convertXmlStringToAndroidLocalization()
)
}
Expand All @@ -45,7 +46,7 @@ class XmlStringsToPlatformTest {
@Test
fun textWithApostropheAndroid() {
assertEquals(
expected = """I'm bought new monitor with 27 inch's""",
expected = """I\'m bought new monitor with 27 inch\'s""",
actual = "I'm bought new monitor with 27 inch's".convertXmlStringToAndroidLocalization()
)
}
Expand Down Expand Up @@ -73,4 +74,52 @@ class XmlStringsToPlatformTest {
actual = "Text with <b>bold</b>, <i>italic</i>, <u>underline</u>".convertXmlStringToLocalization(),
)
}

@Test
fun textWithQuotesAndroid() {
assertEquals(
expected = """%d count \"%s\"""",
actual = "%d count \"%s\"".convertXmlStringToAndroidLocalization()
)
}

@Test
fun textWithQuotesOtherPlatforms() {
assertEquals(
expected = """%d count \"%s\"""",
actual = "%d count \"%s\"".convertXmlStringToAndroidLocalization()
)
}

@Test
fun unicodeEmojiAppleTest() {
assertEquals(
expected = """😈""",
actual = "\uD83D\uDE08".convertXmlStringToLocalization()
)
}

@Test
fun unicodeEmojiApplePluralTest() {
assertEquals(
expected = """😈""",
actual = "\uD83D\uDE08".convertXmlStringToApplePluralLocalization()
)
}

@Test
fun unicodeEmojiAndroidTest() {
assertEquals(
expected = """😈""",
actual = "\uD83D\uDE08".convertXmlStringToAndroidLocalization()
)
}

@Test
fun unicodeEmojiOtherPlatformTest() {
assertEquals(
expected = """😈""",
actual = "\uD83D\uDE08".convertXmlStringToLocalization()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<string name="hello_world">Hello moko-resources World</string>
<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>
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
<string name="positional">second string %2$s first decimal %1$d</string>
<string name="multilined">first line\nsecond line\nthird line.</string>
<string name="quotes">Alex009 said "hello world" &amp; "write tests".</string>
<string name="single_quotes">Alex009 said \'hello\'</string>
<string name="single_quotes">Alex009 said 'hello'</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
<string name="positional">second string %2$s first decimal %1$d</string>
<string name="multilined">first line\nsecond line\nthird line.</string>
<string name="quotes">Alex009 said "hello world" &amp; "write tests".</string>
<string name="single_quotes">Alex009 said \'hello\'</string>
<string name="single_quotes">Alex009 said 'hello'</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
<string name="positional">second string %2$s first decimal %1$d</string>
<string name="multilined">first line\nsecond line\nthird line.</string>
<string name="quotes">Alex009 said "hello world" &amp; "write tests".</string>
<string name="single_quotes">Alex009 said \'hello\'</string>
<string name="single_quotes">Alex009 said 'hello'</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
<string name="positional">segunda cadena %2$s primer decimal %1$d</string>
<string name="multilined">primera línea\nsegunda línea\ntercera línea.</string>
<string name="quotes">Alex009 dijo "hola mundo" &amp; "escribir pruebas".</string>
<string name="single_quotes">Alex009 dijo \'hola\'</string>
<string name="single_quotes">Alex009 dijo 'hola'</string>
<string name="test.dialect">Spanish</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
<string name="positional">вторая строка %2$s первое число %1$d</string>
<string name="multilined">первая строка\nвторая строка\nтретья строка.</string>
<string name="quotes">Alex009 сказал "привет мир" &amp; "пишите тесты".</string>
<string name="single_quotes">Alex009 сказал \'привет\'</string>
<string name="single_quotes">Alex009 сказал 'привет'</string>
<string name="test.dialect">Russian</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
<string name="positional">second string %2$s first decimal %1$d</string>
<string name="multilined">first line\nsecond line\nthird line.</string>
<string name="quotes">Alex009 said "hello world" &amp; "write tests".</string>
<string name="single_quotes">Alex009 said \'hello\'</string>
<string name="single_quotes">Alex009 said 'hello'</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
<string name="positional">second string %2$s first decimal %1$d</string>
<string name="multilined">first line\nsecond line\nthird line.</string>
<string name="quotes">Alex009 said "hello world" &amp; "write tests".</string>
<string name="single_quotes">Alex009 said \'hello\'</string>
<string name="single_quotes">Alex009 said 'hello'</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
<string name="positional">second string %2$s first decimal %1$d</string>
<string name="multilined">first line\nsecond line\nthird line.</string>
<string name="quotes">Alex009 said "hello world" &amp; "write tests".</string>
<string name="single_quotes">Alex009 said \'hello\'</string>
<string name="single_quotes">Alex009 said 'hello'</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
<string name="positional">segunda cadena %2$s primer decimal %1$d</string>
<string name="multilined">primera línea\nsegunda línea\ntercera línea.</string>
<string name="quotes">Alex009 dijo "hola mundo" &amp; "escribir pruebas".</string>
<string name="single_quotes">Alex009 dijo \'hola\'</string>
<string name="single_quotes">Alex009 dijo 'hola'</string>
<string name="test.dialect">Spanish</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
<string name="positional">вторая строка %2$s первое число %1$d</string>
<string name="multilined">первая строка\nвторая строка\nтретья строка.</string>
<string name="quotes">Alex009 сказал "привет мир" &amp; "пишите тесты".</string>
<string name="single_quotes">Alex009 сказал \'привет\'</string>
<string name="single_quotes">Alex009 сказал 'привет'</string>
<string name="test.dialect">Russian</string>
</resources>

0 comments on commit 5496aa8

Please sign in to comment.