How to handle internationalization in a Spring application (inside a class)

I already know how to handle internationalization in Spring application using <spring:message code="xxx"/>

JSP in page. Now my users are allowed to change languages ​​using simple links like

<a href="?lang=it">IT</a>

and <a href="?lang=en">EN</a>

Now I have to handle internationalization inside the class. This is what I did:

1) I created a text.xml file to determine where my texts are

.....
<bean id="messageSource"  class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="struttura"></property>     
</bean>
......

      

2) I created different properties files according to different languages

3) I am using this method to get the message according to the locale

......
ApplicationContext context =  new ClassPathXmlApplicationContext("text.xml");
String stringa = context.getMessage("textCode",null, locale);
.......

      

Everything is working. But I'm sure this isn't the fastest and cleanest way to do it. It looks too complicated!

Does anyone know the best way to achieve my goal?

+3


source to share


1 answer


MessageResource is a Spring managed bean, so you can simply inject it into your controllers (or other Spring managed classes):



@Autowired 
private MessageSource messageResource;

      

+3


source







All Articles