jvmMain.dev.icerock.moko.resources.PluralsResource.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of resources-jvm Show documentation
Show all versions of resources-jvm Show documentation
Resources access for Kotlin Multiplatform development (mobile first)
The newest version!
/*
* Copyright 2021 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/
package dev.icerock.moko.resources
import com.ibm.icu.text.PluralRules
import com.ibm.icu.util.ULocale
import java.util.Locale
actual class PluralsResource(
val resourcesClassLoader: ClassLoader,
val bundleName: String,
val key: String
) {
private fun getPluralMessage(locale: Locale, quantity: Int): String {
val resourceBundle = resourcesClassLoader.getResourceBundle(bundleName, locale)
val pluralRules = PluralRules.forLocale(ULocale.forLocale(locale))
val selectedVariant = pluralRules.select(quantity.toDouble())
val keyWithQuantity = "$key.$selectedVariant"
return if (resourceBundle.containsKey(keyWithQuantity)) {
resourceBundle.getString(keyWithQuantity)
} else {
resourceBundle.getString("$key.${PluralRules.KEYWORD_OTHER}")
}
}
fun localized(locale: Locale = Locale.getDefault(), quantity: Int): String =
getPluralMessage(locale, quantity)
fun localized(locale: Locale = Locale.getDefault(), quantity: Int, vararg args: Any?): String =
String.format(locale = locale, getPluralMessage(locale, quantity), *args)
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy