我在添加多语言到我的网站时遇到了一些麻烦。为什么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
,但是如果我的浏览器设置为荷兰语,它仍会返回荷兰语翻译,如果浏览器设置为英文,则为英文翻译。