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