Click to See Complete Forum and Search --> : 'Simple' Translator Script


Spectra
02-10-2003, 05:46 PM
I'm making a game... and for it we have our own language. I stripped out all the fancy html to help make things a little clearer for someone trying to help me.

<SCRIPT LANGUAGE="JavaScript">
<!--
function translate(form){
var alpha='ABCDEFGHIJKLMNOPQRSTUVWXYZURKPAVDHEQCLNMYGWJTSOBFZIX';
var x = 0;
var raw = form.raw.value;

for (x = 0; x <= raw.length; x++){
var part = raw.substring(x, x+1);
if (part >= 'A' && part <= 'Z'){ var letters = "upper" }
if (part >= 'a' && part <= 'z'){ var letters = "lower" }
var part = part.toUpperCase();

if (part >= 'A' && part <= 'Z'){
part = alpha.charAt(alpha.indexOf(part) + 26);
if (letters == "lower"){ part = part.toLowerCase() }
}

form.cooked.value = form.cooked.value + part;
}
}
// -->
</SCRIPT>

English
<TEXTAREA NAME="raw" COLS="50" ROWS="15"></TEXTAREA>

Ramathian
<TEXTAREA NAME="cooked" COLS="50" ROWS="15"></TEXTAREA>

<INPUT TYPE="button" onClick="translate(this.form)" value="Translate">

All it does is substitute the letters A to Z with a new set of letters that we've assigned for each one.

English:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Ramathian:
U R K P A V D H E Q C L N M Y G W J T S O B F Z I X

So A = U, Z = X, and so on.

What I /want/ it to have is some sort of modification so that I can use a drop down menu to easily translate text back and forth between English and "Ramathian". I also want to add variables so that if a user enters the word "dog" for instance, it'll print out something like "cateater" instead of "pyd". If anyone has any clue as to how to do this, or knows of somewhere that could help me out, I'd greatly appreciate it.

Charles
02-10-2003, 06:06 PM
The thread over at http://forums.webdeveloper.com/showthread.php?s=&threadid=3779 will get you started.

Spectra
02-10-2003, 06:10 PM
Hey thanks. ^_^ I'll check it out. Hopefully I can get this thing working.

Spectra
02-10-2003, 07:31 PM
Thanks Dave. :D That was just what I was looking for.

Spectra
02-10-2003, 08:24 PM
Hm... just another question. Is there a way that I can make it so that when a person puts square brackets(or something similar) around a word, it won't get translated?

Spectra
02-11-2003, 09:28 PM
Thanks again! :) It works perfectly. So far my greatest accomplishment in JavaScript is making a program that draws a square... oh but I'll learn. Hopefully. :p