Browse Source

Share PluralRuleList with languages with the same rule

pull/4519/head
Chanjung Kim 2 months ago
parent
commit
95f7314f62
  1. 17
      components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/intl/PluralRuleList.kt
  2. 5
      components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/PluralRulesTest.kt

17
components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/intl/PluralRuleList.kt

@ -22,7 +22,7 @@ internal class PluralRuleList(private val rules: Array<PluralRule>) {
companion object {
private val cacheMutex = Mutex()
private val cache = mutableMapOf<String, Deferred<PluralRuleList>>()
private val cache = Array<Deferred<PluralRuleList>?>(cldrPluralRuleLists.size) { null }
private val emptyList = PluralRuleList(emptyArray())
@OptIn(InternalResourceApi::class)
@ -31,13 +31,19 @@ internal class PluralRuleList(private val rules: Array<PluralRule>) {
regionQualifier: RegionQualifier,
): PluralRuleList {
val cldrLocaleName = buildCldrLocaleName(languageQualifier, regionQualifier) ?: return emptyList
return getInstance(cldrLocaleName)
}
suspend fun getInstance(cldrLocaleName: String): PluralRuleList {
val listIndex = cldrPluralRuleListIndexByLocale[cldrLocaleName]!!
return coroutineScope {
val deferred = cacheMutex.withLock {
cache.getOrPut(cldrLocaleName) {
async(start = CoroutineStart.LAZY) {
createInstance(cldrLocaleName)
if (cache[listIndex] == null) {
cache[listIndex] = async(start = CoroutineStart.LAZY) {
createInstance(listIndex)
}
}
cache[listIndex]!!
}
deferred.await()
}
@ -58,8 +64,7 @@ internal class PluralRuleList(private val rules: Array<PluralRule>) {
return null
}
internal fun createInstance(cldrLocaleName: String): PluralRuleList {
val cldrPluralRuleListIndex = cldrPluralRuleListIndexByLocale[cldrLocaleName]!!
private fun createInstance(cldrPluralRuleListIndex: Int): PluralRuleList {
val cldrPluralRuleList = cldrPluralRuleLists[cldrPluralRuleListIndex]
val pluralRules = cldrPluralRuleList.map { PluralRule(it.first, it.second) }
return PluralRuleList(pluralRules.toTypedArray())

5
components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/PluralRulesTest.kt

@ -5,6 +5,7 @@
package org.jetbrains.compose.resources
import kotlinx.coroutines.test.runTest
import org.jetbrains.compose.resources.intl.PluralCategory
import org.jetbrains.compose.resources.intl.PluralRuleList
import kotlin.test.*
@ -17,9 +18,9 @@ class PluralRulesTest {
* Tests the actual language pluralization rules with the integer samples given by Unicode.
*/
@Test
fun testIntegerSamples() {
fun testIntegerSamples() = runTest {
for ((locale, samplesByCategory) in cldrPluralRuleIntegerSamples) {
val pluralRuleList = PluralRuleList.createInstance(locale)
val pluralRuleList = PluralRuleList.getInstance(locale)
for ((category, samples) in samplesByCategory) {
for (sample in parsePluralSamples(samples)) {
assertEquals(category, pluralRuleList.getCategory(sample))

Loading…
Cancel
Save