Skip to content

Commit

Permalink
[localize] Add varargs to localize helper
Browse files Browse the repository at this point in the history
  • Loading branch information
melbic committed Apr 11, 2024
1 parent fcb4e55 commit 0426a50
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/androidMain/kotlin/ch/dreipol/dreimultiplatform/Localize.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ package ch.dreipol.dreimultiplatform

interface Localize {
fun localize(string: String): String
fun localize(string: String, vararg args: Any): String

}

0 comments on commit 0426a50

Please sign in to comment.