问题描述
我已经在我的项目中成功实施了i18n.现在我被卡在豆子里了...
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();
}
}
如您所见,我想让我的pageTitle依赖于所提供方法的结果.一切都很好,但是考虑到国际化,这还不是很好.我尝试使用Properties.load(new FileInputStream)方法,但是考虑到文件名为base.properties,base_en_US.properties等,此方法不起作用.
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!
推荐答案
您需要使用与JSF相同的方式来装载资源束:使用 ResourceBundle
API.这是一个示例,假设基本名称为com.example.i18n.base
(正是您在视图中的<f:loadBundle>
或faces-config.xml
中的<resource-bundle>
中使用的值)
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");
// ...
这篇关于使用JSF Facelets的国际化(i18n):Java类中的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!