Localizing enum values in resource bundle

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

    
    
    http://example.com/enumi18n
    
        xlate
        your.package.EnumTranslator
        String getMessageKey(java.lang.Enum)
    
    
    

    Add the taglib to your web.xml

    
        javax.faces.FACELETS_LIBRARIES
        /WEB-INF/enumi18n.taglib.xml
    
    

    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

        
            kk.os.obj.jsf.i18n.enum
            enum
        
    

    Add the custom taglib to your xhtml files

    
    

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

    
    

提交回复
热议问题