Skip to content

Commit

Permalink
Merge pull request #521 from Lucchetto/fix-jvm-plural-fallback
Browse files Browse the repository at this point in the history
Fix plural fallback when given quantity not defined on JVM
  • Loading branch information
Alex009 authored May 22, 2024
2 parents 1f6c2af + 883a892 commit d0a9c3c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,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 =
Expand Down
1 change: 1 addition & 0 deletions samples/resources-gallery/mpp-library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ android {

lint.disable.add("ImpliedQuantity")
lint.disable.add("MissingTranslation")
lint.disable.add("MissingQuantity")
}

kotlin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@
<item quantity="two">plurals-interop: two</item>
<item quantity="other">plurals-interop: other</item>
</plurals>
<plural name="test.plural.fallback">
<item quantity="one">one</item>
<item quantity="other">other</item>
</plural>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@
<item quantity="two">%s two</item>
<item quantity="other">%s other</item>
</plural>
<plural name="test.plural.fallback">
<item quantity="one">один</item>
<item quantity="other">другое</item>
</plural>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 = """
Expand Down

0 comments on commit d0a9c3c

Please sign in to comment.