Localizing enum values in resource bundle

后端 未结 5 738
借酒劲吻你
借酒劲吻你 2020-11-27 05:00

I have a problem with i18n enums in my JSF application. When I started, I had enums with the text defined inside. But now, I have keys tied to message bundles in the enum.

5条回答
  •  青春惊慌失措
    2020-11-27 05:35

    The value which is passed through the converter is not the option label as you seem to expect, but the option value. The best practice is to not do this in the model side, but in the view side, because the model shouldn't need to be i18n aware.

    As to the approach, you're basically unnecessarily overcomplicating things. Since JSF 1.2 there's a builtin EnumConverter which will kick in automatically and since JSF 2.0 you can iterate over a generic array or List in f:selectItems by the new var attribute without the need to duplicate the values over a List in the model.

    Here's how the bean can look like:

    public class Bean {
        private OrderStatus orderStatus;
        private OrderStatus[] orderStatuses = OrderStatus.values();
    
        // ...
    }
    

    And here's how the view can look like (assuming that msg refers to the as you've definied in in faces-config.xml):

    
        
    
    

    That's all.


    Unrelated to the problem, you've typos in the enum name and message keys, it should be:

    PENDING("enum.orderstatus.pending"),
    CANCELLED("enum.orderstatus.cancelled");
    

    And, more clean would be to keep the bundle keys out the enum and use enum itself as part of bundle key. E.g.

    PENDING,
    CANCELLED;
    
    
        
    
    
    enum.orderstatus.PENDING = Pending
    enum.orderstatus.CANCELLED = Cancelled
    

提交回复
热议问题