473,403 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Displaying Javascript alerts in multiple languages

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
7 8292
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Greg Swindle | last post by:
Hello, I have a question about how prototyping relates to variables and their scope. Given the following code: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ var ParentObject = function() {...
14
by: tshad | last post by:
I posted this on the asp.net group, also. I wasn't sure whether this was an asp.net problem or a javascript problem. I have a page that was originally created from a program I found on the net...
12
by: korund | last post by:
How to make javascript alert with non-english text displaying correctly on computers where english only is default system & language settings? For web page the solution is just use meta tags:...
2
by: Severus Snape | last post by:
I can display and hide 1 object at a time, but haven't seen it done on multiple objects simultaneously. I have four (or more) tables on a page that start off hidden, and I want to toggle their...
5
by: cbs7 | last post by:
Hi all I'm a complete newbie to web development and Javascript especially. I've been creating a form for a webpage and have used a validation script gen_validatorv2.js which I downloaded from the...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
12
by: pantagruel | last post by:
Hi, I'm thinking of making a WScript based JavaScript library, I can think of some specific non-browser specific scripting examples that should probably make it in, like Crockford's little...
3
by: tshad | last post by:
Using asp.net 2.0, I am finding that at times, the old javascript will still be there. I was working with it for a couple of hours and the changes seem to happen. But at the end of the day, I...
20
by: shapper | last post by:
Hello, How to create a namespace in Javascript containing two methods? And how to access those methods? Thanks, Miguel
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.