Internationalization (i18n) using JSF Facelets: Messages in Java-classes

我只是一个虾纸丫 提交于 2019-12-02 01:47:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!