Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: i18n with enum

  1. #1

    Default i18n with enum

    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!

  2. #2
    Join Date
    Mar 2008
    Location
    Sydney, AU
    Posts
    974

    Default

    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

  3. #3

    Default

    @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...

  4. #4
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    667

    Lightbulb How about this...

    What would be nice is if the Spring tag library supported this kind of thing (the new bit is in blue):

    Code:
    ...
    <form:options itemValue="id" items="${things} itemCode="messageCode" />
    ...
    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.
    Andrew Swan
    "Now is the EJB of our discontent made glorious Spring"

  5. #5

    Default

    "[...] is direct (untranslated) reading of item properties via the itemLabel attribute."
    And that means what?

  6. #6
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    667

    Post

    Quote Originally Posted by eventhorizon42 View Post
    "[...] is direct (untranslated) reading of item properties via the itemLabel attribute."
    And that means what?
    What I meant is that with the current
    Code:
    <spring:options>
    tag, if you have an enum like this:

    Code:
    public enum Size {
        LARGE,
        MEDIUM,
        SMALL;
    
        public String getNiceName() {
            return WordUtils.capitalizeFully(name());  // hard-coded to English!
        }
    }
    Then all you can do is display a given property or method like this:

    Code:
    <spring:options items="${myEnumValues} itemValue="name" itemLabel="niceName"/>
    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.

    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"

  7. #7
    Join Date
    Mar 2008
    Location
    Sydney, AU
    Posts
    974

    Default

    I have not tried this but you should be able to populate the itemLabel attribute with localized spring:message variables:

    Code:
    <spring:message code="label.person.enum.${enumItem.name}" var="_label"/>
    <spring:option itemLabel="${_label}" .../>
    Just an idea.

    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

  8. #8
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    667

    Post

    Quote Originally Posted by Stefan Schmidt View Post
    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.
    Logged (in 2006!) as SPR-2659.

    Vote, vote, vote!
    Andrew Swan
    "Now is the EJB of our discontent made glorious Spring"

  9. #9
    Join Date
    Oct 2010
    Location
    Almere, The Netherlands
    Posts
    32

    Default i18n with enums and SpringRoo

    This is how I just added i18n support to enums and Roo.

    My enum class:

    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;
        }
        

    Extend the controller with a converter and implement ApplicationContextAware:

    PHP Code:
    Converter<UserRoleStringgetUserRoleConverter() {
                return new 
    Converter<UserRoleString>() {
                    public 
    String convert(UserRole userRole) {
                        return 
    applicationContext.getMessage(userRole.getMessageKey(),nullLocaleContextHolder.getLocale());
                    }
                };
            } 
    And then just add your messages to the application properties for each language.

    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 
    The output in the view jspx is the same as before:
    PHP Code:
    <field:select field="roles" id="c_com_packagename_domain_admin_ApplicationUser_roles" items="${userroles}multiple="true" path="/roles" z="xxx"/> 
    Following the Roo naming convention one could also leave the message key out of the enum by using this in the converter:

    PHP Code:
    String enumKeyName "label_"UserRole.class.getName().replace('.''_') + "_" userRole.toString();
    return 
    applicationContext.getMessage(enumKeyName.toLowerCase(),nullLocaleContextHolder.getLocale()); 
    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)

  10. #10
    Join Date
    Dec 2010
    Posts
    1

    Default Opened a bug report

    I opened a bug report for this issue:
    ROO-1827

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •