Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 5.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-15-2009, 03:09 AM   PM User | #1
skyonline
New Coder

 
Join Date: Dec 2009
Posts: 36
Thanks: 31
Thanked 0 Times in 0 Posts
skyonline is an unknown quantity at this point
text Translator

hai....any of you got free javascripts text Translator code...i really need it ASAP..tq..

i got one but it not work very good when i put 1000++ words...example :

Code:
<script type="text/javascript"> 
function translate(phrase) { 
   phrase = document.getElementById('input').value; 
   phrase = phrase.replace(/why/g, "y"); 
   phrase = phrase.replace(/are/g, "r"); 
   phrase = phrase.replace(/you/g, "u"); 
   phrase = phrase.replace(/for/g, "4"); 
   phrase = phrase.replace(/to/g, "2"); 
   document.getElementById('output').value = phrase; 
} 
</script> 

<textarea id="input"></textarea><br /> 
<input type="button" value="translate" onClick="translate();" /><br /> 
<textarea id="output"></textarea>

Last edited by Trinithis; 12-15-2009 at 09:53 AM..
skyonline is offline   Reply With Quote
Old 12-15-2009, 04:38 AM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Question More information please ...

What's the problem with the script ... too slow with 1000++ words?

Note: if 'CASE' is a problem, add this...
Code:
  phrase = phrase.replace(/why/ig, "y");
to each statment.
jmrker is offline   Reply With Quote
Users who have thanked jmrker for this post:
skyonline (12-16-2009)
Old 12-15-2009, 06:18 AM   PM User | #3
skyonline
New Coder

 
Join Date: Dec 2009
Posts: 36
Thanks: 31
Thanked 0 Times in 0 Posts
skyonline is an unknown quantity at this point
Quote:
Originally Posted by jmrker View Post
What's the problem with the script ... too slow with 1000++ words?

Note: if 'CASE' is a problem, add this...
Code:
  phrase = phrase.replace(/why/ig, "y");
to each statment.
no..ist not about slow...try this and copy on frontpage and preview and tyep peradaban...the result is perhaveban... its not same with true result..the true answer peradaban = civilization... phrase = phrase.replace(/peradaban/ig, "civilization");....why..? its hapen when i put more 1000 ++ words...and many words on list its not same result..

