We are currently adding support for a 2nd language in our application, and we load the message bundle in a Session Scoped bean like so
bundle = ResourceBundle.getBundle(languageSetting.getLanguage().getPropertiesFileName(), new OurCustomBundleControl());
This allows us to retrieve messages like so:
public String get(final String key) {
return bundle.getString(key);
}
The languageSetting
variable is a class that we use to switch the language, which is done via a control on the page.
We are doing things this way for two reasons, firstly we need to use non-UTF-8 characters by overriding the Control class (as suggested here), and we are also using CDI to manage scopes.
Our assumption is that the message bundle class can only be Session scoped because different users will need to have different languages selected, but we are concerned that performance wise it may be preferable to have it Application scoped.
Is is possible to have a custom resource bundle with an Application scope? is it recommended?
I'm thinking it may be possible to somehow specify the message bundle in faces-config
which is the usual way, but we would still need to override the Control
class. And then we could switch the language by switching the locale with UIViewRoot#setLocale()
.
Another option might be to have two message bundles, then have a conditional statement in the get
method?