Suggestion on localization with seam 3 international
conte Sep 21, 2012 2:54 AMHello. I'm building a custom localizer for my web application using Seam3 international module. In my class I have injected the user locale as suggested into Seam3 international documentation. This is my class:
@Named
@RequestScoped
public class CustomLocale {
@Inject
private Identity identity;
@Inject
private IdentitySession identitySession;
@Inject
private CustomLocaleConfiguration localeConfiguration;
@Inject
@Client
@Alter
private Event<Locale> localeEvent;
@Inject
@Client
private Locale currentUserLocale;
@Inject
private UserLogger userLogger;
/* Transaction manager */
@Resource private UserTransaction utx;
/**
* Set the user locale based on passed parameter
*
* @param Locale
* , a {@link Locale}class that represents the new user locale to
* set
* @throws UnsupportedLocaleException if the locale that you try to set isn't supported
*/
public void setUserLocale(Locale locale) throws UnsupportedLocaleException {
if (localeConfiguration.getSupportedLocaleKeys().contains(locale.getLanguage())) {
localeEvent.fire(locale);
} else throw new UnsupportedLocaleException();
}
/**
* Set the user language based on passed parameter
*
* @param String
* , a {@link String}variable that represents the new user language to
* set
* @throws UnsupportedLocaleException if the language that you try to set isn't supported
*/
public void changeUserLanguage(ValueChangeEvent event) throws UnsupportedLocaleException {
setUserLocale(new Locale((String) event.getNewValue()));
}
/**
* Save the application preferred language of the user into database
*
* @return a {@link String} that represents the navigation rule
*/
public String savePreferredLanguage() {
try {
utx.begin();
Attribute preferredLanguage = new SimpleAttribute(Constants.IDENTITY_TYPE_LANGUAGE, (String) currentUserLocale.getLanguage());
identitySession.getAttributesManager().updateAttributes(
identity.getUser(), new Attribute[] { preferredLanguage });
utx.commit();
} catch (Exception e) {
try {
utx.rollback();
} catch (Exception e2) {
userLogger.rollbackException(ExceptionUtils.getStackTrace(e2));
}
userLogger.updateLanguageException(ExceptionUtils.getStackTrace(e));
return Constants.EXCEPTION;
}
return null;
}
}
This class is used by a web page to manage the application preferred language of the user. As you can see, I have a ValueChangeListener (method changeUserLanguage) that is used by a selectbox to change the locale. When the user click on the submit button, the ValueChangeListener changes the locale but, in the action method savePreferredLanguage(), the injected value currentUserLocale is the same as before the change (this because the observer of the org.jboss.seam.international.locale.UserLocaleProducer changes the local reference of the userLocale variable but the injected value into my bean keep following the previous instance of the userLocale object).
So, I solved this problem putting into two separated request scoped classes the ValueChangeListener method (changeUserLanguage) and the action method (savePreferredLanguage). In this way, when the ValueChangeListener is called, it changes the userLocale (variable of the class org.jboss.seam.international.locale.UserLocaleProducer) and than, when the action is called, the fresh value is injected into bean.
My question is: is there a better solution that permits to mantain ValueChangeListener and action method on the same class? Putting all logic into ValueChangeListener isn't the correct solution for me ;-)
Thanks