Announcement Announcement Module
Collapse
No announcement yet.
i18n with enum Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


    • #3
      @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...

      Comment


      • #4
        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.

        Comment


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

          Comment


          • #6
            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, 06:54 PM. Reason: Corrected itemKey to itemValue

            Comment


            • #7
              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

              Comment


              • #8
                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!

                Comment


                • #9
                  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)

                  Comment


                  • #10
                    Opened a bug report

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

                    Comment


                    • #11
                      Alternate Solution

                      I found another solution which allows the conversion to happen in the enum, as I use them from a number of controllers, and have a lot of enums too so the converter method isn't practical.

                      I defined a bean in webmvc-config.xml

                      Code:
                      <bean id="contextApplicationContextProvider" class="ApplicationContextProvider"></bean>
                      To inject the application context into this class which sets it in AppContext

                      Code:
                      public class ApplicationContextProvider implements ApplicationContextAware { 
                          public void setApplicationContext(ApplicationContext ctx) throws BeansException { 
                      
                              // Wiring the ApplicationContext into a static method 
                              AppContext.setApplicationContext(ctx); 
                          } 
                      }
                      Code:
                      public class AppContext { 
                      
                          private static ApplicationContext ctx; 
                      
                          /**
                           * Injected from the class "ApplicationContextProvider" which is automatically
                           * loaded during Spring-Initialization.
                           */ 
                      
                          public static void setApplicationContext(ApplicationContext applicationContext) { 
                              ctx = applicationContext; 
                          } 
                      
                      
                          /**
                           * Get access to the Spring ApplicationContext from everywhere in your Application
                           *
                           * @return
                           */ 
                      
                          public static ApplicationContext getApplicationContext() { 
                              return ctx; 
                          } 
                      }
                      Then in the enum:

                      Code:
                      public enum MyEnum {
                      	VALUEONE("valueOne.text"),
                      	VALUETWO("valueTwo.text");
                      	
                      	MyEnum(String s) { label = s; }
                          
                          String label;
                          
                          public String getLabel() { 
                          	Locale locale = LocaleContextHolder.getLocale();
                          	return AppContext.getApplicationContext().getMessage(label, null, locale);
                          }
                      
                          public String toString()
                          {
                          	return getLabel();
                          }
                      }

                      Comment


                      • #12
                        Is't working for you from the list screen.
                        It works for me when creating a new object, but when I try to display all the objects it shows the technical value.

                        Comment

                        Working...
                        X