Code:
<script type="text/javascript">
function translate(phrase) {
   phrase = document.getElementById('input').value;
phrase = phrase.replace(/ambung/ig,   "back-basket");
phrase = phrase.replace(/amin/ig,   "amen");
phrase = phrase.replace(/amir/ig,   "emir");
phrase = phrase.replace(/ampai/ig,   "hang");
phrase = phrase.replace(/ampai-ampai/ig,   "jellyfish");
phrase = phrase.replace(/mengampaikan/ig,   "to hang clothes out to dry");
phrase = phrase.replace(/terampai/ig,   "hung to air");
phrase = phrase.replace(/ampaian/ig,   "clothes-line");
phrase = phrase.replace(/ampu/ig,   "ingratiate");
phrase = phrase.replace(/mengampu/ig,   "to ingratiate");
phrase = phrase.replace(/pengampu/ig,   "sycophant");
phrase = phrase.replace(/ampun/ig,   "pardon");
phrase = phrase.replace(/keampunan/ig,   "forgiveness");
phrase = phrase.replace(/mengampunkan/ig,   "to forgive");
phrase = phrase.replace(/pengampun/ig,   "one who forgives");
phrase = phrase.replace(/pengampunan/ig,   "amnesty");
phrase = phrase.replace(/amuk/ig,   "amuck");
phrase = phrase.replace(/mengamuk/ig,   "attack without thinking");
phrase = phrase.replace(/pengamuk/ig,   "person who runs amuck");
phrase = phrase.replace(/anai-anai/ig,   "termite");
phrase = phrase.replace(/anak/ig,   "child");
phrase = phrase.replace(/anak angkat/ig,   "adopted child");
phrase = phrase.replace(/anak bulan/ig,   "new moon");
phrase = phrase.replace(/anak dara/ig,   "virgin");
phrase = phrase.replace(/anak merah/ig,   "newborn");
phrase = phrase.replace(/anak-beranak/ig,   "together with the children");
phrase = phrase.replace(/anak-anak/ig,   "many children");
phrase = phrase.replace(/anak-anakan/ig,   "doll");
phrase = phrase.replace(/anakanda/ig,   "son");
phrase = phrase.replace(/beranak/ig,   "to bear a child");
phrase = phrase.replace(/keanak-anakan/ig,   "childish");
phrase = phrase.replace(/peranakan/ig,   "native");
phrase = phrase.replace(/anasir/ig,   "an element");
phrase = phrase.replace(/anasir jahat/ig,   "bad social element");
phrase = phrase.replace(/ancam/ig,   "menace");
phrase = phrase.replace(/mengancam/ig,   "to threaten");
phrase = phrase.replace(/ancaman/ig,   "a threat");
phrase = phrase.replace(/terancam/ig,   "threatened");
phrase = phrase.replace(/anda/ig,   "you");
phrase = phrase.replace(/andai/ig,   "if");
phrase = phrase.replace(/andainya/ig,   "suppose");
phrase = phrase.replace(/andaian/ig,   "assumption");
phrase = phrase.replace(/berandai/ig,   "to discuss");
phrase = phrase.replace(/mengandaikan/ig,   "suppose");
phrase = phrase.replace(/andam/ig,   "beautician who does the make-up for the bride and bridegroom");
phrase = phrase.replace(/mengandam/ig,   "to make-up the bride and bridegroom");
phrase = phrase.replace(/andas/ig,   "anvil");
phrase = phrase.replace(/anduh/ig,   "suspend by means of a sling");
phrase = phrase.replace(/aneh/ig,   "bizarre");
phrase = phrase.replace(/aneka/ig,   "multifarious");
phrase = phrase.replace(/aneka ragam/ig,   "variety show");
phrase = phrase.replace(/beraneka/ig,   "having a variety of");
phrase = phrase.replace(/anekarama/ig,   "concert");
phrase = phrase.replace(/angan/ig,   "imagination");
phrase = phrase.replace(/angan-angan/ig,   "imagination");
phrase = phrase.replace(/berangan-angan/ig,   "to day-dream");
phrase = phrase.replace(/mengangan-angankan/ig,   "to wish for");
phrase = phrase.replace(/terangan-angan/ig,   "to constantly wish for");
phrase = phrase.replace(/angau/ig,   "lovesick");
phrase = phrase.replace(/anggap/ig,   "assume");
phrase = phrase.replace(/menganggap/ig,   "to assume");
phrase = phrase.replace(/anggapan/ig,   "assumption");
phrase = phrase.replace(/beranggapan/ig,   "of the opinion");
phrase = phrase.replace(/anggar/ig,   "estimate");
phrase = phrase.replace(/anggaran/ig,   "estimation");
phrase = phrase.replace(/menganggarkan/ig,   "to estimate");
phrase = phrase.replace(/anggerik/ig,   "orchid");
phrase = phrase.replace(/anggota/ig,   "limb");
phrase = phrase.replace(/beranggota/ig,   "having limbs");
phrase = phrase.replace(/anggota badan/ig,   "part of the body");
phrase = phrase.replace(/keanggotaan/ig,   "membership");
phrase = phrase.replace(/menganggotai/ig,   "to be a member of");
phrase = phrase.replace(/angguk/ig,   "nod");
phrase = phrase.replace(/anggukan/ig,   "act of nodding");
phrase = phrase.replace(/mengangguk/ig,   "to nod");
phrase = phrase.replace(/terangguk-angguk/ig,   "to keep nodding");
phrase = phrase.replace(/anggur/ig,   "grape");
phrase = phrase.replace(/menganggur/ig,   "unemployed");
phrase = phrase.replace(/penganggur/ig,   "one who is unemployed");
phrase = phrase.replace(/pengangguran/ig,   "unemployment");
phrase = phrase.replace(/angin/ig,   "wind");
phrase = phrase.replace(/berangin/ig,   "windy");
phrase = phrase.replace(/mengangin/ig,   "to winnow");
phrase = phrase.replace(/menganginkan/ig,   "to put in a windy place");
phrase = phrase.replace(/peranginan/ig,   "holiday resort");
phrase = phrase.replace(/angka/ig,   "numerical figure");
phrase = phrase.replace(/perangkaan/ig,   "statistics");
phrase = phrase.replace(/angkara/ig,   "bad");
phrase = phrase.replace(/angkasa/ig,   "space");
phrase = phrase.replace(/angkasawan/ig,   "astronaut");
phrase = phrase.replace(/angkat/ig,   "lift");
phrase = phrase.replace(/angkatan/ig,   "troops");
phrase = phrase.replace(/berangkat/ig,   "to depart");
phrase = phrase.replace(/keberangkatan/ig,   "departure");
phrase = phrase.replace(/angkuh/ig,   "haughty");
phrase = phrase.replace(/keangkuhan/ig,   "arrogance");
phrase = phrase.replace(/angkup/ig,   "callipers");
phrase = phrase.replace(/angkut/ig,   "convey");
phrase = phrase.replace(/mengangkut/ig,   "to transport");
phrase = phrase.replace(/berangkut/ig,   "to transport several times");
phrase = phrase.replace(/pengangkutan/ig,   "transportation");
phrase = phrase.replace(/angsa/ig,   "goose");
phrase = phrase.replace(/aniaya/ig,   "maltreatment");
phrase = phrase.replace(/menganiayai/ig,   "to oppress");
phrase = phrase.replace(/penganiaya/ig,   "oppressor");
phrase = phrase.replace(/penganiayaan/ig,   "oppression");
phrase = phrase.replace(/anjak/ig,   "shift position slightly");
phrase = phrase.replace(/beranjak/ig,   "to shift position slightly");
phrase = phrase.replace(/menganjakkan/ig,   "to change the position of");
phrase = phrase.replace(/anjal/ig,   "elastic");
phrase = phrase.replace(/keanjalan/ig,   "flexibility");
phrase = phrase.replace(/menganjal/ig,   "to be elastic");
phrase = phrase.replace(/anjing/ig,   "dog");
phrase = phrase.replace(/anjung/ig,   "projecting facade");
phrase = phrase.replace(/anjungan/ig,   "bridge");
phrase = phrase.replace(/anjur/ig,   "to jut out");
phrase = phrase.replace(/menganjur/ig,   "to jut out");
phrase = phrase.replace(/anjuran/ig,   "proposal");
phrase = phrase.replace(/menganjurkan/ig,   "to propose");
phrase = phrase.replace(/penganjur/ig,   "organizer");
phrase = phrase.replace(/ansur/ig,   "progress");
phrase = phrase.replace(/beransur/ig,   "to progress in stages");
phrase = phrase.replace(/ansuran/ig,   "instalment");
phrase = phrase.replace(/mengansurkan/ig,   "to pay by instalments");
phrase = phrase.replace(/antah/ig,   "rice grains left unhusked after pounding");
phrase = phrase.replace(/antan/ig,   "pestle");
phrase = phrase.replace(/antara/ig,   "among");
phrase = phrase.replace(/mengantarai/ig,   "to mediate");
phrase = phrase.replace(/perantara/ig,   "mediator");
phrase = phrase.replace(/perantaraan/ig,   "mediation");
phrase = phrase.replace(/antarabangsa/ig,   "international");
phrase = phrase.replace(/antero/ig,   "whole");
phrase = phrase.replace(/seantero/ig,   "the whole");
phrase = phrase.replace(/anti/ig,   "opposed to");
phrase = phrase.replace(/anting/ig,   "pendulum");
phrase = phrase.replace(/abai/ig,   "neglect");
phrase = phrase.replace(/mengabaikan/ig,   "to disregard");
phrase = phrase.replace(/abang/ig,   "elder brother");
phrase = phrase.replace(/abang angkat/ig,   "foster brother");
phrase = phrase.replace(/abang ipar/ig,   "brother-in-law");
phrase = phrase.replace(/abang tiri/ig,   "stepbrother");
phrase = phrase.replace(/abdi/ig,   "slave");
phrase = phrase.replace(/mengabdikan/ig,   "to enslave");
phrase = phrase.replace(/pengabdian/ig,   "slavery");
phrase = phrase.replace(/abjad/ig,   "alphabet");
phrase = phrase.replace(/abu/ig,   "ash");
phrase = phrase.replace(/mengabui/ig,   "to cheat");
phrase = phrase.replace(/abuk/ig,   "dust");
phrase = phrase.replace(/abung/ig,   "pomelo");
phrase = phrase.replace(/acah/ig,   "tease");
phrase = phrase.replace(/mengacah-acah/ig,   "to tease");
phrase = phrase.replace(/acapkali/ig,   "often");
phrase = phrase.replace(/acar/ig,   "pickle");
phrase = phrase.replace(/acara/ig,   "event");
phrase = phrase.replace(/mengacarakan/ig,   "to compere");
phrase = phrase.replace(/pengacara/ig,   "master of ceremonies");
phrase = phrase.replace(/aci/ig,   "shaft");
phrase = phrase.replace(/acu/ig,   "to direct weapon at target");
phrase = phrase.replace(/mengacukan/ig,   "to aim weapon at target");
phrase = phrase.replace(/mengacu/ig,   "to test");
phrase = phrase.replace(/acuan/ig,   "a mould");
phrase = phrase.replace(/acuh/ig,   "to care for");
phrase = phrase.replace(/mengacuhkan/ig,   "to care for");
phrase = phrase.replace(/acuh tak acuh/ig,   "to show indifference");
phrase = phrase.replace(/acum/ig,   "provoke");
phrase = phrase.replace(/mengacum/ig,   "to provoke");
phrase = phrase.replace(/acuman/ig,   "provocation");
phrase = phrase.replace(/pengacum/ig,   "instigator");
phrase = phrase.replace(/pengacuman/ig,   "agitation");
phrase = phrase.replace(/ada/ig,   "have");
phrase = phrase.replace(/adakala/ig,   "sometimes");
phrase = phrase.replace(/adakalanya/ig,   "sometimes");
phrase = phrase.replace(/adalah/ig,   "is");
phrase = phrase.replace(/adapun/ig,   "with regard to");
phrase = phrase.replace(/berada/ig,   "rich");
phrase = phrase.replace(/diadakan/ig,   "to be held");
phrase = phrase.replace(/keadaan/ig,   "condition");
phrase = phrase.replace(/mengada-ngada/ig,   "to boast");
phrase = phrase.replace(/mengada-ngadakan/ig,   "to concoct an excuse");
phrase = phrase.replace(/mengadakan/ig,   "to hold");
phrase = phrase.replace(/adab/ig,   "politeness");
phrase = phrase.replace(/beradab/ig,   "courteous");
phrase = phrase.replace(/peradaban/ig,   "civilization");
   document.getElementById('output').value = phrase;
}
</script>

