1

在 Spring 中,我正在实现自己ReloadableResourceBundleMessageSource的方法,并覆盖了resolveCodeWithoutArguments()在 HTML 层管理缺失键的方法。效果很好,问题是在Java层中我想使用该方法

getMessage(String code, Object[] args, String defaultMessage, Locale locale)

该方法不会从属性文件中获取code的描述。args

  • 有人知道我错过了什么吗?

  • 您需要更多信息吗?

豆的定义

<bean id="messageSource" class="com.package.CustomResourceBundle">
    <property name="defaultEncoding" value="UTF-8" />
    <property name="fileEncodings" value="UTF-8"/>
    <property name="cacheSeconds" value="5" />
    <property name="basenames" value="i18n/messages,i18n/support"/>
</bean>

我使用方法 getMessage() 的方法

public static String getBundleMessage(RequestContext context, String key, Object params[]) {

    ApplicationContext appContext = context.getActiveFlow().getApplicationContext();
    DelegatingMessageSource messageSource = (DelegatingMessageSource)appContext.getBean("messageSource");
    messageSource.setAlwaysUseMessageFormat(true);

    return messageSource.getMessage(key, params, "[ "+key+" ]" ,LocaleContextHolder.getLocale());

    }

财产

report_date=Report Date: {0}

CustomResourceBundle 类

public class CustomResourceBundle extends ReloadableResourceBundleMessageSource {

    private String[] basenames = new String[0];
    private long cacheMillis = -1;

    @Override
    public void setCacheSeconds(int cacheSeconds) {
        this.cacheMillis = (cacheSeconds * 1000);
    }

    @Override
    public void setBasenames(String... basenames) {
        if (basenames != null) {
            this.basenames = new String[basenames.length];
            for (int i = 0; i < basenames.length; i++) {
                String basename = basenames[i];
                Assert.hasText(basename, "text");
                this.basenames[i] = basename.trim();
            }
        } else {
            this.basenames = new String[0];
        }
    }

    @Override
    protected String resolveCodeWithoutArguments(String code, Locale locale) {
        if (this.cacheMillis < 0) {
            PropertiesHolder propHolder = getMergedProperties(locale);
            String result = propHolder.getProperty(code);
            if (result != null) {
                return result;
            }
        } else {
            for (String basename : this.basenames) {
                List<String> filenames = calculateAllFilenames(basename, locale);
                for (String filename : filenames) {
                    PropertiesHolder propHolder = getProperties(filename);
                    String result = propHolder.getProperty(code);
                    if (result != null) {
                        return result;
                    }
                }
            }
        }
        return "[ "+code+" ]";
    }

}

更新 - 解决 方法我找不到为什么该方法不能正常工作,所以我实现了该方法。

public static String getResourceBundleMessage(RequestContext context, String key, Object[] params) {

        String finalMsg = "";
        MessageFormat messageFormat = null;

        ApplicationContext appContext = context.getActiveFlow().getApplicationContext();        
        DelegatingMessageSource  messageSource = (DelegatingMessageSource) appContext.getBean("messageSource");

        String  msgValue = messageSource.getMessage(key, null, "[ "+key+" ]", LocaleContextHolder.getLocale());
        messageFormat = new MessageFormat(msgValue);

        if(!msgValue.equalsIgnoreCase("[ "+key+" ]")){
            finalMsg = messageFormat.format(params);
        }           

        return finalMsg;

    }
4

0 回答 0