icerockdev / moko-resources

Resources access for mobile (android & ios) Kotlin Multiplatform development
https://moko.icerock.dev/
Apache License 2.0
1.12k stars 125 forks source link

Access to a string resource by ID #245

Open Tetraquark opened 3 years ago

Tetraquark commented 3 years ago

Is it possible now to access a multiplatform string resource from MR object by its ID at runtime? Let's say there is such strings.xml:

<resources>
    <string name="unknown_error">Unknown error</string>
</resources>

And in runtime need access to the string, something like this:

MR.strings.getString("unknown_error")

If there are no ways to do this, so I think then this will be a useful feature.

Alex009 commented 3 years ago

@Tetraquark hi. for now strings can't be received by id. here same issue #218. and here #233 some implmenetation, which should be completed, merged and released

Alex009 commented 3 years ago

oh, no, this pull request not about strings resources. but simmilar case. so this feature should be implemented and we can use this post as guide - https://medium.com/xorum-io/simple-string-localization-in-kmm-beaffc5e7a17

also:

Adam Powell [G] 1 month ago Don't look up Android resource identifiers by string name. It's slow enough that it shouldn't be used in anything more than debug utilities or toy apps. The comment is downplaying the inefficiency.

blonsky95 commented 1 year ago

Hi, is there an update on this issue? I can see that #218 was added to 0.18.0 release in January 2022 but this issue #245 was added to backlog in June 2022. Is it possible now to access a multiplatform string resource from MR object by its ID at runtime?

Alex009 commented 1 year ago

at now you can't access by string id. but you can do own expect/actual function. something like this:

https://github.com/icerockdev/moko-resources/blob/c7e3ec78838308aaf9079496a080a12a1adff938/resources/src/commonMain/kotlin/dev/icerock/moko/resources/ResourceContainer.kt#L9

https://github.com/icerockdev/moko-resources/blob/c7e3ec78838308aaf9079496a080a12a1adff938/resources/src/androidMain/kotlin/dev/icerock/moko/resources/ResourceContainerExt.kt#L7

https://github.com/icerockdev/moko-resources/blob/c7e3ec78838308aaf9079496a080a12a1adff938/resources/src/iosMain/kotlin/dev/icerock/moko/resources/ResourceContainerExt.kt#L7

https://github.com/icerockdev/moko-resources/blob/c7e3ec78838308aaf9079496a080a12a1adff938/resources/src/jsMain/kotlin/dev/icerock/moko/resources/ResourceContainer.kt#L11

https://github.com/icerockdev/moko-resources/blob/c7e3ec78838308aaf9079496a080a12a1adff938/resources/src/jvmMain/kotlin/dev/icerock/moko/resources/ResourceContainer.kt#L13