0

我们可以像下面这样重用属性文件中的键值吗?

key1=hello
key2=#{key1} world!

谢谢,


更新:下面是覆盖 ResourceBundle 的代码,它可以重用键值,

public class Messages extends ResourceBundle {

    public Messages() {
       setParent(getBundle("resources.test", FacesContext.getCurrentInstance().getViewRoot().getLocale()));
           }

    @Override
    public Enumeration<String> getKeys() {
         return parent.getKeys();
    }


    @Override
    protected Object handleGetObject(String key) {

        try {
               return replaceKey(parent, key, 3);
        } catch (Exception e) {        }
    try { return parent.getObject(key);
    } catch (MissingResourceException e) {
        return key;
    }
    catch (Exception e) {
        return key;
    }
}
    private Pattern Pattern = Pattern.compile("\\$\\{([\\w\\.\\-]+)\\}");

    private String replaceKey(ResourceBundle bundle, String key, int iteration) {
    String message = bundle.getString(key);
    if (message != null) {
      StringBuffer sb = new StringBuffer();
      Matcher matcher = Pattern.matcher(message);
      while (matcher.find() && iteration > 0) {
        // the magic
        matcher.appendReplacement(sb, replaceKey(bundle, matcher.group(1), iteration - 1));
      }
      matcher.appendTail(sb);
      return sb.toString();
    }
    return null;
  }

}

请参阅下面的参考

基本实现

手工制作

4

1 回答 1

1

这只有在自定义时才有可能,ResourceBundle其中您覆盖该handleGetObject()方法以检查检索到的捆绑值是否不包含一些特定的语法,这些语法应该相应地替换为另一个捆绑值。

最后只需ResourceBundle<resource-bundle>or中注册自定义<f:loadBundle>

于 2012-07-27T14:34:02.887 回答