Localizing enum values in resource bundle

后端 未结 5 707
借酒劲吻你
借酒劲吻你 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:16

    I have posted my solution here: Internationalization of multiple enums (translation of enum values) - but still hoping for further enhancement.

    EDIT: with the help of @Joop Eggen, we have come up with a really cool solution:

    EDIT again: complete and ready-to-use solution:

    Make a class

    public final class EnumTranslator {
      public static String getMessageKey(Enum<?> e) {
        return e.getClass().getSimpleName() + '.' + e.name();
      }
    }
    

    Make it a custom EL function

    <?xml version="1.0" encoding="UTF-8"?>
    <facelet-taglib 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
    version="2.0">
    <namespace>http://example.com/enumi18n</namespace>
    <function>
        <function-name>xlate</function-name>
        <function-class>your.package.EnumTranslator</function-class>
        <function-signature>String getMessageKey(java.lang.Enum)</function-signature>
    </function>
    </facelet-taglib>
    

    Add the taglib to your web.xml

    <context-param>
        <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
        <param-value>/WEB-INF/enumi18n.taglib.xml</param-value>
    </context-param>
    

    Have properties files enum_en.properties and enum_yourlanguage.properties like this

    TransferStatus.NOT_TRANSFERRED = Not transferred
    TransferStatus.TRANSFERRED = Transferred
    

    Add the properties files as resource bundles to your faces-config.xml

        <resource-bundle>
            <base-name>kk.os.obj.jsf.i18n.enum</base-name>
            <var>enum</var>
        </resource-bundle>
    

    Add the custom taglib to your xhtml files

    <html ... xmlns:l="http://example.com/enumi18n">
    

    And - voilà - you can now access the translated enum values in jsf:

    <h:outputText value="#{enum[l:xlate(order.transferStatus)]}" />
    
    0 讨论(0)
  • 2020-11-27 05:17

    In case anyone is looking for a simple utility library to handle enum internationalization, please take a look at https://github.com/thiagowolff/litefaces-enum-i18n

    The artifact is also available in Maven Central:

    <dependency>
        <groupId>br.com.litecode</groupId>
        <artifactId>litefaces-enum-i18n</artifactId>
        <version>1.0.1</version>
    </dependency>
    

    Basically, you just need to add the artifact to your project and define the enum respective keys following the described enum naming conventions. The translations (and also CSS class names) can be retrieved using the provided EL functions.

    0 讨论(0)
  • 2020-11-27 05:18

    Well, enum is just another class. There is nothing stopping you from adding parsing and to-string conversion methods that will parse and output locale-sensitive messages.

    Maybe it violates Single Responsible Principle (does it?), but I believe making enum responsible for parsing and returning locale-aware values is the right thing to do.

    Just add two methods like this:

    public String toString(FacesContext context) {
       // need to modify the method   
       FacesUtil.getMessageValue(context, name);
    }
    
    public OrderStatus parse(FacesContext context, String theName) {
      for (OrderStatus value : values()) {
        if (value.toString(context).equals(theName) {
          return value;
        }
      }
      // think of something better
      return null;
    }
    

    I hope I got the code right, as I am not checking it with IDE now... Is this what you were looking for?

    0 讨论(0)
  • 2020-11-27 05:25

    I calculate the message key in the enum like as shown below; so no need to maintain the keys with additional attributes on the enum

    public String getMessageKey() {
        return String.format("enum_%s_%s", this.getClass().getSimpleName(),
                this.name());
    }
    

    Then I use it like this

         <p:selectOneMenu id="type"
            value="#{xyzBean.type}" required="true">
                <f:selectItems
                    value="#{xyzBean.possibleTypes}"
                    var="type" itemLabel="#{msg[type.messageKey]}">
                </f:selectItems>
         </p:selectOneMenu>
    

    with having configured a org.springframework.context.support.ReloadableResourceBundleMessageSource in the app context

    <bean id="msg"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/resources/locale/messages" />
        <property name="useCodeAsDefaultMessage" value="true" />
        <property name="cacheSeconds" value="1" />
    </bean>
    
    0 讨论(0)
  • 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<SelectItem> 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 <var> as you've definied in <resource-bundle> in faces-config.xml):

    <h:selectOneMenu value="#{bean.orderStatus}">
        <f:selectItems value="#{bean.orderStatuses}" var="orderStatus" 
            itemValue="#{orderStatus}" itemLabel="#{msg[orderStatus.name]}" />
    </h:selectOneMenu>
    

    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;
    
    <h:selectOneMenu value="#{bean.orderStatus}">
        <f:selectItems value="#{bean.orderStatuses}" var="orderStatus" 
            itemValue="#{orderStatus}" itemLabel="#{msg['enum.orderstatus.' += orderStatus]}" />
    </h:selectOneMenu>
    
    enum.orderstatus.PENDING = Pending
    enum.orderstatus.CANCELLED = Cancelled
    
    0 讨论(0)
提交回复
热议问题