Krumelur
Krumelur

Reputation: 33048

How to get locale string in Android dynamically by name?

I'm using a library that offers a localization interface ILocalize which has a method

string GetLocale(string constant);

On Android I am working with a standard resource file. If the constant is "TestString" and my resource file's string would be accessed r.strings.TestString - but that obviously only works hardcoded, how can I dynamically get to the string from the constant (map from a string constant to an Android string resource ID)?

Upvotes: 0

Views: 438

Answers (1)

Krumelur
Krumelur

Reputation: 33048

Sometimes it's so easy to miss it:

int resId = context.Resources.GetIdentifier(constant, "string", context.PackageName);
if(resId == 0)
{
    return constant;
}
return context.GetString(resId);

Upvotes: 2

Related Questions