<textarea id="input"></textarea><br />
<input type="button" value="translate" onClick="translate();" /><br />
<textarea id="output"></textarea>

Last edited by Trinithis; 12-15-2009 at 09:53 AM..
skyonline is offline   Reply With Quote
Old 12-15-2009, 07:04 AM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
here a simpler way to do it for most major languages:

live demo
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>	<title>Dan's translate</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="author" content="dandavis" />

<style type='text/css'>
	body {font-family: tahoma, helvetica, san-serif; color: menutext; background-color: menu; }
	#dump { padding-right: 10em; border: 3px solid Black; 
		top: 3em; left: 1em; width: 80%; height: 88%; 
		position: absolute; overflow:auto; background-color: AliceBlue;
	}
</style>	
</head>
<body>
<label for='lang'> Language:
 <select size='1' id='lang' name="lang" onchange="var si=this.selectedIndex; App.lc=this.options[si].value; ">
  <option value="es">Spanish</option>
  <option value="zh-CN">Chinese (Simplified)</option><option value="ru">Russian</option>
  <option value="fr">French</option><option value="el">Greek</option><option value="sq">Albanian</option>
  <option value="ar">Arabic</option><option value="bg">Bulgarian</option><option value="ca">Catalan</option>
  <option value="zh-TW">Chinese (Traditional)</option><option value="hr">Croatian</option>
  <option value="cs">Czech</option><option value="da">Danish</option><option value="nl">Dutch</option>
  <option value="et">Estonian</option><option value="tl">Filipino</option><option value="fi">Finnish</option>
  <option value="gl">Galician</option><option value="de">German</option><option value="iw">Hebrew</option>
  <option value="hi">Hindi</option><option value="hu">Hungarian</option><option value="id">Indonesian</option>
  <option value="it">Italian</option><option value="ja">Japanese</option><option value="ko">Korean</option>
  <option value="lv">Latvian</option><option value="lt">Lithuanian</option><option value="mt">Maltese</option>
  <option value="no">Norwegian</option><option value="pl">Polish</option><option value="pt">Portuguese</option>
  <option value="ro">Romanian</option><option value="sr">Serbian</option><option value="sk">Slovak</option>
  <option value="sl">Slovenian</option><option value="sv">Swedish</option><option value="th">Thai</option>
  <option value="tr">Turkish</option><option value="uk">Ukrainian</option><option value="vi">Vietnamese</option>
</select>
</label>	

&nbsp;
<input type="button" value="translate!" onclick="doTranslate()" />

<br />
<textarea id="dump">Hello World!</textarea>

<script type='text/javascript'>

var App={ 
  lc:  "es",
   transUrl:  "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&callback=incoming&q=",
 enc: (escape||window.encodeURIComponent || window.escape)
};

function doTranslate(){ //do request
	el("lang").onchange();
	el("dump").style.backgroundColor="#666";
	document.title="Translating...";
	jsGet(  App.transUrl + App.enc( el("dump").value ) + "&context=" + "dd" + "&langpair=en%7C"+App.lc );	
}//end doTranslate()


function incoming(id, data, status){ //displays the translated text
	el("dump").style.backgroundColor="";
	document.title="Dan's translate";
	if(status!=200){return;}
 	el("dump").value=data.translatedText||el("dump").value;
}//end incoming()

//support:
function el(tid) {return document.getElementById(tid);}
function jsGet(turl) { 
  var d=document, xJs = d.createElement("script");
  d.getElementsByTagName("head")[0].appendChild(xJs); xJs.src = turl;
}//end jsGet()

</script>
</body>
</html>
tested ff3,ie8,c3
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%

