I am currently using an enum in my Android application to display time periods:
- Weekly
- Bi-Weekly
- Monthly
- Quarterly
- Yearly
I created the enum like this, because I wanted to know the description, and (roughly) number of days:
public enum SpendingPeriods {
WEEKLY("Weekly", 7),
BIWEEKLY("Bi-Weekly", 14),
MONTHLY("Monthly", 30),
QUARTERLY("Quarterly", 120),
YEARLY("Yearly", 365);
private final String description;
private final int numDays;
SpendingPeriods(String description, int numDays){
this.description = description;
this.numDays = numDays;
}
public String getDescription(){
return description;
}
public int getNumDays(){
return numDays;
}
}
Now, I would like to localize the enum, and so far my only solution has been to remove the description from the constructor, and check Locale and switch based on number of days:
SpendingPeriods(int numDays){
this.numDays = numDays;
if(Locale.getDefault().getLanguage().equals("fr")){
switch(numDays){
case 7:
this.description = "Hebdomadaire";
break;
case 14:
this.description="Bimensuel";
break;
case 30:
this.description="Mensuel";
break;
// Others
}
} else{
// Fall back to English for unsupported languages
switch(numDays){
case 7:
this.description = "Weekly";
break;
case 14:
this.description = "Bi-Weekly";
break;
case 30:
this.description = "Monthly";
break;
// Others
}
}
}
I was hoping there would be a way to do this using a string-array in an XML file, but I can't seem to find anything like that. Is this okay practice? Is there a better way?