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

Ashu

02/24/2024, 4:42 PM
Hi guys I wanted to ask an old question, what is the best way to achieve string localisation when targeting android, iOS & desktop platforms? Do I need to use actual/expect or any 3rd party library like MokoResources, or can I have something only in the shared module? And can it not be achieved directly in Kotlin by creating and interface to define string properties and the have multiple classes inherit from it? Like:
interface AppStrings
class EnAppStrings: AppStrings
class HiAppStrings: AppStrings
It it a bad idea?
h

Harold Martin

02/24/2024, 5:47 PM
The two basic approaches to string localization are to either embed the strings as code (like what you've shown) or to create a wrapper that is able to retrieve the strings from each platform natively. Both have tradeoffs (of course!) and various examples available. Either way make sure to consider things like value interpolation and plurals handling.
a

Ashu

02/25/2024, 6:25 AM
@Harold Martin Thanks for your response. I haven't seen examples or use of embedding string as code. As such my concern with using it. Can you point me to a resource where I can see its example and downsides. It would really help.
c

Chanjung Kim

02/25/2024, 9:34 AM
You should use
Locale
on Android and JVM and
NSLocale
on iOS. I don't think manually handling localization is a good idea... I don't know the implementation details of MR, but they also should be using these types.
a

Ashu

02/25/2024, 2:27 PM
@Chanjung Kim no the same locale classes can be used which you mentioned but I am talking about storing actual strings in kotlin classes and fetching them based on Locale. Instead of keeping e.g. for android in strings.xml
h

Harold Martin

02/25/2024, 10:27 PM
@Ashu take a look at this approach, it's the best way to do in strings in code that I'm aware of (and Mirego is one of the best KM companies) https://github.com/mirego/trikot/tree/master/trikot-kword
c

Chanjung Kim

02/25/2024, 11:12 PM
@Ashu You can, but I'm just recommending storing them using features like strings.xml because it also handles something like plurals, numbers, or grammatical genders, which can't be implemented without the knowledge of the languages. You can then generate OS-specific resources from a single XML or JSON resource in Gradle, and that's what MR is doing right now.
a

Ashu

02/27/2024, 7:26 PM
@Harold Martin Thanks for providing that reference. I am reading up on it.
@Chanjung Kim I have a working solution with platform dependent strings in android and ios, but 1st I am having trouble with desktop platform completely in dark about that, and 2nd I am not sure I want to manage resources on 3 different platforms each time. But I will have to look into solutions which generate OS-specific resources from single XML or JSON you mentioned.
5 Views