简体   繁体   中英

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

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!

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");
// ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM