I am working on a Java EE6 project using JPA / EJB / JSF and I am having problems designing multi-language support for entities. There are three relevant objects:
Language (has id)
Competence (has id)
CompetenceName (has a link to competency, a link to the language and string)
Competency has a one-to-many link to CompetenceName, implemented using a Map, containing one object for each language in which the name for Competency exists. Note that competencies are created dynamically , and therefore their names cannot exist in a resource bundle.
When listing competencies on a web page, I want them to be displayed in the language of the current user, which is stored in a session-controlled Bean.
Is there a good way to achieve this without breaking the good MVC design? My first idea was to get a bean session directly from the getName method in the Competence object via FacesContext and look at the CompetenceNames map for it as follows:
public class Competence { ... @MapKey(name="language") @OneToMany(mappedBy="competence", cascade=CascadeType.ALL, orphanRemoval=true) private Map<Language, CompetenceName> competenceNames; public String getName(String controller){ FacesContext context = FacesContext.getCurrentInstance(); ELResolver resolver = context.getApplication().getELResolver(); SessionController sc = (SessionController)resolver.getValue(context.getELContext(), null, "sessionController"); Language language = sc.getLoggedInUser().getLanguage(); if(competenceNames.get(language) != null) return competenceNames.get(language).getName(); else return "resource missing"; }
This decision seems extremely rude, because the object relies on the controller level and must receive the session controller each time I want its name. A more acceptable solution for MVC would be to take a language parameter, but this means that every single call from JSF will have to include a language obtained from a session-controlled bean that does not look like a good solution.
Does anyone have thoughts or design patterns for this problem?
database jpa internationalization jsf resourcebundle
Rasmus franke
source share