HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Localizing an enum in Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
enumlocalizingjava

Problem

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, bu

Solution

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


Your {0,choice, 1#weekly| 2#bi-weekly| 3#monthly| 4#quarterly| 5#annual} expenses are {1,number,currency}.



res/values-fr/strings.xml


Vos dépenses {0,choice, 1#hebdomadaires| 2#bimensuelles| 3#mensuelles| 4#trimestrielles| 5#annuelles} sont {1,number,currency}.



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€.

Code Snippets

import java.text.*;
…

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

Context

StackExchange Code Review Q#90995, answer score: 17

Revisions (0)

No revisions yet.