Announcement Announcement Module
No announcement yet.
Collection View based in Locale - CollectionPropertyEditor? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Collection View based in Locale - CollectionPropertyEditor?


    First, I want to contextualice the problem to explain it easier:

    Think In a Entity which has some types and this type is an attribute of the entity (it is algo an entity)

    I'm working with I18n in 3 locales: Spanish, Galician And English. To represent this situation, the entity type is like this:

    public class Type
        private Integer id;
        private String nameES;
        private String nameGL;
        private String nameUS;
        public String getNameByLang(String locale);
    The method getNameByLang receives "ES", "GL" or "US" and invoke the specific getter for the property represented by this "locale name".

    Now, in the web layer, I want to display in a select the different types in the current locale of the application. To do this, I need to make some trick to show the message in the current locale:

    - I'm using a LabelValue object to fill in it the label and the value of the type. The label is in the current locale.
    - To build the List of LabelValues to show in the select, I do something like this :

    Collection<Types> types = service.getTypes();
    List<LabelValue> typeList = new ArrayList<Type>();
    for(Type t : types)
        typeList.add(new LabelValue( type.getID, type.getNameByLang(locale.getCountry) );
    model.addAttribute("types", typeList);
    This works but perhaps using some Spring advanced concepts (I'm thinking in the possibilities of CollectionPropertyEditors or something similar) I could do it easier. My "ideal" idea would be:

    - Inject the locale/Access the locale from the CollectionPropertyEditor.
    - Passing the collection of types to the select, the property editor automatically converts the label like I'm doing manually.

    ┐Could be this possible or some other approach to solve this situation? The solution I propose at the beginning of this post works, but I think it could be improved.


  • #2
    Is there a way to create a JSP custom tag library and to to use the locale on the fly from JSP?

    I mean something like this: you pass your types variable directly to the model bypassing the build of LabelValue list and use it directly in JSP:
    <c:forEach var="type" items="${types}">
        <myTags:multiLang value="${}">
    where name is a multi-lingual item, supporting all possible locales you need.
    public class MultiLingualWord
        private Integer id;
        private Hash<Locale, String> names;
        public String getValue(Locale locale);
    getValue(locale) will be simply called by taglib passing the correct Locale taken from the context.

    I wonder if there is something standard from spring like that? At least this is nearly how the MessageSource should be implemented to my understanding..
    Last edited by Leo++; Sep 3rd, 2008, 10:45 AM.