https://kotlinlang.org logo
#moko
Title
# moko
s

sendoav

01/27/2020, 2:57 PM
Hi all, I’ve started integrating Moko Resources in our mpp project and just wanted to know if there is any way to access a particular string providing its key. Imagine we declare the string action_go=“GO” in the strings.xml, we would like to access the resource providing the key “action_go”
a

alex009

01/28/2020, 3:46 AM
now all access to strings only by property in
MR
class. what usecase can't be implemented with this way?
s

sendoav

01/28/2020, 6:44 AM
I have a bunch of strings with keys login_error_1, login_error_2, login_error_3. Depending on the type of error we show a different message. I just wonder if it was possible to access the string using “login_error_“+errorId
a

alex009

01/28/2020, 8:26 AM
on android side resource can't be accessed by string key. android generate own R class with all strings and require to pass int resourceId for getting string. so string key in common code API can't be done. to support android & ios you can use function like:
Copy code
fun MR.strings.getLoginError(errorId: Int): StringResource = when(errorId) {
    1 -> login_error_1
    2 -> login_error_2
    else throw IllegalArgumentException("unknown $errorId")
}
s

sendoav

01/28/2020, 10:12 AM
ok ok I’ll try that 🙂
5 Views