How to translate Enum dynamically in Spring?
New in Spring! I am trying to translate Enum as Gender dynamically, but I cannot find how to do it. Let's say I have a gender object ...
public enum Gender {FEMALE SEX, MALE; }
And I have a controller that will use it to populate the jsp like this ....
@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;
}
Now I would like to show not the uppercase MALE and FEMALE values ββas they appear from Enum, but like men and women in English or Hombre and Mujer in Spanish in my JSP :
<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>`
How can i do this? I think this is not property internationalization since jsp is dynamically charging the form: radiobuttons path = "gender" items = "$ {gender}" with MALE FEMALE and I want this to be translated before it gets into the JSP (by others in words, I can't use properties to translate both Enums at the same time, right? When I get a response on my POST controller, I want to get MALE, FEMALE, but Hombre, Mujer (translation) can be shown. Thanks
source to share
According to the docs
1.12 Serializing Enumeration Constants
Continuum constants are serialized differently than ordinary serializable or external objects. The serialized form of an enumeration constant consists solely of its name; constant field values ββare not present on the form. To serialize an enumeration constant, ObjectOutputStream writes the value returned by the enum naming method. to deserialize the enumeration constant, ObjectInputStream reads the constant name from the stream; then the deserialized constant by calling the java.lang.Enum.valueOf method, passing the enum constant and enter along with the resulting constant name as arguments. Like other serializable or external objects, enumeration constants can function as backreference targets that subsequently appear in the serialization stream.
The process by which the continuum constants are serialized cannot be customized: any class writeObject, readObject, readObjectNoData, writeReplace, and readResolve certain enumeration types are ignored during serialization and deserialization. Likewise, any serialPersistentFields or serialVersionUID declarations are also ignored - all enumeration types have a fixed serialVersionUID of 0L. Documenting the serializable fields and data for the enumeration is unnecessary since there is no change in the type of the data sent.
So, if the Enum is just serializable, you need to change the name of the Enum to the required body (all caps are just a convention). If the Enum is simply converted to a string, then the Enum acts exactly the same as a class, so you can simply override the toString method like this. (both constructor and value holder)
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;
}
}
source to share