Cookie Sets Users Language Preference

[Total: 0    Average: 0/5]

This allows you to use cookies to make a page show up in a specified language.

Head Code:

<!--
  -- This script block defines our cookie construct.
  -->
<script type=text/javascript>
function Cookie(document,name,hours,path,domain,secure) {
  // any VAR in "this" that does not start with a "$" will
  // be written into the cookie (read from also)
  this.$doc  = document
  this.$name = name
  if (hours)  this.$expiration=new Date((new Date()).getTime()+hours*3600000); else this.$expiration = null
  if (path)   this.$path   = path;                                             else this.$path       = null
  if (domain) this.$domain = domain;                                           else this.$domain     = null
  if (secure) this.$secure = true;                                             else this.$secure     = false
}

function CookieWrite() {
  var cookieval=""
  for(var prop in this) {
    if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function') || prop == '') continue
	if (cookieval != "") cookieval += '&'
	cookieval+=prop+":"+escape(this[prop])
  }
  var cookie=this.$name+"="+cookieval
  if (this.$expiration) cookie+='; expires=' + this.$expiration.toGMTString()
  if (this.$path)       cookie+='; path='    + this.$path
  if (this.$domain)     cookie+='; domain='  + this.$domain
  if (this.$secure)     cookie+='; secure'
  //alert("writting cookie="+cookie)
  this.$doc.cookie=cookie
}

function CookieRead() {
  var allcookies=this.$doc.cookie
  if (allcookies=="") {
    return false
  }
  var start= allcookies.indexOf(this.$name+'=')
  if (start== -1) {
    return false
  }
  start += this.$name.length+1
  var end=allcookies.indexOf(';',start)
  if (end == -1) end=allcookies.length
  var cookieval = allcookies.substring(start,end)
  var a = cookieval.split('&')
  for (var i=0;i<a.length;i++) a[i]=a[i].split(':')
  for (var i=0;i<a.length;i++) this[a[i][0]]=unescape(a[i][1])
  return true
}

function CookieDelete() {
  var cookie = this.$name+'='
  if (this.$path)   cookie+='; path='+this.$path
  if (this.$domain) cookie+='; domain='+this.$domain
  cookie+='; expires=Fri, 02-Jan-1970 00:00:00 GMT'  // MAKE IT EXPIRE!
  this.$doc.cookie=cookie
}

new Cookie()
Cookie.prototype.write = CookieWrite
Cookie.prototype.del   = CookieDelete
Cookie.prototype.read  = CookieRead
</script>

Body Code:

<script>
/*
 * This construct defines a lookup for 2 languages.
 */
function _LANG_OBJ(){
  this.WORDS=new Array();
  this.WORD=0; /* constant - do not change */
  this.L1=1;   /* constant - do not change */
  this.L2=2;   /* constant - do not change */
  this.DEBUG=0; /* set to 1 to turn on debug alerts */
  this.LANGUAGE_SET=this.L1; /* default to one of them - this just picks the first one */
  this.SetLang=function(_l)
  {
    /* these map language to internal counters */
    if(_l=="ENGLISH") this.LANGUAGE_SET=this.L1;
    if(_l=="SPANISH") this.LANGUAGE_SET=this.L2;
	if(this.DEBUG)alert("Language Set To:"+this.LANGUAGE_SET);
  }
  this.SetWord=function(_w,_l1,_l2)
  {
    for(var i=0;i<this.WORDS.length;i++)
	{
	  if(this.WORDS[i][this.WORD]==_w)
	  {
	    if(this.DEBUG)alert("you have defined ""+_w+"" already.");
	    return 0; /* already defined */
	  }
	}
	/* okay lets add this new word in */
    var _l=this.WORDS.length;
	this.WORDS[_l]=new Array();
	this.WORDS[_l][this.WORD]=_w;
	this.WORDS[_l][this.L1]=_l1;
	this.WORDS[_l][this.L2]=_l2;
	if(this.DEBUG)alert("Added:nIndex="+_l+"nWord:"+this.WORDS[_l][this.WORD]+"nL1:"+this.WORDS[_l][this.L1]+"nL2:"+this.WORDS[_l][this.L2]);
	return 1;
  }
  this.OutPut=function(_w)
  {
    var _ret="<I><B>[OutPut] Error : Word Look Up : Word Not Found : ""+_w+""</B></I>";
    for(var i=0;i<this.WORDS.length;i++)
	{
	  if(this.WORDS[i][this.WORD]==_w)
	  {
        if(this.DEBUG)alert("Found Word:"+_w+"nIndex="+i+"nLanguageSetting="+this.LANGUAGE_SET+"nWord:"+this.WORDS[i][this.WORD]+"nL1:"+this.WORDS[i][this.L1]+"nL2:"+this.WORDS[i][this.L2]+"nReturning:"+this.WORDS[i][this.LANGUAGE_SET]);
        _ret=this.WORDS[i][this.LANGUAGE_SET];
	  }
	}
    document.write(_ret);
  }
}

var _LANG=new _LANG_OBJ();
    _LANG.SetWord("Hi","Language 1 - Hi","Language 2 - Hi");
    _LANG.SetWord("Why","EN:Why","SP:Why");
</script>

<!--
  -- This shows how to set to use the default specified by the URL or a cookie.
  -->
<script>
var _SET="";
/* this looks for a cookie */
var myCookie = new Cookie(document,"myName",240)
if (myCookie.read() && myCookie.language) {
  _SET=myCookie.language;
}
/* this looks to see if the setting was made by a URL parameter */
/* if it was - use it over the cookie setting - and store the new value into the cookie */
var _URL=document.location.toString();
if(_URL.indexOf("LANG=1")!=-1){_SET="ENGLISH";myCookie.language=_SET;myCookie.write();}
if(_URL.indexOf("LANG=2")!=-1){_SET="SPANISH";myCookie.language=_SET;myCookie.write();}
_LANG.SetLang(_SET); /* set the language */
</script>
DEFAULT:<BR>
<table>
<tr><td>Hello Text --&gt;<script>_LANG.OutPut("Hi");</script>&lt;--</td></tr>
<tr><td>Why Text --&gt;<script>_LANG.OutPut("Why");</script>&lt;--</td></tr>
</table>
<HR>

<!--
  -- This shows how to set to use english. hard coded choice in the html.
  -->
<script>
_LANG.SetLang("ENGLISH");
</script>
ENGLISH:<BR>
<table>
<tr><td>Hello Text --&gt;<script>_LANG.OutPut("Hi");</script>&lt;--</td></tr>
<tr><td>Why Text --&gt;<script>_LANG.OutPut("Why");</script>&lt;--</td></tr>
</table>
<HR>
<!--
  -- This shows how to set to use spanish. hard coded choice in the html.
  -->
<script>
_LANG.SetLang("SPANISH");
</script>
SPANISH:<BR>
<table>
<tr><td>Hello Text --&gt;<script>_LANG.OutPut("Hi");</script>&lt;--</td></tr>
<tr><td>Why Text --&gt;<script>_LANG.OutPut("Why");</script>&lt;--</td></tr>
</table>
<HR>
Give a choice to change the default:
<script>
function goNow(_choice)
{
  var _URL=document.location.toString();
  if(_URL.indexOf("?")!=-1)_URL=_URL.substr(0,_URL.indexOf("?"));
  document.location=_URL+"?LANG="+_choice;
}
</script>
<a href="javascript:goNow(1)">ENGLISH</a> |
<a href="javascript:goNow(2)">SPANISH</a>

Times Viewed: 8

Leave a Reply

Your email address will not be published. Required fields are marked *