gpt4 book ai didi

java - 如何在Spring中动态翻译Enum?

转载 作者:行者123 更新时间:2023-12-03 01:03:26 25 4
gpt4 key购买 nike

Spring 新品!我正在尝试动态翻译像性别这样的枚举,但我不知道如何翻译。假设我有一个性别对象...

public enum Gender { FEMALE, MALE; }

我有一个 Controller ,它将用它来填充 jsp,就像这样......

 @RequestMapping(value = "/edit-profile-about3", method = RequestMethod.GET)
public ModelAndView editProfileAbout3(ModelAndView modelAndView) {

SiteUser user = getUser();

Profile profile = profileService.getUserProfile(user);

Profile webProfile = new Profile();
webProfile.safeCopyFrom(profile);

List<Gender> gender = new ArrayList<Gender>(EnumSet.allOf(Gender.class));
modelAndView.getModel().put("gender", gender);
modelAndView.setViewName("app.editprofileabout3");
return modelAndView;
}

现在我不想像在枚举中显示的那样显示大写的 MALE 和 FEMALE 值,而是像我的 JSP 中英语中的 Male 和 Female 或西类牙语中的 Hombre 和 Mujer::

<form:form modelAttribute="profile" class="sky-form" id="sky-form4">

<dl class="dl-horizontal">
<dt>Gender:</dt>
<dd>
<form:radiobuttons path="gender" items="${gender}" />
</dd>
</dl>
<button class="btn-u btn-u-default" type="button" value="Cancel">Cancelar</button>
<button class="btn-u" type="submit" name="submit" value="Save">Guardar</button>
</form:form>`

我该怎么做?我认为这不是属性国际化,因为 jsp 动态收费 form:radiobuttons path="gender"items="${gender}"与 MALE FEMALE 并且我希望在它到达 JSP 之前对其进行翻译(换句话说) ,我不能使用属性同时翻译两个枚举,对吧?当我得到我的 POST Controller 的答案时,我想得到 MALE、FEMALE,但可能会显示 Hombre、Mujer(翻译)。谢谢

最佳答案

根据docs

1.12 Serialization of Enum Constants

Enum constants are serialized differently than ordinary serializable or externalizable objects. The serialized form of an enum constant consists solely of its name; field values of the constant are not present in the form. To serialize an enum constant, ObjectOutputStream writes the value returned by the enum constant's name method. To deserialize an enum constant, ObjectInputStream reads the constant name from the stream; the deserialized constant is then obtained by calling the java.lang.Enum.valueOf method, passing the constant's enum type along with the received constant name as arguments. Like other serializable or externalizable objects, enum constants can function as the targets of back references appearing subsequently in the serialization stream.

The process by which enum constants are serialized cannot be customized: any class-specific writeObject, readObject, readObjectNoData, writeReplace, and readResolve methods defined by enum types are ignored during serialization and deserialization. Similarly, any serialPersistentFields or serialVersionUID field declarations are also ignored--all enum types have a fixed serialVersionUID of 0L. Documenting serializable fields and data for enum types is unnecessary, since there is no variation in the type of data sent.

所以,如果 Enum刚刚被序列化,您必须将 Enum 名称更改为您想要的大小写(所有大写只是约定)。如果 Enum 刚刚转换为字符串,则 Enum 的作用就像一个类,因此您可以像这样重写 toString 方法。 (以及构造函数和值持有者)

public static enum GENDER {
MALE("Male", "Hombre"), FEMALE("Female", "Mujer");

GENDER (String en, String sp) {
en_value = en;
sp_value = sp;
}

private String en_value;
private String sp_value;

@Override
public String toString() {
return en_value;
}

public String toEnglish() {
return en_value;
}

public String toSpanish() {
return sp_value;
}
}

关于java - 如何在Spring中动态翻译Enum?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44164274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com