2017-06-12 147 views
0

我需要在Android「1分鐘前」和「2分鐘前」使用複數顯示。問題是我有一個帶有後綴的時間單位的地圖。從Chrono獲取複數和單個時間單位

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]}" } 

我如何可以轉換,所以我可以打發時間地圖的價值?

+0

在只有英語嗎?如果表單是內置的,我會感到驚訝,但添加或刪除複數可適用於所有五個單詞(不適用於所有英文單詞,但我收集您並不需要)。或者你可以使用兩張地圖,一張用於單數形式,另一張用於複數形式。 –

回答

0

發現了它,你可以簡單地提供資源本身

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)