Last edited by rnd me; 12-15-2009 at 07:07 AM..
rnd me is offline   Reply With Quote
Users who have thanked rnd me for this post:
skyonline (12-16-2009)
Old 12-15-2009, 07:15 AM   PM User | #5
skyonline
New Coder

 
Join Date: Dec 2009
Posts: 36
Thanks: 31
Thanked 0 Times in 0 Posts
skyonline is an unknown quantity at this point
its use google right...i want to make it offline...because i got my own text...i only need jasvascripts
skyonline is offline   Reply With Quote
Old 12-15-2009, 08:02 AM   PM User | #6
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by skyonline View Post
its use google right...i want to make it offline...because i got my own text...i only need jasvascripts
Code:
//data:
var reps={ "ambung":"back-basket",  "amin":"amen",  "amir":"emir",  "ampai":"hang",  "ampai-ampai":"jellyfish",  "mengampaikan":"to hang clothes out to dry",  "terampai":"hung to air",  "ampaian":"clothes-line",  "ampu":"ingratiate",  "mengampu":"to ingratiate",  "pengampu":"sycophant",  "ampun":"pardon",  "keampunan":"forgiveness",  "mengampunkan":"to forgive",  "pengampun":"one who forgives",  "pengampunan":"amnesty",  "amuk":"amuck",  "mengamuk":"attack without thinking",  "pengamuk":"person who runs amuck",  "anai-anai":"termite",  "anak":"child",  "anak angkat":"adopted child",  "anak bulan":"new moon",  "anak dara":"virgin",  "anak merah":"newborn",  "anak-beranak":"together with the children",  "anak-anak":"many children",  "anak-anakan":"doll",  "anakanda":"son",  "beranak":"to bear a child",  "keanak-anakan":"childish",  "peranakan":"native",  "anasir":"an element",  "anasir jahat":"bad social element",  "ancam":"menace",  "mengancam":"to threaten",  "ancaman":"a threat",  "terancam":"threatened",  "anda":"you",  "andai":"if",  "andainya":"suppose",  "andaian":"assumption",  "berandai":"to discuss",  "mengandaikan":"suppose",  "andam":"beautician who does the make-up for the bride and bridegroom",  "mengandam":"to make-up the bride and bridegroom",  "andas":"anvil",  "anduh":"suspend by means of a sling",  "aneh":"bizarre",  "aneka":"multifarious",  "aneka ragam":"variety show",  "beraneka":"having a variety of",  "anekarama":"concert",  "angan":"imagination",  "angan-angan":"imagination",  "berangan-angan":"to day-dream",  "mengangan-angankan":"to wish for",  "terangan-angan":"to constantly wish for",  "angau":"lovesick",  "anggap":"assume",  "menganggap":"to assume",  "anggapan":"assumption",  "beranggapan":"of the opinion",  "anggar":"estimate",  "anggaran":"estimation",  "menganggarkan":"to estimate",  "anggerik":"orchid",  "anggota":"limb",  "beranggota":"having limbs",  "anggota badan":"part of the body",  "keanggotaan":"membership",  "menganggotai":"to be a member of",  "angguk":"nod",  "anggukan":"act of nodding",  "mengangguk":"to nod",  "terangguk-angguk":"to keep nodding",  "anggur":"grape",  "menganggur":"unemployed",  "penganggur":"one who is unemployed",  "pengangguran":"unemployment",  "angin":"wind",  "berangin":"windy",  "mengangin":"to winnow",  "menganginkan":"to put in a windy place",  "peranginan":"holiday resort",  "angka":"numerical figure",  "perangkaan":"statistics",  "angkara":"bad",  "angkasa":"space",  "angkasawan":"astronaut",  "angkat":"lift",  "angkatan":"troops",  "berangkat":"to depart",  "keberangkatan":"departure",  "angkuh":"haughty",  "keangkuhan":"arrogance",  "angkup":"callipers",  "angkut":"convey",  "mengangkut":"to transport",  "berangkut":"to transport several times",  "pengangkutan":"transportation",  "angsa":"goose",  "aniaya":"maltreatment",  "menganiayai":"to oppress",  "penganiaya":"oppressor",  "penganiayaan":"oppression",  "anjak":"shift position slightly",  "beranjak":"to shift position slightly",  "menganjakkan":"to change the position of",  "anjal":"elastic",  "keanjalan":"flexibility",  "menganjal":"to be elastic",  "anjing":"dog",  "anjung":"projecting facade",  "anjungan":"bridge",  "anjur":"to jut out",  "menganjur":"to jut out",  "anjuran":"proposal",  "menganjurkan":"to propose",  "penganjur":"organizer",  "ansur":"progress",  "beransur":"to progress in stages",  "ansuran":"instalment",  "mengansurkan":"to pay by instalments",  "antah":"rice grains left unhusked after pounding",  "antan":"pestle",  "antara":"among",  "mengantarai":"to mediate",  "perantara":"mediator",  "perantaraan":"mediation",  "antarabangsa":"international",  "antero":"whole",  "seantero":"the whole",  "anti":"opposed to",  "anting":"pendulum",  "abai":"neglect",  "mengabaikan":"to disregard",  "abang":"elder brother",  "abang angkat":"foster brother",  "abang ipar":"brother-in-law",  "abang tiri":"stepbrother",  "abdi":"slave",  "mengabdikan":"to enslave",  "pengabdian":"slavery",  "abjad":"alphabet",  "abu":"ash",  "mengabui":"to cheat",  "abuk":"dust",  "abung":"pomelo",  "acah":"tease",  "mengacah-acah":"to tease",  "acapkali":"often",  "acar":"pickle",  "acara":"event",  "mengacarakan":"to compere",  "pengacara":"master of ceremonies",  "aci":"shaft",  "acu":"to direct weapon at target",  "mengacukan":"to aim weapon at target",  "mengacu":"to test",  "acuan":"a mould",  "acuh":"to care for",  "mengacuhkan":"to care for",  "acuh tak acuh":"to show indifference",  "acum":"provoke",  "mengacum":"to provoke",  "acuman":"provocation",  "pengacum":"instigator",  "pengacuman":"agitation",  "ada":"have",  "adakala":"sometimes",  "adakalanya":"sometimes",  "adalah":"is",  "adapun":"with regard to",  "berada":"rich",  "diadakan":"to be held",  "keadaan":"condition",  "mengada-ngada":"to boast",  "mengada-ngadakan":"to concoct an excuse",  "mengadakan":"to hold",  "adab":"politeness",  "beradab":"courteous",  "peradaban":"civilization"};

