多个枚举的国际化(枚举值的翻译) [英] Internationalization of multiple enums (translation of enum values)

查看:2403
本文介绍了多个枚举的国际化(枚举值的翻译)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次讨论了之前和我想要的内容分享我的解决方案,并要求增强,其他方法或最佳实践。



我有几个枚举,我需要国际化(我需要将枚举值翻译成一些语言以便在jsf页面中显示它们)。例如枚举:

  public enum TransferStatus {
NOT_TRANSFERRED,
TRANSFERRED
}

翻译将是例如尚未转移 / 转移,所有好的



翻译应存储在MessageBundle(属性文件)中。我正在寻找一个简单,通用的解决方案(最好不需要在所有枚举中编写额外的代码),这在jsf方面不需要太多。当然,两个不同的枚举可能会同样的枚举值(例如,在不同枚举中具有不同含义的值 COMPLETED



我想出了一个解决方案:



(1)在属性文件中存储翻译如下:

  TransferStatus.NOT_TRANSFERRED =尚未传输
TransferStatus.TRANSFERRED =已转移,所有好
(2)创建一个帮助类,它使用枚举并生成查找键:

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

(3)将此代码添加到每个枚举: / p>

  public String getKey(){
return EnumTranslator.getMessageKey(this);
}

(4)现在,我可以访问像我这样的枚举的翻译值:

 < h:outputText value =#{enum [order.transferStatus.key]}/> 

哪个是好的,但我只是不喜欢添加相同的 getKey()方法到每个枚举。应该有更好的一点!现在轮到你了,所以,这个是完整的,随时可用的解决方案:() - )

解决方案

感谢@Joop Eggen)



制作课

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

使其成为自定义EL函数

 <?xml version =1.0encoding =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>

将taglib添加到您的web.xml

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

拥有属性文件enum_en.properties和enum_yourlanguage.properties这样的

  TransferStatus.NOT_TRANSFERRED =未传输
TransferStatus.TRANSFERRED =传输

将属性文件作为资源包添加到faces-config.xml

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

将自定义taglib添加到您的xhtml文件

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

And - voilà - 您现在可以访问jsf中的已翻译的枚举值:

 < h:outputText value =#{enum [l:xlate(order.transferStatus)]}/> ; 


Again something that has been discussed before and where I wanted to share "my" solution and ask for enhancements, other approaches or best practices.

I have several enums where I need internationalization (I need the enum values translated into some languages in order to display them in a jsf page). Examle enum:

public enum TransferStatus {
  NOT_TRANSFERRED,
  TRANSFERRED
}

Translation would be for example Not yet transferred / Transferred, all good

The translation should be stored in a MessageBundle (properties files). I was searching for an easy, generic solution (best would be without the need of writing extra code in all the enums) that does not need much on the jsf side. Just to mention it, of course it it possible that two different enums shae the same enum value (e.g. values like COMPLETED that have a different meaning in different enums).

The solution I came up with:

(1) Store translations in the properties file like this:

TransferStatus.NOT_TRANSFERRED = Not yet transferred
TransferStatus.TRANSFERRED = Transferred, all good

(2) Make a helper class that takes an enum and generates the lookup key:

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

(3) Add this code to every enum:

public String getKey() {
  return EnumTranslator.getMessageKey(this);
}

(4) Now, I can access the translated values of my enums like this:

<h:outputText value="#{enum[order.transferStatus.key]}" />

Which is okay, but what I just don't like is adding the same getKey() method to every enum. There should be something better that that! Now it's your turn, SO :-)

解决方案

Ok, now this is the complete and ready-to-use solution: (thanks to @Joop Eggen)

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)]}" />

这篇关于多个枚举的国际化(枚举值的翻译)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