【问题标题】:Internal references in propertyfiles for Spring MessageSourceSpring MessageSource 的属性文件中的内部引用
【发布时间】:2014-03-03 11:39:03
【问题描述】:

我有几个网页,上面有类似的表格。

存在于多个页面中的一个字段是电子邮件地址。

我希望能够使用特定于页面的消息代码,但我希望能够引用另一个消息代码以便拥有一个声明。通过这种方式,我可以将电子邮件地址标签的外观更改为一个位置并在所有网页中进行更改,但同时,我可以仅通过属性文件更新更改单个页面的文本。

我正在寻找这样的功能:

message.properties:

label.email=Email address

webpage1.label.email=${label.email}
webpage2.label.email=${label.email}

然而, 使用以下jsp代码时:

<spring:message code="webpage1.label.email"/>

我在我的网页中获得了 ${label.email} 而不是“电子邮件地址”的字面值。

有什么提示吗?

【问题讨论】:

    标签: spring spring-mvc internationalization


    【解决方案1】:

    你可以用这个替换 DefaultPropertiesPersister:

    这将允许您引用其他条目,例如:

    user=User
    user.add=Add ${user}
    user.delete=Delete ${user}
    

    只需使用您的 MessageSource 指定此持久性,例如messageSource.setPropertiesPersister(new RecursivePropertiesPersister());

    来源:

    public class RecursivePropertiesPersister extends DefaultPropertiesPersister {
        private final static Pattern PROP_PATTERN = Pattern.compile("\\$'?\\{'?([^}']*)'?\\}'?");
        private final static String CURRENT = "?LOOP?";
    
        @Override
        public void load(Properties props, Reader reader) throws IOException {
            Properties propsToLoad = new Properties();
            super.load(propsToLoad, reader);
            replace(propsToLoad, props);
        }
    
        @Override
        public void load(Properties props, InputStream is) throws IOException {
            Properties propsToLoad = new Properties();
            super.load(propsToLoad, is);
            replace(propsToLoad,props);
        }
    
        protected void replace ( Properties src, Properties dest) {
            for (Map.Entry entry: src.entrySet()) {
                String key = (String) entry.getKey();
                String value = (String)entry.getValue();
                replace(src, dest, key, value);
            }
        }
    
        protected String replace(Properties src, Properties dest, String key, String value) {
            String replaced = (String) dest.get(key);
            if (replaced != null) {
                // already replaced (or loop), just return the string
                return replaced;
            }
            dest.put(key,CURRENT); // prevent loops
            final Matcher matcher = PROP_PATTERN.matcher(value);
            final StringBuffer sb = new StringBuffer();
            while (matcher.find()) {
                final String subkey = matcher.group(1);
                final String replacement = (String)src.get(subkey);
                matcher.appendReplacement(sb,replace(src,dest,subkey,replacement));
            }
            matcher.appendTail(sb);
            final String resolved = sb.toString();
            dest.put(key, resolved);
            return resolved;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-01
      • 2012-10-21
      • 1970-01-01
      • 2015-05-28
      • 1970-01-01
      • 2017-12-15
      • 2018-04-18
      • 1970-01-01
      相关资源
      最近更新 更多