Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 4 123 ... LastLast
Results 1 to 15 of 52

Thread: text Translator

  1. #1
    New Coder
    Join Date
    Dec 2009
    Posts
    36
    Thanks
    31
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,091
    Thanks
    38
    Thanked 498 Times in 492 Posts

    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.

  • Users who have thanked jmrker for this post:

    skyonline (12-16-2009)

  • #3
    New Coder
    Join Date
    Dec 2009
    Posts
    36
    Thanks
    31
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,374
    Thanks
    11
    Thanked 592 Times in 572 Posts
    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
    Last edited by rnd me; 12-15-2009 at 07:07 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • Users who have thanked rnd me for this post:

    skyonline (12-16-2009)

  • #5
    New Coder
    Join Date
    Dec 2009
    Posts
    36
    Thanks
    31
    Thanked 0 Times in 0 Posts
    its use google right...i want to make it offline...because i got my own text...i only need jasvascripts

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,374
    Thanks
    11
    Thanked 592 Times in 572 Posts
    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 !"
    Last edited by rnd me; 12-15-2009 at 08:05 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • Users who have thanked rnd me for this post:

    skyonline (12-16-2009)

  • #7
    New Coder
    Join Date
    Dec 2009
    Posts
    36
    Thanks
    31
    Thanked 0 Times in 0 Posts
    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..

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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

  • #9
    New Coder
    Join Date
    Dec 2009
    Posts
    36
    Thanks
    31
    Thanked 0 Times in 0 Posts
    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.

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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.

  • #11
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,374
    Thanks
    11
    Thanked 592 Times in 572 Posts
    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 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #12
    New Coder
    Join Date
    Dec 2009
    Posts
    36
    Thanks
    31
    Thanked 0 Times in 0 Posts
    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.

  • #13
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    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

  • #14
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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.

  • #15
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,374
    Thanks
    11
    Thanked 592 Times in 572 Posts
    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 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%


  •  
    Page 1 of 4 123 ... LastLast

    LinkBacks (?)


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •