java - Android Studio access translated strings with variable -
i'm programming application communicates server. if there's error, server returns definded errorcode. prompt error user need translate errorcode correct language. have xml english , german translations.
i thought, refer strings this:
final string errorcodeid = perrorcodeid; string errorcode = getstring(r.string.errorcodeid)
i'm looking easy way access these translated strings, have no idea, how it.
thanks in advance :)
you should define mapping errorcode
-> r.string.errorcode*
constants. use mapping r.string.errorcode*
constant errorcode
. , use getstring string correct translation.
note android automatically select correct translation based on user locale.
so, mapping like:
map<integer, integer> errorcodemapping = immutablemap.<integer, integer>builder() .put(1001, r.string.errorcodedenied) .build();
and use:
if (errorcodemapping.get(errorcodefromserver) != null) { string description = getstring(errorcodemapping.get(errorcodefromserver)); }
Comments
Post a Comment