Hi. My next problem:
I have a field of type enum and the selectbox shows the technical values.
I would like to translate these values somehow automatically.
Any ideas?
THX!
Hi. My next problem:
I have a field of type enum and the selectbox shows the technical values.
I would like to translate these values somehow automatically.
Any ideas?
THX!
You mean you want Roo to translate arbitrary text for you? This functionality is currently not implemented but maybe you want to develop a nice little plugin which provides this service? You could for example interface with the Google translate web service to make it happen...
Stefan Schmidt
Software Engineer, Spring Roo
SpringSource - a division of VMware
twitter @schmidtstefan
@Stefan Schmidt: No :-) That's not what I want.
I would modify a properties file or so. I tried that already but it did not work. Maybe I did it wrong...
What would be nice is if the Spring tag library supported this kind of thing (the new bit is in blue):
In which case Spring would use the value of each item's messageCode property to look up the relevant translation from the existing MessageSource. All that's supported at the moment is direct (untranslated) reading of item properties via the itemLabel attribute.Code:... <form:options itemValue="id" items="${things} itemCode="messageCode" /> ...
Andrew Swan
"Now is the EJB of our discontent made glorious Spring"
"[...] is direct (untranslated) reading of item properties via the itemLabel attribute."
And that means what?![]()
What I meant is that with the currenttag, if you have an enum like this:Code:<spring:options>
Then all you can do is display a given property or method like this:Code:public enum Size { LARGE, MEDIUM, SMALL; public String getNiceName() { return WordUtils.capitalizeFully(name()); // hard-coded to English! } }
The values displayed in the drop-down or list box will be untranslated (i.e. not localised according to the user's locale). What I would like to be able to do is ask each enum constant for a message code (as in my "How about this" example) that you could then pass to the MessageSource to get its translation into the user's own language, as you can do for all other text in a Spring app. This is actually a shortcoming of Spring's JSP tag library, nothing to do with Roo.Code:<spring:options items="${myEnumValues} itemValue="name" itemLabel="niceName"/>
Last edited by Andrew Swan; Feb 15th, 2010 at 06:54 PM. Reason: Corrected itemKey to itemValue
Andrew Swan
"Now is the EJB of our discontent made glorious Spring"
I have not tried this but you should be able to populate the itemLabel attribute with localized spring:message variables:
Just an idea.Code:<spring:message code="label.person.enum.${enumItem.name}" var="_label"/> <spring:option itemLabel="${_label}" .../>
It might be useful if someone could open this as a Jira ticket against Spring Framework as I think we can improve Spring MVC here.
Cheers,
Stefan
Stefan Schmidt
Software Engineer, Spring Roo
SpringSource - a division of VMware
twitter @schmidtstefan
Logged (in 2006!) as SPR-2659.
Vote, vote, vote!![]()
Andrew Swan
"Now is the EJB of our discontent made glorious Spring"
This is how I just added i18n support to enums and Roo.
My enum class:
Extend the controller with a converter and implement ApplicationContextAware:PHP Code:public enum UserRole {
ROLE_ADMIN("label_com_packagename_reference_userrole_admin"),
ROLE_SUPPORT("label_com_packagename_reference_userrole_support"),
ROLE_CLIENT_ADMIN("label_com_packagename_reference_userrole_client_admin"),
ROLE_STD_USER("label_com_packagename_reference_userrole_client_std_user");
private String messageKey;
private UserRole(String name) {
this.messageKey = name;
}
public String getMessageKey() {
return messageKey;
}
}
And then just add your messages to the application properties for each language.PHP Code:Converter<UserRole, String> getUserRoleConverter() {
return new Converter<UserRole, String>() {
public String convert(UserRole userRole) {
return applicationContext.getMessage(userRole.getMessageKey(),null, LocaleContextHolder.getLocale());
}
};
}
The output in the view jspx is the same as before:PHP Code:label_com_tripolis_dynapix_reference_userrole_admin=Super admin
label_com_packagename_reference_userrole_support=Support
label_com_packagename_reference_userrole_client_admin=Administrator
label_com_packagename_reference_userrole_client_std_user=Standard user
Following the Roo naming convention one could also leave the message key out of the enum by using this in the converter:PHP Code:<field:select field="roles" id="c_com_packagename_domain_admin_ApplicationUser_roles" items="${userroles}" multiple="true" path="/roles" z="xxx"/>
Resulting in 'label_packagename_reference_userrole_role_admin' as the key for the first element. (there is probably a method already somewhere that does this kind of name generation for Roo)PHP Code:String enumKeyName = "label_"+ UserRole.class.getName().replace('.', '_') + "_" + userRole.toString();
return applicationContext.getMessage(enumKeyName.toLowerCase(),null, LocaleContextHolder.getLocale());
I opened a bug report for this issue:
ROO-1827