Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with IF sentence and navigator.userLanguage

Status
Not open for further replies.

goggel

Technical User
Oct 8, 2009
2
NO
Hi

I try to make a javascript that checks what language the user is using. But the if sentence is not running properly, it just goes down to the bottom and continiue running and not giving me correct return.


Code:
function isSecurity(chkFlag){
	if (chkFlag == "true") return true;
	if (chkFlag == "false") return false;
	return false;
}

function changeH1Class(){
	document.getElementById("blockMessage").className = "security";
}

  var l_lang;
  if (navigator.userLanguage) // Explorer
    l_lang = navigator.userLanguage;
  else if (navigator.language) // FF
    l_lang = navigator.language;
  else
    l_lang = 'test';


function writeHeaderTest(){
	if(isSecurity(securityFlag)){
		document.write(l_lang);
		changeH1Class();
	}
	else{
		document.write(l_lang);
	}
}

if(l_lang == en)
{

function writeHeader(){
	if(isSecurity(securityFlag)){
		document.write("English123");
		changeH1Class();
	}
	else{
		document.write("English456");
	}
}


function writeReason(){
	if(isSecurity(securityFlag)){
		document.write("Sites in this category may pose a security threat to network resources or private information, and are blocked by your organization.");
	}
}

function writeWarning(){
	if(isSecurity(securityFlag)){
		document.write('<span class="warning">Not Recommended</span>');
	}
}

function writeWarning1(){
	if(isSecurity(securityFlag)){
		document.write('This action is not recommended.')
	}
}
}
else if(l_lang == no)
{

function writeHeader(){
	if(isSecurity(securityFlag)){
		document.write("Test123");
		changeH1Class();
	}
	else{
		document.write("Test456");
	}
}


function writeReason(){
	if(isSecurity(securityFlag)){
		document.write("Sites in this category may pose a security threat to network resources or private information, and are blocked by your organization.");
	}
}

function writeWarning(){
	if(isSecurity(securityFlag)){
		document.write('<span class="warning">Not Recommended</span>');
	}
}

function writeWarning1(){
	if(isSecurity(securityFlag)){
		document.write('This action is not recommended.')
	}
}
}
else{
function changeH1Class(){
	document.getElementById("blockMessage").className = "security";
}

function writeHeader(){
	if(isSecurity(securityFlag)){
		document.write("Norsk 123");
		changeH1Class();
	}
	else{
		document.write("Norsk 456");
	}
}

function writeReason(){
	if(isSecurity(securityFlag)){
		document.write("Los sitios de esta categoría pueden representar una amenaza a la seguridad de los recursos de la red o a información privada y, por lo tanto, su organización los bloquea.");
	}
}

function writeWarning(){
	if(isSecurity(securityFlag)){
		document.write('<span class="warning">No recomendado</span>');
	}
}

function writeWarning1(){
	if(isSecurity(securityFlag)){
		document.write('Esta acción no se recomienda.')
	}
}
}
 
Hi

You are talking about this lines, right ?
goggel said:
Code:
if(l_lang == en)
[gray]// (...)[/gray]
else if(l_lang == no)
Where you defined the en and no variables ? I guess nowhere. In that case change those lines to :
Code:
[b]if[/b][teal]([/teal]l_lang [teal]==[/teal] [green][i][highlight]'[/highlight]en[highlight]'[/highlight][/i][/green][teal])[/teal]
[gray]// (...)[/gray]
[b]else[/b] [b]if[/b][teal]([/teal]l_lang [teal]==[/teal] [green][i][highlight]'[/highlight]no[highlight]'[/highlight][/i][/green][teal])[/teal]
But note that the language code may be followed by region code. For example mine is "en-US". So better change the test to :
Code:
[b]if[/b][teal]([/teal]l_lang[highlight][teal].[/teal][COLOR=darkgoldenrod]substr[/color][teal]([/teal][purple]0[/purple][teal],[/teal][purple]2[/purple][teal])[/teal][/highlight] [teal]==[/teal] [green][i]'en'[/i][/green][teal])[/teal]
[gray]// (...)[/gray]
[b]else[/b] [b]if[/b][teal]([/teal]l_lang[highlight][teal].[/teal][COLOR=darkgoldenrod]substr[/color][teal]([/teal][purple]0[/purple][teal],[/teal][purple]2[/purple][teal])[/teal][/highlight] [teal]==[/teal] [green][i]'no'[/i][/green][teal])[/teal]


Feherke.
 
You could also do something like this:

Code:
if(l_lang.indexOf('en') > -1)
// (..)
else if (l_lang.indexOf('no') > -1)
 
Hi, I have now tried both of the solutions. One thing that I have forgotten to mention is that I am using IE8 on my computer. I don't know if that makes some kind of trouble?
I don't think it's a problem with the if sentences anymore, I tried to use firefox on a diffrent computer that is using en-US and it did work. So I got some questions that might lead me the correct way.

1: Where does these commands get the language setting from, in IE or Regional Settings?
2: Is it diffrent commands in IE 8 compared to older IE versions (don't have any older IE version available atm but will try to get it today).
3: It seems to me no matter what language setting I'm changing its still the same language, could it be cached somewhere so when I change my local language setting the old setting is cached somewhere?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top