Recommendations for storing translatable android strings in the database
In one of my application forms, I have a spinner populated from a string array android server resource, so translation management is done by generating additional XML files in the android application. The form data is stored in mongodb via the API and I also have full control over it, if that matters.
The app will launch in 5 languages ββ(it might grow later), so I can get 5 different "versions" of the same string, but I want the strings to appear in the app in the language the app is currently in (regardless of language in which it was stored).
If possible, I would like to achieve this using native Android translation capabilities, i.e. upload 5 XML files to the application, one for each language. I also want the server code to be as deep as possible.
At the moment I can only think of storing the index in the db and then using a switch / case loop presenting the data in the application view. I don't like this at all because the possible parameters have to be hard-coded and any change would mean a new version of the application.
What's the best practice?
source to share
From your comments, I understand that your actual problem is storing the spinner value in the database.
As you say in your question, you shouldn't be storing a counter string, but an integer representing that value. And then use your counter with List <Integer> instead of List <String>.
An alternative to this int index without a value is enumeration:
public enum SpinnerValues {
EXAMPLE_1(R.string.example1_string),
EXAMPLE_2(R.string.example2_string),
EXAMPLE_3(R.string.example3_string);
private int mStringResId;
private SpinnerValues(int stringResourceId) {
mStringResId = stringResourceId;
}
public int getStringResId() {
return mStringResId;
}
}
In this case, your spinner data would be List <SpinnerValue> and you would store your list in the database with either a sequential number or a string representation of the enum (I would recommend the string value to change the order of the enum values ββwould not break your database) ...
source to share