Normally I retrieve i18n messages via <spring:message code="" />
tag in jsp. But as response of ajax query I get message code in i18n property file. How can I get message by this code via JS?
Asked
Active
Viewed 4,617 times
0

kassie
- 727
- 4
- 11
- 24
-
See also https://stackoverflow.com/questions/6218970/resolving-springmessages-in-javascript-for-i18n-internationalization – Grigory Kislin May 24 '17 at 23:03
2 Answers
2
There is no "normal" way to get messages from JS, but you have two solutions:
First solution: by an Ajax call.
Second solution : Send you're value at the loading of the page in hidden input html
<c:set var="val"><spring:message code="username"/></c:set>
<input id="username" type="hidden" value="${val}"/>
In your javascript (using jquery) you can then use it as follows:
$('#username').val()

skulled
- 1,689
- 1
- 13
- 9
1
You can inject MessageSource
to your controller and return a String
message from that controller. Simply:
@Autowired
MessageSource messageSource;
@RequestMapping(value="/myajax", method=RequestMethod.GET)
@ResponseBody
public String getMyAjaxMessage() {
return messageSource.getMessage(..); // use your proper arguments or extract from request parameters
}
javascript (assuming jquery is used):
$.get('/myajax', function(data){
// do whatever you want with data (will contain your message)
});
Here are docs:

px5x2
- 1,495
- 2
- 14
- 29