https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
l

Lammert Westerhoff

01/20/2021, 11:42 AM
Hi everyone. Me and @Jamie Craane have created a gradle plugin to help with cross platform localization. Simply said, you create one file with all strings in all languages and the plugin will generate Kotlin native code so you can use those strings in a shared project (for example a view model). It also creates actual implementations for iOS, Android and JS. We’ve just open sourced it and are in early stages but are looking for feedback and contributions. Have a look at https://github.com/jcraane/kmm-resources/
n

nrobi

01/20/2021, 11:52 AM
At first glance seems similar to moko-resources, though moko doesn’t have (hopefully yet) a js target. Are there any significant usage and/or implementation differences?
1
l

Lammert Westerhoff

01/20/2021, 12:00 PM
Yes, although I’ve never used moko myself since we started building this around the same time moko started. But at least one small difference is that with our plugin, you can completely resolve a resource to a String within Kotlin code. moko will only get you a StringDesc and then you need to resolve it in platform specific code. And we use YAML for the localizations file which we quite liked. Some more small difference. But yes, the goal we try to achieve is pretty much the same as what moko tries to achieve.
a

alex009

01/20/2021, 12:26 PM
hi! good to see altertaive solutions for moko-resources and other opinions 🙂 how you resolve string from common code on android? :)
j

Jamie Craane

01/20/2021, 12:44 PM
Copy code
in commonMain we generate an expected function like this:

expect fun L.General.withParams(value0: String, value1: String): String

this is represented in the following way in the yaml file:

general:
  withParams:
    nl: "Een %1$s tekst met een %2$s"
    en: "A piece of %1$s with a piece of %2$s"

in androidMain we generate an actual implementation which delegates to Android string resources:

actual fun L.General.withParams(value0: String, value1: String): String = localizationContext?.getString(R.string.l_general_withParams, value0, value1) ?: ""

The string resource files are also generated from the same input file:

<string name="l.general.withParams">A piece of %1$s with a piece of %2$s</string>
Hope this answers your question.
a

alex009

01/20/2021, 1:46 PM
where you setup
Copy code
localizationContext
?
j

Jamie Craane

01/20/2021, 1:48 PM
You need to initialize that in the onCreate of your application. We may provide an Android Initializer in the future which can be registered in the Manifest which then can initialize this context.
a

alex009

01/20/2021, 1:49 PM
so you use ApplicationContext instead of acitivtyContext?
j

Jamie Craane

01/20/2021, 1:52 PM
Yes. In our app we wrap it in a ContextWrapper so a configuration with a correct locale is set. The context wrapper part is not documented yet in the plugin, this is something which is good to add.
Another approach we tried was passing the context with every method call but since not all implementations require a context we did liked the other approach more.
5 Views