//engine:
function repAll(str, reps){
 var buff=String(str);
 for(i in reps){if(reps.hasOwnProperty(i) && (phrase.indexOf(i)!==-1)){
  buff=buff.replace(new RegExp("\\b"+i.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1')+"\\b", "ig"),reps[i]);
 }}
 return buff;
}


//test:
var phrase="Say amin peradaban !"
alert(repAll(phrase, reps));//shows: "Say amen civilization !"
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%

Last edited by rnd me; 12-15-2009 at 08:05 AM..
rnd me is offline   Reply With Quote
Users who have thanked rnd me for this post:
skyonline (12-16-2009)
Old 12-15-2009, 08:09 AM   PM User | #7
skyonline
New Coder

 
Join Date: Dec 2009
Posts: 36
Thanks: 31
Thanked 0 Times in 0 Posts
skyonline is an unknown quantity at this point
Quote:
Originally Posted by rnd me View Post
Code:
//data:
var reps={ "ambung":"back-basket",  "amin":"amen",  "amir":"emir",  "ampai":"hang",  "ampai-ampai":"jellyfish",  "mengampaikan":"to hang clothes out to dry",  "terampai":"hung to air",  "ampaian":"clothes-line",  "ampu":"ingratiate",  "mengampu":"to ingratiate",  "pengampu":"sycophant",  "ampun":"pardon",  "keampunan":"forgiveness",  "mengampunkan":"to forgive",  "pengampun":"one who forgives",  "pengampunan":"amnesty",  "amuk":"amuck",  "mengamuk":"attack without thinking",  "pengamuk":"person who runs amuck",  "anai-anai":"termite",  "anak":"child",  "anak angkat":"adopted child",  "anak bulan":"new moon",  "anak dara":"virgin",  "anak merah":"newborn",  "anak-beranak":"together with the children",  "anak-anak":"many children",  "anak-anakan":"doll",  "anakanda":"son",  "beranak":"to bear a child",  "keanak-anakan":"childish",  "peranakan":"native",  "anasir":"an element",  "anasir jahat":"bad social element",  "ancam":"menace",  "mengancam":"to threaten",  "ancaman":"a threat",  "terancam":"threatened",  "anda":"you",  "andai":"if",  "andainya":"suppose",  "andaian":"assumption",  "berandai":"to discuss",  "mengandaikan":"suppose",  "andam":"beautician who does the make-up for the bride and bridegroom",  "mengandam":"to make-up the bride and bridegroom",  "andas":"anvil",  "anduh":"suspend by means of a sling",  "aneh":"bizarre",  "aneka":"multifarious",  "aneka ragam":"variety show",  "beraneka":"having a variety of",  "anekarama":"concert",  "angan":"imagination",  "angan-angan":"imagination",  "berangan-angan":"to day-dream",  "mengangan-angankan":"to wish for",  "terangan-angan":"to constantly wish for",  "angau":"lovesick",  "anggap":"assume",  "menganggap":"to assume",  "anggapan":"assumption",  "beranggapan":"of the opinion",  "anggar":"estimate",  "anggaran":"estimation",  "menganggarkan":"to estimate",  "anggerik":"orchid",  "anggota":"limb",  "beranggota":"having limbs",  "anggota badan":"part of the body",  "keanggotaan":"membership",  "menganggotai":"to be a member of",  "angguk":"nod",  "anggukan":"act of nodding",  "mengangguk":"to nod",  "terangguk-angguk":"to keep nodding",  "anggur":"grape",  "menganggur":"unemployed",  "penganggur":"one who is unemployed",  "pengangguran":"unemployment",  "angin":"wind",  "berangin":"windy",  "mengangin":"to winnow",  "menganginkan":"to put in a windy place",  "peranginan":"holiday resort",  "angka":"numerical figure",  "perangkaan":"statistics",  "angkara":"bad",  "angkasa":"space",  "angkasawan":"astronaut",  "angkat":"lift",  "angkatan":"troops",  "berangkat":"to depart",  "keberangkatan":"departure",  "angkuh":"haughty",  "keangkuhan":"arrogance",  "angkup":"callipers",  "angkut":"convey",  "mengangkut":"to transport",  "berangkut":"to transport several times",  "pengangkutan":"transportation",  "angsa":"goose",  "aniaya":"maltreatment",  "menganiayai":"to oppress",  "penganiaya":"oppressor",  "penganiayaan":"oppression",  "anjak":"shift position slightly",  "beranjak":"to shift position slightly",  "menganjakkan":"to change the position of",  "anjal":"elastic",  "keanjalan":"flexibility",  "menganjal":"to be elastic",  "anjing":"dog",  "anjung":"projecting facade",  "anjungan":"bridge",  "anjur":"to jut out",  "menganjur":"to jut out",  "anjuran":"proposal",  "menganjurkan":"to propose",  "penganjur":"organizer",  "ansur":"progress",  "beransur":"to progress in stages",  "ansuran":"instalment",  "mengansurkan":"to pay by instalments",  "antah":"rice grains left unhusked after pounding",  "antan":"pestle",  "antara":"among",  "mengantarai":"to mediate",  "perantara":"mediator",  "perantaraan":"mediation",  "antarabangsa":"international",  "antero":"whole",  "seantero":"the whole",  "anti":"opposed to",  "anting":"pendulum",  "abai":"neglect",  "mengabaikan":"to disregard",  "abang":"elder brother",  "abang angkat":"foster brother",  "abang ipar":"brother-in-law",  "abang tiri":"stepbrother",  "abdi":"slave",  "mengabdikan":"to enslave",  "pengabdian":"slavery",  "abjad":"alphabet",  "abu":"ash",  "mengabui":"to cheat",  "abuk":"dust",  "abung":"pomelo",  "acah":"tease",  "mengacah-acah":"to tease",  "acapkali":"often",  "acar":"pickle",  "acara":"event",  "mengacarakan":"to compere",  "pengacara":"master of ceremonies",  "aci":"shaft",  "acu":"to direct weapon at target",  "mengacukan":"to aim weapon at target",  "mengacu":"to test",  "acuan":"a mould",  "acuh":"to care for",  "mengacuhkan":"to care for",  "acuh tak acuh":"to show indifference",  "acum":"provoke",  "mengacum":"to provoke",  "acuman":"provocation",  "pengacum":"instigator",  "pengacuman":"agitation",  "ada":"have",  "adakala":"sometimes",  "adakalanya":"sometimes",  "adalah":"is",  "adapun":"with regard to",  "berada":"rich",  "diadakan":"to be held",  "keadaan":"condition",  "mengada-ngada":"to boast",  "mengada-ngadakan":"to concoct an excuse",  "mengadakan":"to hold",  "adab":"politeness",  "beradab":"courteous",  "peradaban":"civilization"};

//engine:
function repAll(str, reps){
 var buff=String(str);
 for(i in reps){if(reps.hasOwnProperty(i) && (phrase.indexOf(i)!==-1)){
  buff=buff.replace(new RegExp("\\b"+i.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1')+"\\b", "ig"),reps[i]);
 }}
 return buff;
}


//test:
var phrase="Say amin peradaban !"
alert(repAll(phrase, reps));//shows: "Say amen civilization !"

how to use it..??...i test on frontpage..but is not working...can u explain how to use..
skyonline is offline   Reply With Quote
Old 12-15-2009, 08:15 AM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Your problem is nothing to do with how many words, but rather when one word is embedded within another (who put the age in manager? ). The greater the number of words in your dictionary the greater the chance that this will happen.

phrase = phrase.replace(/ada/ig, "have");
peradaban...the result is perhaveban

To cause your regex to operate only on whole words you need to add the word boundary \b to each word:-

phrase = phrase.replace(/\bada\b/ig, "have");


In last month's article about beer and the many breweries in Belgium, 'rapist monks' should have read 'Trappist monks'. - Woking Review
Philip M is offline   Reply With Quote
Old 12-15-2009, 08:53 AM   PM User | #9
skyonline
New Coder

 
Join Date: Dec 2009
Posts: 36
Thanks: 31
Thanked 0 Times in 0 Posts
skyonline is an unknown quantity at this point
wow!!! its work..TQ to all and Philip M.....and i have one more question....how to make in input text and result text area on this script have copy,paste,salect all and clear text button (function)..??

this is the scripts :

Code:
<script type="text/javascript">
function translate(phrase) {
   phrase = document.getElementById('input').value;
phrase = phrase.replace(/\babadi\b/ig,   "eternal");
phrase = phrase.replace(/\bkeabadian\b/ig,   "immortality");
phrase = phrase.replace(/\bmengabadikan\b/ig,   "perpetuate");
phrase = phrase.replace(/\babah\b/ig,   "father");
phrase = phrase.replace(/\babah-abah\b/ig,   "harness");
   document.getElementById('output').value = phrase;
}
</script>

<textarea id="input"></textarea><br />
<input type="button" value="translate" onClick="translate();" /><br />
<textarea id="output"></textarea>

Last edited by Trinithis; 12-15-2009 at 09:53 AM..
skyonline is offline   Reply With Quote
Old 12-15-2009, 09:17 AM   PM User | #10
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by skyonline View Post
wow!!! its work..TQ to all and Philip M.....and i have one more question....how to make in result text area on this script have copy,paste,salect all and clear text button
Code:
<script type="text/javascript">
function CopyToClipboard() {
CopiedTxt = document.selection.createRange();
CopiedTxt.execCommand("Copy");
}

function PasteFromClipboard(){  
document.myForm.txtArea.focus();
PastedText = document.myForm.txtArea.createTextRange();
PastedText.execCommand("Paste");
} 
</script>

<form name="myForm">
Select this text, copy it using the copy button, and paste it below.<br /><br />
<textarea id="txtArea" cols="60" rows="5">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ipsum leo, scelerisque at dapibus ac, consectetur vel ipsum. Morbi et metus ut diam molestie ullamcorper. Suspendisse rutrum semper semper. Donec volutpat neque in lorem tempus scelerisque. </textarea>
<br />
<input type="button" onClick="CopyToClipboard()" value="Copy to clipboard" />
<input type="button" onClick="PasteFromClipboard()" value="Paste from clipboard" /> 
<input type="button" value="Highlight Text" onClick="javascript:this.form.txtArea.focus();this.form.txtArea.select();"> 
<input type="button" value = "Clear The Text" onclick = "document.getElementById('txtArea').value = '' "> 

</form>
But this copy/paste code will only work in IE.

Note that there is a security issue with data stored on the clipboard, and many people choose to disable
"Allow paste operations via script" to prevent access from the internet.

Last edited by Philip M; 12-15-2009 at 10:04 AM..
Philip M is offline   Reply With Quote
Old 12-15-2009, 09:33 AM   PM User | #11
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by skyonline View Post
how to use it..??...i test on frontpage..but is not working...can u explain how to use..
sorry about that; i had a nice explanation all written and a "databaseError" discarded my whole post with tested code.

i recovered most of the code and dumped it, as i was in a hurry.
i missed a type-o trying to hurry...

basically, i said you should break up your app into chunks; separating the content from the program code.
this makes it slimmer, and lets you optimize the code in a single place.
i gave it a big performance boost by running String.indexOf to check to see if the slow replace process and regexp construction should even be bothered with.

since indexOf is at least 20X than a pre-built regexp, this will be faster unless the text contains 950+ of the 1000 terms (back of napkin).


here is everything in a simple html wrap to demo it (tested ff3.5):
Code:
<html><script>


//data:
var reps={ "ambung":"back-basket",  "amin":"amen",  "amir":"emir",  "ampai":"hang",  "ampai-ampai":"jellyfish",  "mengampaikan":"to hang clothes out to dry",  "terampai":"hung to air",  "ampaian":"clothes-line",  "ampu":"ingratiate",  "mengampu":"to ingratiate",  "pengampu":"sycophant",  "ampun":"pardon",  "keampunan":"forgiveness",  "mengampunkan":"to forgive",  "pengampun":"one who forgives",  "pengampunan":"amnesty",  "amuk":"amuck",  "mengamuk":"attack without thinking",  "pengamuk":"person who runs amuck",  "anai-anai":"termite",  "anak":"child",  "anak angkat":"adopted child",  "anak bulan":"new moon",  "anak dara":"virgin",  "anak merah":"newborn",  "anak-beranak":"together with the children",  "anak-anak":"many children",  "anak-anakan":"doll",  "anakanda":"son",  "beranak":"to bear a child",  "keanak-anakan":"childish",  "peranakan":"native",  "anasir":"an element",  "anasir jahat":"bad social element",  "ancam":"menace",  "mengancam":"to threaten",  "ancaman":"a threat",  "terancam":"threatened",  "anda":"you",  "andai":"if",  "andainya":"suppose",  "andaian":"assumption",  "berandai":"to discuss",  "mengandaikan":"suppose",  "andam":"beautician who does the make-up for the bride and bridegroom",  "mengandam":"to make-up the bride and bridegroom",  "andas":"anvil",  "anduh":"suspend by means of a sling",  "aneh":"bizarre",  "aneka":"multifarious",  "aneka ragam":"variety show",  "beraneka":"having a variety of",  "anekarama":"concert",  "angan":"imagination",  "angan-angan":"imagination",  "berangan-angan":"to day-dream",  "mengangan-angankan":"to wish for",  "terangan-angan":"to constantly wish for",  "angau":"lovesick",  "anggap":"assume",  "menganggap":"to assume",  "anggapan":"assumption",  "beranggapan":"of the opinion",  "anggar":"estimate",  "anggaran":"estimation",  "menganggarkan":"to estimate",  "anggerik":"orchid",  "anggota":"limb",  "beranggota":"having limbs",  "anggota badan":"part of the body",  "keanggotaan":"membership",  "menganggotai":"to be a member of",  "angguk":"nod",  "anggukan":"act of nodding",  "mengangguk":"to nod",  "terangguk-angguk":"to keep nodding",  "anggur":"grape",  "menganggur":"unemployed",  "penganggur":"one who is unemployed",  "pengangguran":"unemployment",  "angin":"wind",  "berangin":"windy",  "mengangin":"to winnow",  "menganginkan":"to put in a windy place",  "peranginan":"holiday resort",  "angka":"numerical figure",  "perangkaan":"statistics",  "angkara":"bad",  "angkasa":"space",  "angkasawan":"astronaut",  "angkat":"lift",  "angkatan":"troops",  "berangkat":"to depart",  "keberangkatan":"departure",  "angkuh":"haughty",  "keangkuhan":"arrogance",  "angkup":"callipers",  "angkut":"convey",  "mengangkut":"to transport",  "berangkut":"to transport several times",  "pengangkutan":"transportation",  "angsa":"goose",  "aniaya":"maltreatment",  "menganiayai":"to oppress",  "penganiaya":"oppressor",  "penganiayaan":"oppression",  "anjak":"shift position slightly",  "beranjak":"to shift position slightly",  "menganjakkan":"to change the position of",  "anjal":"elastic",  "keanjalan":"flexibility",  "menganjal":"to be elastic",  "anjing":"dog",  "anjung":"projecting facade",  "anjungan":"bridge",  "anjur":"to jut out",  "menganjur":"to jut out",  "anjuran":"proposal",  "menganjurkan":"to propose",  "penganjur":"organizer",  "ansur":"progress",  "beransur":"to progress in stages",  "ansuran":"instalment",  "mengansurkan":"to pay by instalments",  "antah":"rice grains left unhusked after pounding",  "antan":"pestle",  "antara":"among",  "mengantarai":"to mediate",  "perantara":"mediator",  "perantaraan":"mediation",  "antarabangsa":"international",  "antero":"whole",  "seantero":"the whole",  "anti":"opposed to",  "anting":"pendulum",  "abai":"neglect",  "mengabaikan":"to disregard",  "abang":"elder brother",  "abang angkat":"foster brother",  "abang ipar":"brother-in-law",  "abang tiri":"stepbrother",  "abdi":"slave",  "mengabdikan":"to enslave",  "pengabdian":"slavery",  "abjad":"alphabet",  "abu":"ash",  "mengabui":"to cheat",  "abuk":"dust",  "abung":"pomelo",  "acah":"tease",  "mengacah-acah":"to tease",  "acapkali":"often",  "acar":"pickle",  "acara":"event",  "mengacarakan":"to compere",  "pengacara":"master of ceremonies",  "aci":"shaft",  "acu":"to direct weapon at target",  "mengacukan":"to aim weapon at target",  "mengacu":"to test",  "acuan":"a mould",  "acuh":"to care for",  "mengacuhkan":"to care for",  "acuh tak acuh":"to show indifference",  "acum":"provoke",  "mengacum":"to provoke",  "acuman":"provocation",  "pengacum":"instigator",  "pengacuman":"agitation",  "ada":"have",  "adakala":"sometimes",  "adakalanya":"sometimes",  "adalah":"is",  "adapun":"with regard to",  "berada":"rich",  "diadakan":"to be held",  "keadaan":"condition",  "mengada-ngada":"to boast",  "mengada-ngadakan":"to concoct an excuse",  "mengadakan":"to hold",  "adab":"politeness",  "beradab":"courteous",  "peradaban":"civilization"};

//engine:
function repAll(str, reps){
 var buff=String(str);
 for(i in reps){if(reps.hasOwnProperty(i) && (buff.indexOf(i)!==-1)){
  buff=buff.replace(new RegExp("\\b"+i.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1')+"\\b", "ig"),reps[i]);
 }}
 return buff;
}


function translate(){
  document.getElementById('output').value = repAll(document.getElementById('input').value, reps);
}
</script>

<textarea id="input">"Say amin peradaban !"</textarea><br />
<input type="button" value="translate" onClick="translate();" /><br />
<textarea id="output"></textarea>
</html>
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 12-15-2009, 09:45 AM   PM User | #12
skyonline
New Coder

 
Join Date: Dec 2009
Posts: 36
Thanks: 31
Thanked 0 Times in 0 Posts
skyonline is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
Code:
<script type="text/javascript">
function CopyToClipboard() {
CopiedTxt = document.selection.createRange();
CopiedTxt.execCommand("Copy");
}

function PasteFromClipboard(){  
document.myForm.txtArea.focus();
PastedText = document.myForm.txtArea.createTextRange();
PastedText.execCommand("Paste");
} 
</script>

<form name="myForm">
Select this text, copy it using the copy button, and paste it below.<br /><br />
<textarea id="txtArea" cols="60" rows="5">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ipsum leo, scelerisque at dapibus ac, consectetur vel ipsum. Morbi et metus ut diam molestie ullamcorper. Suspendisse rutrum semper semper. Donec volutpat neque in lorem tempus scelerisque. </textarea>
<br />
<input type="button" onClick="CopyToClipboard()" value="Copy to clipboard" />
<input type="button" onClick="PasteFromClipboard()" value="Paste from clipboard" /> 
<input type="button" value="Highlight Text" onClick="javascript:this.form.txtArea.focus();this.form.txtArea.select();"> 
<input type="button" value = "Clear The Text" onclick = "document.getElementById('txtArea').value = '' "> 

</form>
But this copy/paste code will only work in IE.
but..How to combine with this javascripts (Translator):

Code:
<script type="text/javascript">
function translate(phrase) {
phrase = document.getElementById('input').value;
phrase = phrase.replace(/\babadi\b/ig, "eternal");
phrase = phrase.replace(/\bkeabadian\b/ig, "immortality");
phrase = phrase.replace(/\bmengabadikan\b/ig, "perpetuate");
phrase = phrase.replace(/\babah\b/ig, "father");
phrase = phrase.replace(/\babah-abah\b/ig, "harness");
document.getElementById('output').value = phrase;
}
</script>

<textarea id="input"></textarea><br />
<input type="button" value="translate" onClick="translate();" /><br />
<textarea id="output"></textarea>

Last edited by Trinithis; 12-15-2009 at 09:54 AM..
skyonline is offline   Reply With Quote
Old 12-15-2009, 09:51 AM   PM User | #13
Trinithis
Regular Coder

 
Join Date: Jun 2007
Location: USA
Posts: 527
Thanks: 26
Thanked 74 Times in 72 Posts
Trinithis will become famous soon enough
Whoa! Lots of replies since I coded my solution:

translator.js
Code:
function Translator () {
  this.rules = [];
}

(function () {
  var dollar = /(\$+)/g;

  function Rule (regex, replacement) {
    if (typeof replacement === "function") {
      var oldReplacement = replacement;
      replacement = function (execResult) {
        for (var i = 0; i < execResult.length; ++i) {
          execResult [i] = execResult [i].replace (dollar, "$1$1");
        }
        return oldReplacement (execResult);
      };
    }
    this.regex = regex;
    this.replacement = replacement;
  }
  
  Translator.prototype = {
      constructor: Translator
  
    , addRule: function (regex, replacement, keepStartCase) {
        if (!regex.global) {
          throw new TypeError ("Translator.prototype.addTranslation: expects " + regex + " with global property set.");
        }
        if (keepStartCase) {
          var oldReplacement = replacement;
          replacement = function (execResult) {
            var str = typeof oldReplacement === "function"
              ? oldReplacement (execResult)
              : oldReplacement
              ;
            var chr = execResult [0].charAt (0);
            if (!execResult [0].length) {
              return str;
            }
            var head = str.substr (0, 1) [chr == chr.toUpperCase () ? "toUpperCase" : "toLowerCase"] ();
            var tail = str.substr (1);
            return head + tail;
          };
        }
        var rule = new Rule (regex, replacement);
        this.rules.push (rule);
      }
  
    , translate: function (str) {
        var translation = [];
        var pos = 0;
        while (pos < str.length) {
          var bestExec = null;
          var bestRule = null;
          for (var i = 0; i < this.rules.length; ++i) {
            var rule = this.rules [i];
            rule.regex.lastIndex = pos;
            var exec = rule.regex.exec (str);
            if (exec) {
              var doUpdate = !bestExec 
                || (exec.index < bestExec.index)
                || (exec.index == bestExec.index && exec [0].length > bestExec [0].length)
                ;
              if (doUpdate) {
                bestExec = exec;
                bestRule = rule;
              }
            }
          }
          if (!bestExec) {
            return translation.join ("") + str.substr (pos);
          }
          else {
            translation.push (str.substring (pos, bestExec.index));
            pos = bestRule.regex.lastIndex;
            var replacement = typeof bestRule.replacement === "function"
              ? bestRule.replacement (bestExec)
              : bestRule.replacement
              ;
            translation.push (bestExec [0].replace (bestRule.regex, replacement));
          }
        }
        return translation.join ("");
      }
  };
}) ();
test.html
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html>
  <head>
    <title> Test </title>
  </head>
  <body>
    <div>
      <script type="text/javascript" src="translator.js"></script>
      <script type="text/javascript">

        var t = new Translator ();

        t.addRule (/ham/gi, "beef");
        t.addRule (/prime/gi, "SHOULDN'T MATCH IN EXAMPLE");
        t.addRule (/hamburgers/gi, "prime ribs");
        t.addRule (/ribs/gi, "SHOULDN'T MATCH IN EXAMPLE");
        t.addRule (/(')/g, "$1$1");
        t.addRule (/(b)(oss)/g, function (execResult) {
          return execResult [1].toUpperCase () + execResult [2];
        });
        t.addRule (/the/gi, "the", true);
        t.addRule (/(\$|@)/g, function (execResult) {
          return "(" + execResult [0] + "1$1)";
        });

        document.writeln (t.translate ("'The hamburgers are $20,' Jordan said to the boss @ outside."));

      </script>
    </div>
  </body>
</html>

Last edited by Trinithis; 12-15-2009 at 10:07 AM.. Reason: test.js -> test.html
Trinithis is offline   Reply With Quote
Old 12-15-2009, 10:12 AM   PM User | #14
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by skyonline View Post
but..How to combine with this javascripts (Translator):
No idea what you mean. Simply place the script I gave you on the page. The only difference is that your textrea is named output and I have named it txtArea in my script.

Trinithis and rnd me - skyonline seems to be a beginner. The art of successful teaching is to tailor the advice to the knowledge/skill level of the student. Sophisticated graduate-level solutions are not very helpful to people struggling with basic concepts - especially if they contain errors.

Last edited by Philip M; 12-15-2009 at 10:26 AM..
Philip M is offline   Reply With Quote
Old 12-15-2009, 10:24 AM   PM User | #15
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
tisk!

Code:
"Jordan said to the boss @ ou".replace(/(b)(oss)/g, function (execResult) {
          return execResult [1].toUpperCase () + execResult [2];
 }); //==="Jordan said to the Os @ ou"
is that right? it's an error in IE, but hey that's only 2/3rds of the market (sadly)...
i've never seen anything but virtual perfection from Trinithis, so i feel lucky.


that the first arg to a replace function is usually junk: the whole match (a string).
i usually name my params (junk, a, b) as a result...
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:51 AM.


Advertisement
Log in to turn off these ads.