Solved

struts+javascript alert/confirm i18n texts

Posted on 2004-10-18
10
1,841 Views
Last Modified: 2012-05-05
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 ?

Thanks.
0
Comment
Question by:ldbkutty
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 12337543
can't you simply do:

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

or doesn't that work?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12337554
Sorry:

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

Otherwise, you can do:

<%
   ResourceBundle bundle = ResourceBundle.getBundle( "ApplicationResources" ) ;
%>

then;

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

(I think)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12337690
0
 
LVL 3

Accepted Solution

by:
sbockelman earned 400 total points
ID: 12373484
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 ApplicationResources_en.properties file will supply the values for the constants, while the JScript logic does not have to be repeated in language-specific files.

make sense?
0
 
LVL 32

Author Comment

by:ldbkutty
ID: 12378949
Problem is my JS is an external file, so i cannot use the <bean:message....> directly into the file.

sbockelman,
could you please explain me more ?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 35

Expert Comment

by:TimYates
ID: 12378969
>  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?
0
 
LVL 32

Author Comment

by:ldbkutty
ID: 12378990
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.
0
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 100 total points
ID: 12379142
I think what sbocelman meant is something like this:

You have a JSP like this:

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

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 :-)

Tim
0
 
LVL 3

Assisted Solution

by:sbockelman
sbockelman earned 400 total points
ID: 12381915
Tim,

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.

0
 
LVL 35

Expert Comment

by:TimYates
ID: 12381985
:-)  It's a really cool way of doing it :-)

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

Tim

PS:  The CAPITALS idea is a really good one :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maven example error 3 52
Image decoding from Camera 3 72
nextBoolean(double p) for Random class 3 35
table example 4 24
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now