By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,842 Members | 2,440 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,842 IT Pros & Developers. It's quick & easy.

Displaying Javascript alerts in multiple languages

P: n/a
Hello

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 <fmttags.

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?

Regards
Swetha

Sep 12 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
sw***********@gmail.com wrote:
Hello

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 <fmttags.

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]);

Peter

Sep 12 '06 #2

P: n/a
Hi Peter

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?

Thanks
Swetha

petermich...@gmail.com wrote:
sw***********@gmail.com wrote:
Hello

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 <fmttags.

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]);

Peter
Sep 12 '06 #3

P: n/a
sw***********@gmail.com wrote:
petermich...@gmail.com wrote:
sw***********@gmail.com 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 <fmttags.
>
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

Sep 12 '06 #4

P: n/a
pe**********@gmail.com wrote:
<snip>
<script type="text/javascript">
var lang = "en";
document.write("<scr"+"ipt type='text/javascript'
src='lang/"+lang+"/messages.js'></scr"+"ipt>");
</script>
<snip>
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.
Being in a - document.write - call is irrelevant. And HTML parser
reading a script element (as CDATA) has to decide where it is going to
stop passing text to the javascript engine and return to treating text
as mark-up. The HTML spec says that it should do this when it encounters
the first instance of the character sequence '</', which would be
regardless of whether it is in a javascript string (became an HTML
parser known nothing about javascript strings). The SGML spec says that
CDATA will be scanned for '</', but only terminated it when the '</'
turns out to be the start of closing tag for the element, and this is
what browsers do in practice.

However, both the HTML prose and the action of browsers can be satisfied
by avoiding the character sequence '</' inside script elements in an
HTML page. This can be most easily done by escaping the forward slash to
'<\/', which avoids the overheads of superfluous string concatenation
operations.

Opening HTML tags in javascript strings are not an issue at all, and the
HTML spec suggests that the '</' in all closing tags would need to
handled in some way (which the W3C HTML validator used to enforce).

Javascript in external JS files has no issues as no HTML parser ever
examines their text.

Richard.
Sep 12 '06 #5

P: n/a
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

pe**********@gmail.com wrote:
sw***********@gmail.com wrote:
petermich...@gmail.com wrote:
sw***********@gmail.com 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 <fmttags.

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
Sep 12 '06 #6

P: n/a
Ok, it seems to be working now...

what I've done is simply:

<head>
<script type="text/language"
src="lang/<%=(String)session.getAttribute("locale")%>/messages.js">
</script>
</head>

And in the body I access the messages for display.

Thanks
Swetha

sw***********@gmail.com wrote:
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

pe**********@gmail.com wrote:
sw***********@gmail.com wrote:
petermich...@gmail.com wrote:
sw***********@gmail.com 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 <fmttags.
>
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
Sep 12 '06 #7

P: n/a
ASM
sw***********@gmail.com a écrit :
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 +
don't you forget the slash between lang and file?
lang/en/messages.js
"messages.js'>"mesg1"<\/script>"););
return conf;
}
if(check == 1) {
document.write('<script type="text/javascript" '+
'src="lang/'+lang+'/messages.js"><\/script>";
var conf = confirm(mesg1);
if(conf) alert('good');
else alert('bad');
}
}
</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?
slash missing ?

--
Stephane Moriaux et son [moins] vieux Mac
Sep 12 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.