Skip to content

Commit 409a9d9

Browse files
committed
translate helper
1 parent 9309ba2 commit 409a9d9

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/commonMain/kotlin/spp.protocol/utils/TimeUtils.kt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,35 @@
1717
*/
1818
package spp.protocol.utils
1919

20-
fun Int.toPrettyDuration(): String {
20+
import kotlin.jvm.JvmOverloads
21+
22+
fun Int.toPrettyDuration(translate: (String)->String = { it }): String {
2123
val days = this / 86400000.0
2224
if (days > 1) {
23-
return "${days.toInt()}dys"
25+
return "${days.toInt()}" + translate("dys")
2426
}
2527
val hours = this / 3600000.0
2628
if (hours > 1) {
27-
return "${hours.toInt()}hrs"
29+
return "${hours.toInt()}" + translate("hrs")
2830
}
2931
val minutes = this / 60000.0
3032
if (minutes > 1) {
31-
return "${minutes.toInt()}mins"
33+
return "${minutes.toInt()}" + translate("mins")
3234
}
3335
val seconds = this / 1000.0
3436
if (seconds > 1) {
35-
return "${seconds.toInt()}secs"
37+
return "${seconds.toInt()}" + translate("secs")
3638
}
37-
return "${this}ms"
39+
return "$this" + translate("ms")
3840
}
3941

40-
fun Double.fromPerSecondToPrettyFrequency(): String {
42+
@JvmOverloads
43+
fun Double.fromPerSecondToPrettyFrequency(translate: (String)->String = { it }): String {
4144
return when {
42-
this > 1000000.0 -> "${this / 1000000.0.toInt()}M/sec"
43-
this > 1000.0 -> "${this / 1000.0.toInt()}K/sec"
44-
this > 1.0 -> "${this.toInt()}/sec"
45-
else -> "${(this * 60.0).toInt()}/min"
45+
this > 1000000.0 -> "${this / 1000000.0.toInt()}M/" + translate("sec")
46+
this > 1000.0 -> "${this / 1000.0.toInt()}K/" + translate("sec")
47+
this > 1.0 -> "${this.toInt()}/" + translate("sec")
48+
else -> "${(this * 60.0).toInt()}/" + translate.invoke("min")
4649
}
4750
}
4851

0 commit comments

Comments
 (0)