问题
I have successfully implemented i18n in my project. Now I'm stuck in my Bean...
private String pageTitle;
public List<Product> getProductsByCategory(String category) {
if(validate(category)) {
pageTitle = category;
return service.getProductsByCategory(String category);
} else {
pageTitle = "Random products";
return service.getRandomProducts();
}
}
As you can see, I would like to let my pageTitle depend on the result of the provided method. All fine, but this is not fine considering internationalization. I have tried using a Properties.load(new FileInputStream) approach, but this does not work considering the files are name base.properties, base_en_US.properties and so on.
Does anyone know the best practice in this kind of situation? Thanks in advance!
回答1:
You need to use the same way as JSF is under the covers using to load resource bundles: using the ResourceBundle API. Here's an example assuming that the base name is com.example.i18n.base (exactly the value as you'd use in <f:loadBundle> in view or <resource-bundle> in faces-config.xml)
ResourceBundle bundle = ResourceBundle.getBundle("com.example.i18n.base",
FacesContext.getCurrentInstance().getViewRoot().getLocale());
String pageTitle = bundle.getString("page.title");
// ...
来源:https://stackoverflow.com/questions/11335195/internationalization-i18n-using-jsf-facelets-messages-in-java-classes