// $Id: albhed.in.js,v 1.3 2011/10/01 15:30:01 stephen Exp $ /* * English to Al Bhed translation toy * * Copyright (c) 2011 Stephen Williams, all rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the copyright holder may not be used to endorse or * promote products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ // map of English letters to Al Bhed letters var e2a = new Object; e2a['a'] = 'y'; e2a['b'] = 'p'; e2a['c'] = 'l'; e2a['d'] = 't'; e2a['e'] = 'a'; e2a['f'] = 'v'; e2a['g'] = 'k'; e2a['h'] = 'r'; e2a['i'] = 'e'; e2a['j'] = 'z'; e2a['k'] = 'g'; e2a['l'] = 'm'; e2a['m'] = 's'; e2a['n'] = 'h'; e2a['o'] = 'u'; e2a['p'] = 'b'; e2a['q'] = 'x'; e2a['r'] = 'n'; e2a['s'] = 'c'; e2a['t'] = 'd'; e2a['u'] = 'i'; e2a['v'] = 'j'; e2a['w'] = 'f'; e2a['x'] = 'q'; e2a['y'] = 'o'; e2a['z'] = 'w'; // map of Al Bhed letters to English letters var a2e = new Object; for (var letter in e2a) a2e[e2a[letter]] = letter; // given a string and a map, replaces letters in the string // with their equivalents in the map. [ stops mapping and ] // restarts. Returns the result in a new string; does not // modify the input string function translate(s, map) { var output = new Array; var passthrough = false; for (var i = 0, len = s.length; i < len; i++) { var c = s.charAt(i), l; if (!passthrough) { if (c in map) c = map[c]; else if ((l = c.toLowerCase()) in map) c = map[l].toUpperCase() else if (c == '[') passthrough = true; } else if (c == ']') passthrough = false; output.push(c); } return output.join(''); } // extracts text from a text area source, translates it // using the supplied map, and pastes it into the text // area target function translate_area(source, target, map) { var original = source.value; var translated = translate(original, map); target.value = translated; } // English to Al Bhed is form 0; Al Bhed to English is form 1 var e2a_form = 0, a2e_form = 1; // form submission hooks that return false to cancel submission // so the translation runs entirely in the browser function e2a_submit() { translate_area(document.forms[e2a_form]['english'], document.forms[a2e_form]['albhed'], e2a); return false; } function a2e_submit() { translate_area(document.forms[a2e_form]['albhed'], document.forms[e2a_form]['english'], a2e); return false; } document.forms[e2a_form].onsubmit = e2a_submit; document.forms[a2e_form].onsubmit = a2e_submit;