Hi all, I’ve started integrating Moko Resources in...
# moko
s
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
now all access to strings only by property in
MR
class. what usecase can't be implemented with this way?
s
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
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
ok ok I’ll try that 🙂