I had the same Idea as I develop also with Vaadin and the solution which works for me is to use a StaticContextInitializer Bean. Therefore do the following:
First make your field for the Message class static and provide the getter and setter methods:
private static MessageSource messageSource;
private TextSource() {
}
public static String getText(String key, Locale locale) {
return messageSource.getMessage(key,null, ensureLocale(locale));
}
public static String getText(String key, Locale locale, Object[] parameter) {
return messageSource.getMessage(key, parameter, ensureLocale(locale));
}
private static Locale ensureLocale(Locale locale) {
if (locale == null)
locale = Locale.getDefault();
return locale;
}
Add the static setter method to the class:
public static void setMessageSource(MessageSource messageSource) {
TextSource.messageSource = messageSource;
}
Write your StaticInitializer Bean with a @PostConstruct
annotation and inject the MessageSource with @Autowired
annotation.
@Component
public class FrontendStaticContextInitializer {
@Autowired
private MessageSource messageSource;
@PostConstruct
public void initialize() {
TextSource.setMessageSource(messageSource);
}
}
After that you'll be able to call the class like that in your Views: TextSource.getText("login.textfield.placeholder.benutzername", getLocale())