从 Chrono Time unit 获取复数和单数

Get plural and single from Chrono Time unit

我需要在 Android“1 分钟前”和“2 分钟前”中使用复数显示。问题是我有一张带有后缀的时间单位地图。

 val suffixes = mapOf(
        ChronoUnit.WEEKS to context.getString(R.string.time_units_week),
        ChronoUnit.DAYS to context.getString(R.string.time_units_day),
        ChronoUnit.HOURS to context.getString(R.string.time_units_hour),
        ChronoUnit.MINUTES to context.getString(R.string.time_units_minute),
        ChronoUnit.SECONDS to context.getString(R.string.time_units_second)
)

然后我就这样使用它

fun timeLabel(now: ZonedDateTime): String = temporalUnits
        .map { Pair(it, targetTime.until(now, it)) }
        .find { it.second > 0 }
        ?.let { "${it.second} ${suffixes[it.first]}" }

如何转换它以便将时间值传递给地图?

找到了,自己提供资源即可

private val suffixes = mapOf(
        ChronoUnit.WEEKS to R.plurals.time_unit_week,
        ChronoUnit.DAYS to R.plurals.time_unit_day,
        ChronoUnit.HOURS to R.plurals.time_unit_hour,
        ChronoUnit.MINUTES to R.plurals.time_unit_minute,
        ChronoUnit.SECONDS to R.plurals.time_unit_second
)

fun timeLabel(now: ZonedDateTime): String = temporalUnits
        .map { Pair(it, targetTime.until(now, it)) }
        .find { it.second > 0 }
        ?.let {
            val pluralResources = suffixes[it.first]!!
            resources.getQuantityString(pluralResources, it.second.toInt(), it.second.toInt())
        }
        ?: context.getString(R.string.time_unit_now)