简体   繁体   English

找不到MessageSource的ResourceBundle [messages]:无法找到基本名称消息的包

[英]ResourceBundle [messages] not found for MessageSource: Can't find bundle for base name messages

In applicationContext.xml I have defined MessageSource like this: 在applicationContext.xml中,我已经定义了MessageSource,如下所示:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>/WEB-INF/i18n/messages</value>
        </list>
    </property>
</bean>

I also need to load all the localized messages so I have made my own class for it: 我还需要加载所有本地化的消息,所以我已经为它创建了自己的类:

public class ErpMessageSource extends ResourceBundleMessageSource {

    public Map<String, String> getMessages(String basename, Locale locale) {
        Set<String> keys = getKeys(basename, locale);
        Map<String, String> m = new HashMap<String, String>();

        for (String key : keys) {
            try {
                m.put(key, getMessage(key, null, locale));              
            } catch (Exception e) {
                System.err.println(key + " - " + e.getLocalizedMessage());
            }
        }

        return m;
    }

    private Set<String> getKeys(String basename, Locale locale) {
        ResourceBundle bundle = getResourceBundle(basename, locale);
        return bundle.keySet();
    }

}

I have 2 problems with this: 我有2个问题:

Problem 1: My message files are located under WEB-INF/i18n directory. 问题1:我的消息文件位于WEB-INF / i18n目录下。 It contains only 2 files: messages_en.properties and messages_hr.properties. 它只包含2个文件:messages_en.properties和messages_hr.properties。

If I try to run the above code I'm getting warning: "ResourceBundle [messages] not found for MessageSource: Can't find bundle for base name messages, locale 'some locale'", followed by NullPointerException. 如果我尝试运行上面的代码,我会收到警告:“找不到MessageSource的ResourceBundle [messages]:找不到基本名称消息的bundle,locale'some locale'”,后跟NullPointerException。

If I move messages files to WEB-INF/classes folder problem disappears however I get 2nd problem. 如果我将消息文件移动到WEB-INF / classes文件夹,问题就会消失,但是我遇到第二个问题。

Problem 2: If I run the code above while messages are under WEB-INF/classes directory I'm getting exception: "No message found under code 'some.code' for locale 'some locale'" even thou all keys are loaded correctly. 问题2:如果我在WEB-INF / classes目录下运行上面的代码我得到异常:“在代码'some.code'下找不到用于locale'some locale'的消息”即使你所有的密钥都正确加载。

Question is: how to avoid problem no.1 by keeping messages inside WEB-INF/i18n folder and I need a solution for 2nd problem because I really have no idea what's going on there. 问题是:如何通过将消息保存在WEB-INF / i18n文件夹中来避免问题1,我需要第2个问题的解决方案,因为我真的不知道那里发生了什么。

You probably only have to add the resource folder to the classpath of your project. 您可能只需要将资源文件夹添加到项目的类路径中。

Try to do the following operation: 尝试执行以下操作:

Right click on the project name in the Package Explorer view ---> Properties ---> Java Build Path ---> stay in the Source tab (opened by default) ---> click on Folder ---> the list of all the folders inside your project should now appear ---> tick on the folder that contains the message properties file 右键单击Package Explorer视图中的项目名称---> Properties ---> Java Build Path --->保留在Source选项卡中(默认打开)--->单击Folder ---> list现在应该出现项目中所有文件夹--->勾选包含消息属性文件的文件夹

Rebuild the project and try to run. 重建项目并尝试运行。

Although it has been 2 years since the question asked, but I've got the same problem and found a solution. 虽然问题已经过去了2年,但我遇到了同样的问题并找到了解决方案。

If you want to keep your i18n properties file somewhere outside the webcontent folder, you should modify the file org.eclipse.resources.prefs in the .setting folder. 如果要将i18n属性文件保留在webcontent文件夹之外的某个位置,则应修改.setting文件夹中的文件org.eclipse.resources.prefs。 and do like this: 并且这样做:

eclipse.preferences.version=1
encoding//WebContent/WEB-INF/i18n/property_en.properties=UTF-8
encoding//WebContent/WEB-INF/i18n/property_fr.properties=UTF-8

In the bean property, set the messageSource like you did before. 在bean属性中,像以前一样设置messageSource。 but add : 但添加:

<property name="defaultEncoding" value="UTF-8" />

Hope it works. 希望它有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “无法找到基本名称消息的包”错误 - “Can't find bundle for base name messages” error 无法使用 ResourceBundle.getBundle() 加载 ResourceBundle 找不到基本名称的包,区域设置 en_US - Unable to load ResourceBundle with ResourceBundle.getBundle() Can't find bundle for base name , locale en_US java.util.MissingResourceException:找不到基本名称消息的bundle,locale en_US - java.util.MissingResourceException: Can't find bundle for base name messages, locale en_US glassfish服务器上的“找不到基本名称消息包,语言环境ar_EG”例外是什么? - What is “Can't find bundle for base name messages, locale ar_EG” exception on glassfish server? YuiCompressorTask:“无法找到基本名称org.mozilla.javascript.resources.Messages,locale en_US的包” - YuiCompressorTask: “Can't find bundle for base name org.mozilla.javascript.resources.Messages, locale en_US” java.util.MissingResourceException:找不到基本名称消息包,语言环境en_US Floreant POS - java.util.MissingResourceException: Can't find bundle for base name messages, locale en_US Floreant POS 找不到MessageSource的ResourceBundle - ResourceBundle not found for MessageSource .MissingResourceException:找不到基本名称的包 - .MissingResourceException: Can't find bundle for base name MissingResourceException - 找不到基本名称的包 - MissingResourceException - Can't find bundle for base name 找不到基本名称的包 - Can't find bundle for base name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM