Internationalization with Enums Labels

Asked

Viewed 169 times

0

Hello I’m trying to do internationalization with Labels from an Enum:

public enum WeekDay {
  MONDAY("msg.week_monday", "mon"), 
  TUESDAY("msg.week_tuesday", "tue"), 
  WEDNESDAY("msg.week_wednesday", "wed"),
  THURSDAY("msg.week_thursday", "thu"), 
  FRIDAY("msg.week_friday", "fri"), 
  SATURDAY("msg.week_saturday", "sat"),
  SUNDAY("msg.week_sunday", "sun");

    private String label;
    private String value;

    private WeekDay(String label, String value) {
        this.label = label;
        this.value = value;
    }

    public String getLabel() {
        return label;
    }

    public String getValue() {
        return this.value;

    }
}

and call on the page like this :

  <f:selectItems value="#{myMB.weekDays}"
                                    var="dayWeek" itemValue="#{dayWeek}"
                                    itemLabel="#{dayWeek.label}" />

But the result is the literals of the Abels and not their corresponding in my msg.properties file.:

week_monday=Segunda-feira
week_tuesday=Ter\u00E7a-feira
week_wednesday=Quarta-feira
week_thursday=Quinta-feira
week_friday=Sexta-feira
week_saturday=S\u00E1bado
week_sunday=Domingo

When I call it that itemLabel="#{msg.week_monday}" works, does anyone know how to do it? I’m having the same problem trying to translate fields coming from the bank.

1 answer

1

Do so inside the getLabel

FacesContext context = FacesContext.getCurrentInstance();              
FacesContext.getCurrentInstance().getApplication().evaluateExpressionGet(context,  label, String.class);

This code will make the bean value msg be processed within the Enum.

  • Vlw Renato, I managed to simply change itemLabel="#{msg[dayWeek.label]} for itemLabel="#{msg[dayWeek.label]}"

  • I say: Vlw Renato, I managed to simply change itemLabel="#{dayWeek.label} for itemLabel="#{msg[dayWeek.label]}"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.