Spring MessageSource多次解析/插入字符串

时间:2012-09-17 23:07:34

标签: java spring internationalization

所以,我很确定这只是因为我自己对Spring和Java中各种消息解析/插值的发生方式缺乏了解,我想要的是:

InternationalMessages.properties

BadParsingFormat={{0}} should fall betweeen {1} and {2}
pointTop=The top coordinate
pointLeft=The left coordinate

APIEndPoint.java

// Standard implementation and wiring of message source
@Autowired
MessageSource messageSource;

public void someMethod(String somethingToParse) {
    String parsed;
        try {
            parsed = SomeKindOfParser.parse(somethingToParse);
        }
        catch(BadParsingFormatException exception) {
            String error = messageSource.getMessage("BadParsingFormat",
                new Object[]{
                        exception.getVariableName(),
                        exception.getLowerBound(),
                        exception.getUpperBound()
                });
            // Do something useful with this error message
        }
}

在这个俗气的示例中,exception.getVariableName()将返回pointToppointLeft。我想进一步解决/插入到特定的名称,但我似乎无法完成它。我知道我可以使用另一个messageSource调用并单独获取该bundle值,但如果我能避免这种情况,我会更喜欢它。

1 个答案:

答案 0 :(得分:0)

无法想到除了你已经建议的任何其他方式 - 再次调用messageSource:

BadParsingFormat={0} should fall betweeen {1} and {2}
pointTop=The top coordinate
pointLeft=The left coordinate

使用代码

   String error = messageSource.getMessage("BadParsingFormat",
            new Object[]{
                    messageSource.getMessage(exception.getVariableName(),null, null),
                    exception.getLowerBound(),
                    exception.getUpperBound()
            });
相关问题