Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Localization String in KMM #753

Open
mikek9084 opened this issue Aug 6, 2024 · 1 comment
Open

Using Localization String in KMM #753

mikek9084 opened this issue Aug 6, 2024 · 1 comment

Comments

@mikek9084
Copy link

thank you very much,
how can I use SharedRes. in common main

buildString {
        append("Limit)
        append(SharedRes.strings.km)
}
@mndsl
Copy link

mndsl commented Sep 1, 2024

You have to use expect/actual functions
in commonMain:

expect fun sharedString(res: StringResource, vararg args: Any): String

in androidMain:

Note

You will need context

actual fun sharedString(res: StringResource, vararg args: Any): String {
   return when (args.size) {
       0    -> res.getString(context)
       else -> ResourceFormattedStringDesc(res, args.toList()).toString(context)
   }
}

in iosMain:

actual fun sharedString(res: StringResource, vararg args: Any): String {
    return when (args.size) {
        0    -> res.desc().localized()
        else -> ResourceFormattedStringDesc(res, args.toList()).localized()
    }
}

Then in commonMain you can get any string like this:

val text = sharedString(MR.strings.my_string)
val text = sharedString(MR.strings.my_string_formatted, 10)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants