【问题标题】:Using resource bundles in servlets?在 servlet 中使用资源包?
【发布时间】:2013-08-30 10:34:43
【问题描述】:

我必须根据语言环境加载属性文件,并且我的 servlet 中有以下代码。

ResourceBundle contactBundle = ResourceBundle.getBundle("/popproperties/impprops", request.getLocale());

我还在popproperties 文件夹中创建了如下的道具文件。

impprops_en_US.properties

//some key value pairs here

以上代码运行良好,没有任何问题。我的问题是我只有一个 porperties 文件为impprops_en_US.properties。如果用户将浏览器设置从美国更改为其他区域设置,则没有其他对应区域设置的属性文件。在这种情况下,我仍然必须加载 impprops_en_US.properties 文件。我怎样才能做到这一点?我需要再创建一个属性文件为impprops.properties 吗?

谢谢!

【问题讨论】:

  • i18n 的文档是怎么说的?我猜它默认使用 en_US,但我知道文档指定了行为。

标签: java jsp servlets internationalization resourcebundle


【解决方案1】:

这取决于您的代码,例如,如果您在选择 default locale 时得到空值/默认值,那么您可以相应地进行检查并加载所需的语言环境。 例如,就我而言:

   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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    • 2016-08-20
    相关资源
    最近更新 更多