我在添加多語言到我的網站時遇到了一些麻煩。爲什麼ResourceBundle.getBundle(String,Locale)忽略Locale?
它對jstl的.jsp文件工作正常,但是當我嘗試使用Java代碼獲取翻譯時,它會忽略Locale參數並使用客戶端瀏覽器區域設置。
我:
兩個屬性文件: dictionary.properties,包含:validate.firstname.empty=You have to enter a first name
dictionary_nl.properties,包含:validate.firstname.empty=U heeft geen voornaam ingevuld
而這個Java代碼:
public List<String> validate(UserForm user, Locale locale)
{
List<String> errors = new ArrayList<>();
ResourceBundle resources = ResourceBundle.getBundle("dictionary", new Locale("en"));
if (user.getFirstName() == null || user.getFirstName().trim().isEmpty())
{
errors.add(resources.getString("validate.firstname.empty"));
}
return errors;
}
出於測試目的,我插入了一個Locale的新實例到getBundle
,但是如果我的瀏覽器設置爲荷蘭語,它仍會返回荷蘭語翻譯,如果瀏覽器設置爲英文,則爲英文翻譯。