Close

Spring Framework - ResourceBundleMessageSource Examples

Spring Framework 

ResourceBundleMessageSource implements MessageSource and uses. ResourceBundle internally. In this core Spring example we, are injecting MessageSource in MyBean which needs to show localized messages.

package com.logicbig.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;

import java.io.IOException;
import java.util.Locale;

public class MessageSourceExample {
public static void main (String[] args) throws IOException {
Locale.setDefault(Locale.US);
//uncomment next line to change the locale
//Locale.setDefault(Locale.FRANCE);
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(Config.class);

MyBean bean = context.getBean(MyBean.class);
bean.doSomething();
}

@Configuration
public static class Config {

@Bean
public MyBean myBean () {
return new MyBean();
}

@Bean
public MessageSource messageSource () {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages/msg");
return messageSource;
}
}

public static class MyBean {
@Autowired
private MessageSource messageSource;

public void doSomething () {
System.out.println(messageSource.getMessage("app.name", new Object[]{"Joe"},
Locale.getDefault()));
}
}
}

resources/messages/msg_en_us.properties:

(In a maven project, 'resources' folder is in class path by default)

app.name = resource bundle test invoked by {0}

Output

resource bundle test invoked by Joe
Original Post




This example shows how to use ApplicationContext to get localized messages. ApplicationContext extends MessageSource but delegates the message resolution to the underlying configured MessageSource instance.

package com.logicbig.example;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;

import java.io.IOException;
import java.util.Locale;

public class MessageSourceViaContextExample {
public static void main (String[] args) throws IOException {
Locale.setDefault(Locale.US);
//uncomment next line to change the locale
// Locale.setDefault(Locale.FRANCE);
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(Config.class);

System.out.println(context.getMessage("app.name", new Object[]{"Joe"},
Locale.getDefault()));

}

@Configuration
public static class Config {

@Bean
public MessageSource messageSource () {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages/msg");
return messageSource;
}
}
}

Output

resource bundle test invoked by Joe
Original Post




This example shows how to use multiple message sources using messageSource.setBasenames(..)

package com.logicbig.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;

import java.io.IOException;
import java.util.Locale;

public class MultipleMessageSourceExample {
public static void main (String[] args) throws IOException {
Locale.setDefault(Locale.US);
//uncomment next line to change the locale
//Locale.setDefault(Locale.FRANCE);
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(Config.class);

MyBean bean = context.getBean(MyBean.class);
bean.doSomething();
}

@Configuration
public static class Config {

@Bean
public MyBean myBean () {
return new MyBean();
}

@Bean
public MessageSource messageSource () {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasenames("messages/msg", "messages/msg2");
return messageSource;
}
}

public static class MyBean {
@Autowired
private MessageSource messageSource;

public void doSomething () {
System.out.println(messageSource.getMessage("app.name",
new Object[]{"Joe"},
Locale.getDefault()));
System.out.println(messageSource.getMessage("app2.name",
new Object[]{"Jackie"},
Locale.getDefault()));
}
}
}

Output

resource bundle test invoked by Joe
resource bundle another test invoked by Jackie
Original Post




This example shows how to use messageSource.setParentMessageSource(..) to add multiple sources hierarchically.

package com.logicbig.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;

import java.io.IOException;
import java.util.Locale;

public class ParentMessageSourceExample {
public static void main (String[] args) throws IOException {
Locale.setDefault(Locale.US);
//uncomment next line to change the locale
// Locale.setDefault(Locale.FRANCE);
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(Config.class);

MyBean bean = context.getBean(MyBean.class);
bean.doSomething();
}

@Configuration
public static class Config {

@Bean
public MyBean myBean () {
return new MyBean();
}

@Bean
public MessageSource messageSource () {
ResourceBundleMessageSource messageSource =
new ResourceBundleMessageSource();
messageSource.setBasename("messages/msg2");
ResourceBundleMessageSource parentMessageSource =
new ResourceBundleMessageSource();
parentMessageSource.setBasename("messages/msg");
messageSource.setParentMessageSource(parentMessageSource);
return messageSource;
}
}

public static class MyBean {
@Autowired
private MessageSource messageSource;

public void doSomething () {
System.out.println(messageSource.getMessage("app.name",
new Object[]{"Joe"},
Locale.getDefault()));
System.out.println(messageSource.getMessage("app2.name",
new Object[]{"Jackie"},
Locale.getDefault()));
}
}
}

Output

resource bundle child test invoked by Joe
resource bundle another test invoked by Jackie
Original Post




See Also