From 0426a5065a54f967665e8ee077da600f95615f3b Mon Sep 17 00:00:00 2001 From: Samuel Bichsel Date: Thu, 11 Apr 2024 08:36:49 +0200 Subject: [PATCH] [localize] Add varargs to localize helper --- .../ch/dreipol/dreimultiplatform/Localize.kt | 17 +++++++++++++---- .../ch/dreipol/dreimultiplatform/Localize.kt | 2 ++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/androidMain/kotlin/ch/dreipol/dreimultiplatform/Localize.kt b/src/androidMain/kotlin/ch/dreipol/dreimultiplatform/Localize.kt index 1072f35..df55c77 100644 --- a/src/androidMain/kotlin/ch/dreipol/dreimultiplatform/Localize.kt +++ b/src/androidMain/kotlin/ch/dreipol/dreimultiplatform/Localize.kt @@ -3,12 +3,21 @@ package ch.dreipol.dreimultiplatform import android.content.Context fun Context.getString(identifier: String): String { - val resourceId = resources.getIdentifier(identifier, "string", packageName) + val resourceId = stringId(identifier) return resources.getString(resourceId) } +private fun Context.stringId(identifier: String): Int { + return resources.getIdentifier(identifier, "string", packageName) +} + +fun Context.getString(identifier: String, vararg args: Any): String { + val stringId = stringId(identifier) + return resources.getString(stringId, *args) +} + class Localizer(private val context: Context) : Localize { - override fun localize(string: String): String { - return context.getString(string) - } + override fun localize(string: String): String = context.getString(string) + + override fun localize(string: String, vararg args: Any): String = context.getString(string, *args) } \ No newline at end of file diff --git a/src/commonMain/kotlin/ch/dreipol/dreimultiplatform/Localize.kt b/src/commonMain/kotlin/ch/dreipol/dreimultiplatform/Localize.kt index ba0744c..325d9b9 100644 --- a/src/commonMain/kotlin/ch/dreipol/dreimultiplatform/Localize.kt +++ b/src/commonMain/kotlin/ch/dreipol/dreimultiplatform/Localize.kt @@ -2,4 +2,6 @@ package ch.dreipol.dreimultiplatform interface Localize { fun localize(string: String): String + fun localize(string: String, vararg args: Any): String + } \ No newline at end of file