Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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?

share|improve this question
    
Hi! Welcome to Code Review. Please only state the purpose of your code in the title. –  Manny Meng yesterday
    
@MannyMeng thanks! Sorry, first question on this exchange site. Is that better? –  McAdam331 yesterday
1  
Yes, much better. This helps reviewers know what your code is about, as stuff like "Is this good?" titles don't really tell anything about the code. –  Manny Meng yesterday
    
That makes sense. You can tell I migrated from StackOverflow ;P –  McAdam331 yesterday
    
You can use properties files for simple configuration values too... –  Eddie B yesterday

1 Answer 1

up vote 11 down vote accepted

Don't localize your Java code. You don't want to hand over your source code to your translator, and you don't want to recompile the code to add a support for a new language. Rather, you should internationalize your code and do all the localization through resources, as recommended in the Android Developers documentation.

Furthermore, localizing strings in isolation doesn't work in the general case. For example, "monthly" may need to be translated as « mensuel », « mensuelle », « mensuels », « mensuelles », or « mensuellement », depending on context. Word order also tends to differ between languages. Therefore, the only sane approach is to translate entire phrases at the point of use.

res/values/strings.xml

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <string name="expenses">Your {0,choice, 1#weekly| 2#bi-weekly| 3#monthly| 4#quarterly| 5#annual} expenses are {1,number,currency}.</string>
</resources>

res/values-fr/strings.xml

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <string name="expenses">Vos dépenses {0,choice, 1#hebdomadaires| 2#bimensuelles| 3#mensuelles| 4#trimestrielles| 5#annuelles} sont {1,number,currency}.</string>
</resources>

Java code

import java.text.*;
…

Format expensesFmt = new MessageFormat(getResources().getString(R.string.expenses));
textView.setText(expensesFmt.format(new Object[] { MONTHLY.ordinal(), 1337 }));

… to get

Your monthly expenses are $1337.00.

or

Vos dépenses mensuelles sont 1337,00€.
share|improve this answer
    
Note that this allows me to save 156€ by simply switching the language. Thanks a bunch. :D:D:D But seriously, is there a way to check the resource for completeness, e.g., when adding BI_ANNUAL I'd like to know that the option is present in all XML files? –  maaartinus yesterday
    
Thanks a lot, this was really well explained and is a lot cleaner than my current solution. I appreciate it –  McAdam331 yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.