Kmp Strings
abstract class KmpStrings(replacementPattern: Regex = Regex("%s"), useFallbackLocale: Boolean = true)
Kotlin Multiplatform localized strings
Runtime replacements are supported with the replacementPattern
parameter. By default, it is set to '%s'. Replacements are not formatted in any way.
If a key is not found in the current locale, the first provided locale will be used as the fallback unless useFallbackLocale is set to false. An empty string will be returned if no string matches the key.
Note: The string keys need to be declared before the locales due to the way KmpStrings is set up. If you would like to declare the locales you may declare your locales using the lazy builder:
override val locales by lazy {
mapOf(
"en" to StringsEnglish,
"es" to StringsSpanish,*
)
}
Content copied to clipboard
Example Usage:
object Strings: KmpStrings() {
val hello = kmpStringKey()
val myName = kmpStringKey()
override val locales = mapOf(
"en" to StringsEnglish,
"es" to StringsSpanish,
)
}
private object StringsEnglish: KmpStringSet() {
override val strings: Map<KmpStringKey, String> = mapOf(
Strings.hello to "Hello world!",
Strings.myName to "My name is %s",
}
}
private object StringsSpanish: KmpStringSet() {
override val strings: Map<KmpStringKey, String> = mapOf(
Strings.hello to "Hola Mundo!",
Strings.myName to "Mi nombre es %s",
}
}
@Composable
fun Test() {
Text(rememberKmpString(Strings.hello))
Text(rememberKmpString(Strings.myName, "Tom"))
}
Content copied to clipboard