2012-07-04 42 views
1

我已經在我的項目中成功實現了i18n。現在我被困在我的豆...使用JSF Facelets的國際化(i18n):Java類中的消息

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(新的FileInputStream)方法,但是這不起作用,因爲文件名是base.properties,base_en_US.properties等等。

有沒有人知道在這種情況下的最佳做法?提前致謝!

回答

4

您需要使用與JSF相同的方式來使用加載資源包:使用ResourceBundle API。這裏有一個例子假設基本名稱是com.example.i18n.base(正是因爲你在<f:loadBundle>在視圖中使用值或faces-config.xml<resource-bundle>

ResourceBundle bundle = ResourceBundle.getBundle("com.example.i18n.base", 
    FacesContext.getCurrentInstance().getViewRoot().getLocale()); 
String pageTitle = bundle.getString("page.title"); 
// ... 
+0

感謝BalusC!這完全是我一直在尋找的! – Aquillo

+0

不客氣。 – BalusC