Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring framework: No message found under code for locale

This is my messageResource declaration

<?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

messages_en.properties
messages_fr.properties
messages.properties

What Am I missing here?

like image 437
user962206 Avatar asked Feb 25 '13 11:02

user962206


4 Answers

In general such issue appears not because of non-existence locale, but because MessageBundle is configured improperly. In your case you seem to need to remove "/" in your basename.

<bean id="messageSource"
     class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
     p:basename="/WEB-INF/messages" />

Why it is so:

If you have messages.properties and messages_en.properties bundle, then bundle name is messages. If you have them in the WEB-INF folder, then basename is /WEB-INF/messages, i.e. according to /path/to/bundle/bundlename. If you have messages.properties within /WEB-INF/messages folder, then corresponding basename is /WEB-INF/messages/messages.

like image 182
n1ckolas Avatar answered Nov 08 '22 23:11

n1ckolas


For spring boot you need something like this:

@Bean
public MessageSource messageSource() {
     ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
     messageSource.setBasename("/WEB-INF/classes/messages");
     return messageSource;
}
like image 21
ropo Avatar answered Nov 08 '22 23:11

ropo


For spring boot folder ressources you need to add name of Bean:

@Bean(name="messageSource")
public ResourceBundleMessageSource bundleMessageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}
like image 6
hadialaoui Avatar answered Nov 09 '22 00:11

hadialaoui


You can specify in Spring boot application.properties too

# INTERNATIONALIZATION 
spring.messages.basename=i18n/messages
spring.messages.encoding=UTF-8
like image 3
Appasamy T Avatar answered Nov 09 '22 00:11

Appasamy T