我必须根据语言环境加载属性文件,并且我的servlet中有以下代码。
ResourceBundle contactBundle = ResourceBundle.getBundle("/popproperties/impprops", request.getLocale());
我也在popproperties
文件夹中创建了如下所示的道具文件。
//some key value pairs here
以上代码工作正常,没有任何问题。我的问题是我只有一个porperties文件为impprops_en_US.properties
。如果用户将浏览器设置从美国更改为其他某个区域设置,则没有其他对应的区域设置属性文件。在这种情况下,我仍然需要加载impprops_en_US.properties
文件。我怎样才能做到这一点?我是否需要再创建一个属性文件impprops.properties
?
谢谢!
答案 0 :(得分:4)
这取决于您的代码,如果您在选择default locale
时获得null / default,那么您可以进行检查并加载所需的区域设置。
对于前者,在我的情况下:
String cc=req.getParameter("country");
String ln=req.getParameter("language");
Locale l=null;
if(cc==null)
l=new Locale("en","US");
else
l=new Locale(ln,cc);
ResourceBundle rb=ResourceBundle.getBundle("ApplicationResources",l);
req.setAttribute("resource", rb);