What I have in the head part of the document is this:
<script language="text/javascript">
function validateForm()
{
var lang = <%= (String)session.getAttribute("locale")%>;
var check = 0;
<!-- do some checks -->
if(check == 1)
{
var conf =confirm(document.write("<script
type='text/javascript' src='lang/" + lang +
"messages.js'>"mesg1"<\/script>")
;
return conf;
}
}
</script>
And the lang/en/messages.js contains
mesg1 = "This is message 1";
And the lang/ar/messags.js contains
mesg1 = "This is Arabic message 1";
However when I run this, it gives me a syntax error at the line which
has document.write();
What am I doing wrong here?
Swetha
(e-mail address removed) wrote:
(e-mail address removed) wrote:
I have a Java-based web application whose interface can be in multiple
languages. My requirement is that the javascript alerts that I display
should be displayed in the language in which the interface has been
generated. The interface language is dynamically loaded and the text is
displayed using Java's <fmt> tags.
How can I make the Javascript alerts, etc. also change their display
language dynamically? Is it possible in Javascript or do I have to use
another technology to do the same?
I imagine you need to set a variable in JavaScript that holds the
current language and set up a JavaScript object that contains the
different messages. Something like...
var lang = "en";
var message1 = {en: "English alert", fr:"French alert"};
alert(message1[lang]);
Does this mean that whenever I add a new language to the application, I
will have to manually add error messages in the new language wherever I
have alerts? Is there some way where this can be done without actually
having to change the code? Something similar to properties files in
Java?
You could do something like this in the head element of the document so
you only load one language.
<script type="text/javascript">
var lang = "en";
document.write("<scr"+"ipt type='text/javascript'
src='lang/"+lang+"/messages.js'></scr"+"ipt>");
</script>
Where you have files like
lang/en/messages.js
message1="english message";
lang/fr/messages.js
message1="french message";
I can't remember the trick about "script" in the document write
statement. i think you only have to break up the second one so that the
script element doesn't close.
Peter