Spring 框架:在语言环境的代码下找不到消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15065734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring framework: No message found under code for locale
提问by user962206
This is my messageResource declaration
这是我的 messageResource 声明
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- Auto-detect controllers in this package -->
<context:component-scan base-package="levelup.world.web" />
<!-- Prepend /WEB-INF/jsp/ and append .jsp to the view name -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Access resource bundles with the specified basename -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
p:basename="/WEB-INF/messages/" />
</beans>
When I run my application, this error shows up
当我运行我的应用程序时,出现此错误
No message found under code 'country.plural' for locale 'fil_PH'
now inside my messages folder inside web-inf, I have the following message properties
现在在 web-inf 的消息文件夹中,我有以下消息属性
messages_en.properties
messages_fr.properties
messages.properties
What Am I missing here?
我在这里错过了什么?
回答by n1ckolas
In general such issue appears not because of non-existence locale, but because MessageBundleis configured improperly. In your case you seem to need to remove "/" in your basename.
一般来说,出现此类问题不是因为不存在语言环境,而是因为MessageBundle配置不当。在您的情况下,您似乎需要删除基本名称中的“/”。
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
p:basename="/WEB-INF/messages" />
Why it is so:
为什么会这样:
If you have messages.propertiesand messages_en.propertiesbundle, then bundle name is messages. If you have them in the WEB-INFfolder, then basename is /WEB-INF/messages, i.e. according to /path/to/bundle/bundlename. If you have messages.propertieswithin /WEB-INF/messagesfolder, then corresponding basename is /WEB-INF/messages/messages.
如果您有messages.properties和messages_en.properties捆绑,则捆绑名称为messages. 如果WEB-INF文件夹中有它们,则 basename 为/WEB-INF/messages,即根据 / path/to/bundle/bundlename。如果您有文件夹messages.properties内/WEB-INF/messages,则相应的基本名称是/WEB-INF/messages/messages.
回答by ropo
For spring boot you need something like this:
对于弹簧靴,您需要这样的东西:
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("/WEB-INF/classes/messages");
return messageSource;
}
回答by hadialaoui
For spring boot folder ressources you need to add name of Bean:
对于 spring boot 文件夹资源,您需要添加 Bean 的名称:
@Bean(name="messageSource")
public ResourceBundleMessageSource bundleMessageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}
回答by Appasamy T
You can specify in Spring boot application.properties too
您也可以在 Spring boot application.properties 中指定
# INTERNATIONALIZATION
spring.messages.basename=i18n/messages
spring.messages.encoding=UTF-8
回答by Tomasz Dudkowski
You can add some code in application.properties:
您可以在 application.properties 中添加一些代码:
spring.messages.always-use-message-format=false
spring.messages.basename=messages
spring.messages.cache-seconds=-1
spring.messages.encoding=UTF-8
spring.messages.fallback-to-system-locale=true
回答by MR AND
By default maven assumes to find such resource bundle message source under
默认情况下,maven 假定在下面找到这样的资源包消息源
src/main/resources
源代码/主要/资源
. By moving all the necessary folders under the location and also making sure we have the following code in the context
. 通过移动该位置下的所有必要文件夹,并确保我们在上下文中有以下代码
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename"><value>messages</value></property>
</bean>

