Reputation: 4267
I already know how to handle internationalization in a Spring application using <spring:message code="xxx"/>
in a JSP 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 a class. This is what I did:
1) I created a text.xml file to identify where my texts are
.....
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="struttura"></property>
</bean>
......
2) I created a different properties files according to different languages
3) I use this method to get the message according to the locale
......
ApplicationContext context = new ClassPathXmlApplicationContext("text.xml");
String stringa = context.getMessage("textCode",null, locale);
.......
Everything works. But I'm sure this is not the fastest and cleanest way to do it. It looks too intricate!
Does anybody know a better way to reach my goal?
Upvotes: 4
Views: 425
Reputation: 15628
MessageResource is a Spring managed bean so you can just inject it into your controllers (or other Spring managed classes):
@Autowired
private MessageSource messageResource;
Upvotes: 4