Internationalization in a Vaadin4Spring application is very much handled by the I18N features of Spring itself. However, to make it a bit easier to use, the addon provides a helper class, namely I18N.
The I18N
can be injected into any Spring managed bean and uses the locale of the currently active UI
when looking up messages from the application context.
Example:
@Autowired
I18N i18n;
...
myLabel = new Label(i18n.get("myMessageKey", "My argument"));
It is also possible to specify the Locale, when the bean creation happens before the UI is created.
Example:
@Autowired
I18N i18n;
...
myLabel = new Label(i18n.getWithLocale("myMessageKey", Locale.GERMAN, "My argument"));
A composite message source is an implementation of the Spring MessageSource
interface
that resolves messages by querying all beans in the application context that implement the
MessageProvider interface.
The idea behind this is to make it possible to build modular UI applications that all share a single message source.
Each module would have its own message provider, and would use either I18N
or the application context directly
to retrieve localized strings. The modules would be completely separated on a source code level, but during runtime they
would all run inside a single application context.
You can enable the composite message source by adding the @EnableCompositeMessageSource
annotation to your
Spring configuration. This will add the message source to your application context.
After this, create as many MessageProvider
beans as you want. You can either implement the interface from
scratch, or use the ResourceBundleMessageProvider. The latter reads messages from
resource bundles with a specific basename and encoding (check the JavaDocs for more information).
In the main application:
@EnableAutoConfiguration
@EnableCompositeMessageSource
@ComponentScan
@Configuration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
...
}
In the UI module:
@Configuration
public class MyModule {
@Bean
MessageProvider communicationMessages() {
return new ResourceBundleMessageProvider("mymodule.messages"); // Will use UTF-8 by default
}
...
}
Finally a tip: If you are building a modular application, it is good practice to prepend the messages with some kind of module identifier, for example a Java package name. That way you can avoid naming conflicts when additional modules are added in the future.