Displaying Javascript alerts in multiple languages

S

swethasivaram

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 <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?

Regards
Swetha
 
P

petermichaux

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



Peter
 
S

swethasivaram

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

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



Peter
 
P

petermichaux

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
 
R

Richard Cornford

<script type="text/javascript">
var lang = "en";
document.write("<scr"+"ipt type='text/javascript'
src='lang/"+lang+"/messages.js'></scr"+"ipt>");
</script>
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.
 
S

swethasivaram

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:

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
 
S

swethasivaram

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

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
 
A

ASM

(e-mail address removed) 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 ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top