I'm finding the accepted answer and discussion for this SO question getString Outside of a Context or Activity unclear.
I'm new to Android and I'm trying to understand how I can reference my resource strings in a model class so that I can properly support localization.
Specifically, my model has a Location property and I would like to be able to return a string for the compass ordinal for the bearing. Because compass ordinals like "North" need to be localized I'm trying to store them in my strings.xml.
I think I understand that I need the application context to get to the resources object but I'm wondering if this is possible without having to pass the context in. Storing a UI context in a model seems like a violation of MVC.
To accomplish this I wanted to include a method in my model like this. The first if shows how I'm trying to use the strings.xml entry.
public String compassOrdinalForBearing(float bearing) {
assert bearing >= 0.0 && bearing <= 360.0;
if ((bearing > 336.5) && (bearing <= 360.0))
//Problem here
return Context.getResources().getString(R.string.compass_ordinal_north);
else if ((bearing >= 0) && (bearing <= 22.5))
return "North";
else if ((bearing > 22.5) && (bearing <= 67.5))
return "Northeast";
else if ((bearing > 67.5) && (bearing <= 112.5))
return "East";
else if ((bearing > 112.5) && (bearing <= 157.5))
return "Southeast";
else if ((bearing > 157.5) && (bearing <= 202.5))
return "South";
else if ((bearing > 202.5) && (bearing <= 247.5))
return "Southwest";
else if ((bearing > 247.5) && (bearing <= 292.5))
return "West";
else if ((bearing > 292.5) && (bearing <= 337.5))
return "Northwest";
else
assert false;
return null;
}