Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2012
  • Last Modified:

struts+javascript alert/confirm i18n texts

I include an external JS file for my struts JSP.

How i can specify <bean:message key="blah.blah"/> (Application Resources messages) for my JS alert and confirm Texts !?

At present, i have only direct german messages in the alert JS function. However if my project is internationalized, i will need english texts there.

Should i pass the <bean:message key="blah.blah"/> as arugument for the JS function i am calling from my JSP !? If yes, please tell me how it should be ?

  • 5
  • 2
  • 2
  • +1
3 Solutions
can't you simply do:

  myFunction( <bean:message key="blah.blah"/> ) ;

or doesn't that work?

  myFunction( '<bean:message key="blah.blah"/>' ) ;

Otherwise, you can do:

   ResourceBundle bundle = ResourceBundle.getBundle( "ApplicationResources" ) ;


  myFunction( '<%= bundle.getString( "blah.blah" ) %>' ) ;

(I think)
zzynxSoftware engineerCommented:
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

You COULD do something like this:

make a *.js file that is really a JSP
in that file define "javascript constants" (i.e., var ERROR_REQUIRED = "required field"; )
but, instead of hardcoded Strings, use bean:write to supply the values

then in the actual *.js files, use the "constants" where you would use these Strings.

at runtime, then, the file will supply the values for the constants, while the JScript logic does not have to be repeated in language-specific files.

make sense?
ldbkuttyAuthor Commented:
Problem is my JS is an external file, so i cannot use the <bean:message....> directly into the file.

could you please explain me more ?
>  Problem is my JS is an external file, so i cannot use the <bean:message....> directly into the file.

But surely you can call it with <bean:message tags in the function calls?
ldbkuttyAuthor Commented:
yes, i can call with function calls, but just thinking if there is a way to put <bean:message > directly in the JS

If it is not possible, i have to go with function calls.
I think what sbocelman meant is something like this:

You have a JSP like this:

    <script type="text/javascript" src="myjavascript.js"><!-- Import javascript --></script>
    <script type="text/javascript" src="javascript-constants.jsp"><!-- Import constants --></script>
  <body bgcolor="#ffffff">
    <script type="text/javascript">
        doWrite() ;

with your javascript file (myjavascript.js) like this:

// This is the main javascript file
// Set up default values here:
var value = 'This is the default value' ;
// functions, using the default values
function doWrite()
  document.write( value ) ;

And a jsp file (javascript-constants.jsp) like this:

// Fraudulent JavaScript File...
// This jsp pretends to be a javascript file, so it can overwrite some constants
// you should be able to replace the < % bit with < bean:message
var value = '<%= "Cest la valeur par d&eacute;faut" %>' ;

Then, as you can see, the javascript-constants.jsp file overwrites the "value" variable with the French version of the text..

so doWrite prints french, NOT english as it originally was in the javascript file...

You should be able to replace the <%= %> bit with a bean:message bit instead :-)


Yes, that's pretty much what I meant.

Thanks for the more explicit formulation.

I wouldn't necessarily even bother with defaults, however, and have to trust that I didn't forget to overwrite some variables in the constants file.

I'd also name the JS variables used as constants with the UPPER_CASE convention to make it clear.

I think that's a good i18n strategy for client-side code.

:-)  It's a really cool way of doing it :-)

I just had to give it a go, to see if it would work right ;-)  


PS:  The CAPITALS idea is a really good one :-)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now