From 6881efc90404d3dfd7efee85c4257857d6cda50b Mon Sep 17 00:00:00 2001 From: Lucchetto Date: Sat, 1 Jul 2023 23:13:38 +0200 Subject: [PATCH 1/3] Add fallback for plural when string of quantity not defined on JVM Signed-off-by: Lucchetto --- .../kotlin/dev/icerock/moko/resources/PluralsResource.kt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/resources/src/jvmMain/kotlin/dev/icerock/moko/resources/PluralsResource.kt b/resources/src/jvmMain/kotlin/dev/icerock/moko/resources/PluralsResource.kt index 53c9cab56..0b87b1370 100644 --- a/resources/src/jvmMain/kotlin/dev/icerock/moko/resources/PluralsResource.kt +++ b/resources/src/jvmMain/kotlin/dev/icerock/moko/resources/PluralsResource.kt @@ -4,7 +4,6 @@ package dev.icerock.moko.resources -import com.ibm.icu.text.MessageFormat import com.ibm.icu.text.PluralRules import com.ibm.icu.util.ULocale import java.util.* @@ -22,7 +21,11 @@ actual class PluralsResource( val selectedVariant = pluralRules.select(quantity.toDouble()) val keyWithQuantity = "$key.$selectedVariant" - return resourceBundle.getString(keyWithQuantity) + return if (resourceBundle.containsKey(keyWithQuantity)) { + resourceBundle.getString(keyWithQuantity) + } else { + resourceBundle.getString("$key.${PluralRules.KEYWORD_OTHER}") + } } fun localized(locale: Locale = Locale.getDefault(), quantity: Int): String = From 047f1f4060d010e7f9f81e9a47991d4faa5087dc Mon Sep 17 00:00:00 2001 From: Lucchetto Date: Sun, 2 Jul 2023 10:23:59 +0200 Subject: [PATCH 2/3] Add tests for plural fallback Signed-off-by: Lucchetto --- .../commonMain/resources/MR/base/plurals-test.xml | 4 ++++ .../commonMain/resources/MR/ru/plurals-test.xml | 4 ++++ .../com/icerock/library/PluralResourceEnTests.kt | 12 ++++++++++++ .../com/icerock/library/PluralResourceRuTests.kt | 14 ++++++++++++-- 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/samples/resources-gallery/mpp-library/src/commonMain/resources/MR/base/plurals-test.xml b/samples/resources-gallery/mpp-library/src/commonMain/resources/MR/base/plurals-test.xml index ab64371c1..3443d601c 100644 --- a/samples/resources-gallery/mpp-library/src/commonMain/resources/MR/base/plurals-test.xml +++ b/samples/resources-gallery/mpp-library/src/commonMain/resources/MR/base/plurals-test.xml @@ -25,4 +25,8 @@ plurals-interop: two plurals-interop: other + + one + other + diff --git a/samples/resources-gallery/mpp-library/src/commonMain/resources/MR/ru/plurals-test.xml b/samples/resources-gallery/mpp-library/src/commonMain/resources/MR/ru/plurals-test.xml index 9430468c6..69351a7d9 100644 --- a/samples/resources-gallery/mpp-library/src/commonMain/resources/MR/ru/plurals-test.xml +++ b/samples/resources-gallery/mpp-library/src/commonMain/resources/MR/ru/plurals-test.xml @@ -33,4 +33,8 @@ %s two %s other + + один + другое + diff --git a/samples/resources-gallery/mpp-library/src/commonTest/kotlin/com/icerock/library/PluralResourceEnTests.kt b/samples/resources-gallery/mpp-library/src/commonTest/kotlin/com/icerock/library/PluralResourceEnTests.kt index 5946a6e36..934d5436b 100644 --- a/samples/resources-gallery/mpp-library/src/commonTest/kotlin/com/icerock/library/PluralResourceEnTests.kt +++ b/samples/resources-gallery/mpp-library/src/commonTest/kotlin/com/icerock/library/PluralResourceEnTests.kt @@ -50,6 +50,18 @@ class PluralResourceEnTests : BaseStringResourceTests("en") { actual = MR.plurals.test_plural.desc(22) ) + @Test + fun checkPluralFallback1() = pluralTest( + expected = "one", + actual = MR.plurals.test_plural_fallback.desc(1) + ) + + @Test + fun checkPluralFallback2() = pluralTest( + expected = "other", + actual = MR.plurals.test_plural_fallback.desc(2) + ) + @Test fun checkVariantsPlurals() = pluralTest( expected = """ diff --git a/samples/resources-gallery/mpp-library/src/commonTest/kotlin/com/icerock/library/PluralResourceRuTests.kt b/samples/resources-gallery/mpp-library/src/commonTest/kotlin/com/icerock/library/PluralResourceRuTests.kt index 01c2b9537..27c8d15b1 100644 --- a/samples/resources-gallery/mpp-library/src/commonTest/kotlin/com/icerock/library/PluralResourceRuTests.kt +++ b/samples/resources-gallery/mpp-library/src/commonTest/kotlin/com/icerock/library/PluralResourceRuTests.kt @@ -6,9 +6,7 @@ package com.icerock.library import com.icerockdev.library.MR import com.icerockdev.library.Testing -import dev.icerock.moko.resources.desc.StringDesc import dev.icerock.moko.resources.desc.desc -import kotlin.test.BeforeTest import kotlin.test.Ignore import kotlin.test.Test @@ -52,6 +50,18 @@ class PluralResourceRuTests : BaseStringResourceTests("ru") { actual = MR.plurals.test_plural.desc(22) ) + @Test + fun checkPluralFallback1() = pluralTest( + expected = "один", + actual = MR.plurals.test_plural_fallback.desc(1) + ) + + @Test + fun checkPluralFallback2() = pluralTest( + expected = "другое", + actual = MR.plurals.test_plural_fallback.desc(2) + ) + @Test fun checkVariantsPlurals() = pluralTest( expected = """ From 90b9ba0c31e0c7abf90eaf6429ae0e7290e962a8 Mon Sep 17 00:00:00 2001 From: Aleksey Mikhailov Date: Tue, 21 May 2024 18:46:27 +0700 Subject: [PATCH 3/3] suppress android lint in sample --- samples/resources-gallery/mpp-library/build.gradle.kts | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/resources-gallery/mpp-library/build.gradle.kts b/samples/resources-gallery/mpp-library/build.gradle.kts index c47c9d5db..c0b4c61b7 100644 --- a/samples/resources-gallery/mpp-library/build.gradle.kts +++ b/samples/resources-gallery/mpp-library/build.gradle.kts @@ -49,6 +49,7 @@ android { lint.disable.add("ImpliedQuantity") lint.disable.add("MissingTranslation") + lint.disable.add("MissingQuantity") } kotlin {