Skip to content

Commit

Permalink
Update from ChoiceFormat to PluralRules
Browse files Browse the repository at this point in the history
  • Loading branch information
nrobi144 committed Mar 29, 2021
1 parent 18d772e commit 97489b6
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ abstract class BaseGenerator<T> : MRGenerator.Generator {
PropertySpec.builder(name, resourceClassName)
property.addModifiers(*getPropertyModifiers())
getPropertyInitializer(
key,
loadLanguageMap()[BASE_LANGUAGE].orEmpty()
key
)?.let { property.initializer(it) }
objectBuilder.addProperty(property.build())
}
Expand All @@ -53,10 +52,7 @@ abstract class BaseGenerator<T> : MRGenerator.Generator {
}

protected abstract fun loadLanguageMap(): Map<LanguageType, Map<KeyType, T>>
protected abstract fun getPropertyInitializer(
key: String,
baseLanguageMap: Map<KeyType, T>
): CodeBlock?
protected abstract fun getPropertyInitializer(key: String): CodeBlock?

protected open fun getClassModifiers(): Array<KModifier> = emptyArray()
protected open fun getPropertyModifiers(): Array<KModifier> = emptyArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AndroidPluralsGenerator(

override fun getPropertyModifiers(): Array<KModifier> = arrayOf(KModifier.ACTUAL)

override fun getPropertyInitializer(key: String, baseLanguageMap: Map<KeyType, PluralMap>) =
override fun getPropertyInitializer(key: String) =
CodeBlock.of("PluralsResource(R.plurals.%L)", processKey(key))

override fun getImports(): List<ClassName> = listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AndroidStringsGenerator(

override fun getPropertyModifiers(): Array<KModifier> = arrayOf(KModifier.ACTUAL)

override fun getPropertyInitializer(key: String, baseLanguageMap: Map<KeyType, String>) =
override fun getPropertyInitializer(key: String) =
CodeBlock.of("StringResource(R.string.%L)", processKey(key))

override fun getImports(): List<ClassName> = listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ class ApplePluralsGenerator(

override fun getPropertyModifiers(): Array<KModifier> = arrayOf(KModifier.ACTUAL)

override fun getPropertyInitializer(
key: String,
baseLanguageMap: Map<KeyType, PluralMap>
) = CodeBlock.of(
override fun getPropertyInitializer(key: String) = CodeBlock.of(
"PluralsResource(resourceId = %S, bundle = ${AppleMRGenerator.BUNDLE_PROPERTY_NAME})",
key
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ class AppleStringsGenerator(

override fun getPropertyModifiers(): Array<KModifier> = arrayOf(KModifier.ACTUAL)

override fun getPropertyInitializer(
key: String,
baseLanguageMap: Map<KeyType, String>
) = CodeBlock.of("StringResource(resourceId = %S, bundle = $BUNDLE_PROPERTY_NAME)", key)
override fun getPropertyInitializer(key: String) =
CodeBlock.of("StringResource(resourceId = %S, bundle = $BUNDLE_PROPERTY_NAME)", key)

override fun generateResources(
resourcesGenerationDir: File,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
package dev.icerock.gradle.generator.common

import com.squareup.kotlinpoet.CodeBlock
import dev.icerock.gradle.generator.KeyType
import dev.icerock.gradle.generator.NOPObjectBodyExtendable
import dev.icerock.gradle.generator.ObjectBodyExtendable
import dev.icerock.gradle.generator.PluralMap
import dev.icerock.gradle.generator.PluralsGenerator
import org.gradle.api.file.FileTree

class CommonPluralsGenerator(
pluralsFileTree: FileTree
) : PluralsGenerator(pluralsFileTree), ObjectBodyExtendable by NOPObjectBodyExtendable() {
override fun getPropertyInitializer(
key: String,
baseLanguageMap: Map<KeyType, PluralMap>
key: String
): CodeBlock? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package dev.icerock.gradle.generator.common

import com.squareup.kotlinpoet.CodeBlock
import dev.icerock.gradle.generator.KeyType
import dev.icerock.gradle.generator.NOPObjectBodyExtendable
import dev.icerock.gradle.generator.ObjectBodyExtendable
import dev.icerock.gradle.generator.StringsGenerator
Expand All @@ -14,8 +13,5 @@ import org.gradle.api.file.FileTree
class CommonStringsGenerator(
stringsFileTree: FileTree
) : StringsGenerator(stringsFileTree), ObjectBodyExtendable by NOPObjectBodyExtendable() {
override fun getPropertyInitializer(
key: String,
baseLanguageMap: Map<KeyType, String>
): CodeBlock? = null
override fun getPropertyInitializer(key: String): CodeBlock? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ class JvmPluralsGenerator(

override fun getPropertyModifiers(): Array<KModifier> = arrayOf(KModifier.ACTUAL)

override fun getPropertyInitializer(key: String, baseLanguageMap: Map<KeyType, PluralMap>) =
override fun getPropertyInitializer(key: String) =
CodeBlock.of(
"PluralsResource(resourcesClassLoader = resourcesClassLoader, bundleName = %L, key = %S, numberFormat = %L)",
"PluralsResource(resourcesClassLoader = resourcesClassLoader, bundleName = %L, key = %S)",
JvmMRGenerator.PLURALS_BUNDLE_PROPERTY_NAME,
key,
getNumberFormat(key, baseLanguageMap)
)

override fun generateResources(
Expand All @@ -45,56 +44,15 @@ class JvmPluralsGenerator(
val stringsFile = File(localizationDir, "$fileDirName.properties")

val content = strings.map { (key, pluralMap) ->
"$key = {0}\n" +
pluralMap.map { (quantity, value) ->
"${
getQuantityKey(
key = key,
quantity = quantity
)
} = ${value.replaceAndroidFormatParameters()}"
}.joinToString("\n")
val keysWithPlurals = pluralMap.map { (quantity, value) ->
"$key.$quantity" to value
}

keysWithPlurals.joinToString("\n") { (key, value) ->
"$key = ${value.replaceAndroidFormatParameters()}"
}
}.joinToString("\n")

stringsFile.writeText(content)
}

private fun getNumberFormat(key: String, baseLanguageMap: Map<KeyType, PluralMap>) =
baseLanguageMap[key]?.let {
"listOf(${
it.mapNotNull { (quantity, _) ->
val quantityInNumber =
mapAndroidQuantityToDouble(quantity) ?: return@mapNotNull null
"${quantityInNumber.toDouble()} to \"${
getQuantityKey(
key = key,
quantity = quantity
)
}\""
}.joinToString()
})"
}

private fun getQuantityKey(key: String, quantity: String) =
"${key}_${QUANTITY_PREFIX}_${mapAndroidQuantityToDouble(quantity)}"

// Other will be skipped
private fun mapAndroidQuantityToDouble(quantity: String) = when (quantity) {
"zero" -> ZERO
"one" -> ONE
"two" -> TWO
"few" -> FEW
"many" -> MANY
else -> null
}

companion object {
private const val QUANTITY_PREFIX = "quantity"
private const val ZERO = 0
private const val ONE = 1
private const val TWO = 2
private const val FEW = 3
private const val MANY = 6
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class JvmStringsGenerator(

override fun getPropertyModifiers(): Array<KModifier> = arrayOf(KModifier.ACTUAL)

override fun getPropertyInitializer(key: String, baseLanguageMap: Map<KeyType, String>) =
override fun getPropertyInitializer(key: String) =
CodeBlock.of(
"StringResource(resourcesClassLoader = resourcesClassLoader, bundleName = %L, key = %S)",
JvmMRGenerator.STRINGS_BUNDLE_PROPERTY_NAME,
Expand Down
6 changes: 5 additions & 1 deletion resources/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ kotlin {
jvm()
sourceSets {
val commonMain by getting
val jvmMain by getting
val jvmMain by getting {
dependencies {
implementation("com.ibm.icu:icu4j:68.2")
}
}
val appleMain by creating {
dependsOn(commonMain)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,29 @@

package dev.icerock.moko.resources

import java.text.ChoiceFormat
import java.text.Format
import java.text.MessageFormat
import java.util.Locale
import com.ibm.icu.text.MessageFormat
import com.ibm.icu.text.PluralRules
import com.ibm.icu.util.ULocale
import java.util.*

actual class PluralsResource(
val resourcesClassLoader: ClassLoader,
val bundleName: String,
val key: String,
val numberFormat: List<Pair<Double, String>>
val key: String
) {
fun localized(locale: Locale = Locale.getDefault(), quantity: Int): String {
val resourceBundle = resourcesClassLoader.getResourceBundle(bundleName, locale)

val limits = numberFormat.map { it.first }
val strings = numberFormat.map { resourceBundle.getString(it.second) }
val choiceFormat = ChoiceFormat(limits.toDoubleArray(), strings.toTypedArray())
private fun getPluralMessage(locale: Locale, quantity: Int): String {
val resourceBundle = resourcesClassLoader.getResourceBundle(bundleName, locale)

val message = resourceBundle.getString(key)
val messageFormat = MessageFormat(message, locale)
messageFormat.formats = arrayOf<Format?>(choiceFormat)
val pluralRules = PluralRules.forLocale(ULocale.forLocale(locale))
val keyWithQuantity = "$key.${pluralRules.select(quantity.toDouble())}"

val messageArguments: Array<out Any> = arrayOf(quantity)
return messageFormat.format(messageArguments)
return resourceBundle.getString(keyWithQuantity)
}

fun localized(locale: Locale = Locale.getDefault(), quantity: Int, vararg args: Any): String {
val resourceBundle = resourcesClassLoader.getResourceBundle(bundleName, locale)

val limits = numberFormat.map { it.first }
val strings = numberFormat.map { resourceBundle.getString(it.second) }
val choiceFormat = ChoiceFormat(limits.toDoubleArray(), strings.toTypedArray())

val message = resourceBundle.getString(key)
val messageFormat = MessageFormat(message, locale)
messageFormat.formats =
arrayOf<Format?>(choiceFormat) + args.map { null }.toTypedArray<Format?>()
fun localized(locale: Locale = Locale.getDefault(), quantity: Int): String =
MessageFormat.format(getPluralMessage(locale, quantity))

val messageArguments: Array<out Any> = arrayOf(quantity, *args)
return messageFormat.format(messageArguments)
}
fun localized(locale: Locale = Locale.getDefault(), quantity: Int, vararg args: Any?): String =
MessageFormat.format(getPluralMessage(locale, quantity), *args)
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ object Testing {
MR.plurals.test_plural.desc(2),
MR.plurals.test_plural.desc(3),
MR.plurals.test_plural.desc(7),
nestedTest()
// nestedTest()
)
}

Expand All @@ -52,16 +52,16 @@ object Testing {
val simpleStringExt = MR.strings.test_simple.desc()

// create formatted string
val formattedString = StringDesc.ResourceFormatted(MR.strings.format, 9)
val formattedStringExt = MR.strings.format.format(9)
val formattedString = StringDesc.ResourceFormatted(MR.strings.format, 3)
val formattedStringExt = MR.strings.format.format(3)

// create plural
val simplePlural = StringDesc.Plural(MR.plurals.test_plural, 10)
val simplePluralExt = MR.plurals.test_plural.desc(10)
val simplePlural = StringDesc.Plural(MR.plurals.test_plural, 6)
val simplePluralExt = MR.plurals.test_plural.desc(6)

// create formatted plural
val formattedPlural = StringDesc.PluralFormatted(MR.plurals.my_plural, 10, 10)
val formattedPluralExt = MR.plurals.my_plural.format(10, 10)
val formattedPlural = StringDesc.PluralFormatted(MR.plurals.my_plural, 7, 7)
val formattedPluralExt = MR.plurals.my_plural.format(7, 7)

// raw string
val simpleRaw = StringDesc.Raw("raw string")
Expand Down

0 comments on commit 97489b6

Please sign in to comment.