2

I am setting the hardcoded messages for Ex: "Are you sure, you want to delete the message?" ,in the message.properties file.

Want to take this messages from the message.properties into javascript. Please suggest me ways to achive this.

For ex:

BootstrapDialog.show({
            message: 'Are you sure you want to '+activeStatus+' this message ?',
            title: 'Alert'
});


function handleAjaxError(xhr, textStatus, error) {

    if (textStatus == 'timeout') {
        alert('The server took too long to send the data.');
    } else if (textStatus == "parsererror") {
        alert("Ajax error occured.");
    }
}
0

4 Answers 4

1

Have a look at the Spring theme tag :

Retrieves the theme message with the given code, or text if code isn't resolvable. The HTML escaping flag participates in a page-wide or application-wide setting (i.e. by HtmlEscapeTag or a "defaultHtmlEscape" context-param in web.xml).

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<spring:theme code="code.of.your.message" text="Alternative text"/>
Sign up to request clarification or add additional context in comments.

1 Comment

This I have used for JSP pages, but I want the same thing to be happen on js pages also.
1

You could read resource properties(messages_en.properties)file from JSP/JSPF file like this

<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:setLocale value="en"/>
<fmt:setBundle basename="messages"/>
<script type="text/javascript">
   var activeStatus= '<fmt:message key="activeStatus"/>';
</script>

or using AJAX

3 Comments

its showing the key ("activeStatus" here) as it is. not its value.
Don't forget to put your properties file under classpath and include the JSP when you using it. Hope this link helpful
I have written the same code the way you have suggested but still it is showing the the text in format '???text???'
0

Before your javascript is first initialized, you can make an ajax call to get i18n texts, and assign them to an javascript object. Later in your js code you can get text from it. Of course you need to have a backend controller to read texts from your message.properties files and gives response to your ajax request.

You need to load your javascripts in the callback of this ajax request, otherwise your text values would be undefined. (Because ajax takes some time).

Comments

0

Write a service that reads all properites and writes Javascript code to its output file like.

Returned content should be like

var properties = { p1:'value1', ... };

and after that include this from your page with html script tag.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.