音译脚本 - 奇怪的错误

时间:2013-02-17 17:49:40

标签: javascript html transliteration

此HTML脚本会对输入的每个单词,短语或字母进行音译。

基本上,它是一个非官方的翻译。

使用jsFiddle测试代码时,出现以下错误:

  

{“error”:“请使用POST请求”}

我将文件正确保存为.html,并使用Google Chrome打开。

一切都是这样的;我能够音译文本,但是当我放置太多文本进行音译时,框中的文本被省略,代码(页面)基本上“刷新”自己。

但是,此代码翻译小短语和单词。

要音译的字符数量是否有限制?

我不确定错误是什么,但我确定前面提到的jsFiddle错误与此错误相符。

让我感到困惑。

音译代码如下:

<html>
<head>
    <title>English - Imzreth Translator</title>
    <script>
        validator = "aábcdeéfghjiíklmnoöpqrstuüvwxyzAÁBCDEÉFGHIJKLMNOÖPQRSTUÜVWXYZ’";
        vowels = "aáeéiíoöuüyAÁEÉIÍOÖUÜY’";

        aemap = new Array();
        aemap["au"] = "a";
        aemap["a"] = "a";
        aemap["l"] = "b";
        aemap["b"] = "b";
        aemap["c"] = "c";
        aemap["ku"] = "d";
        aemap["d"] = "d";
        aemap["u"] = "e";
        aemap["u"] = "e";
        aemap["f"] = "f";
        aemap["gé"] = "g";
        aemap["g"] = "g";
        aemap["h"] = "h";
        aemap["o"] = "i";
        aemap["i"] = "i";
        aemap["t"] = "j";
        aemap["j"] = "j";
        aemap["ré"] = "k";
        aemap["k"] = "k";
        aemap["l"] = "l";
        aemap["m"] = "m";
        aemap["q"] = "n";
        aemap["n"] = "n";
        aemap["i"] = "o";
        aemap["o"] = "o";
        aemap["ph"] = "p";
        aemap["p"] = "p";
        aemap["q"] = "q";
        aemap["r"] = "r";
        aemap["s"] = "s";
        aemap["s"] = "t";
        aemap["t"] = "t";
        aemap["e"] = "u";
        aemap["u"] = "u";
        aemap["z"] = "v";
        aemap["v"] = "v";
        aemap["w"] = "w";
        aemap["l"] = "x";
        aemap["x"] = "x";
        aemap["y"] = "y";
        aemap["á"] = "y";
        aemap["z"] = "z";

        eamap = new Array();
        eamap["a"] = "au";
        eamap["b"] = "l";
        eamap["c"] = "c";
        eamap["d"] = "ku";
        eamap["e"] = "u";
        eamap["f"] = "f";
        eamap["g"] = "gé";
        eamap["h"] = "h";
        eamap["i"] = "o";
        eamap["j"] = "t";
        eamap["k"] = "ré";
        eamap["l"] = "l";
        eamap["m"] = "m";
        eamap["n"] = "n";
        eamap["o"] = "i";
        eamap["p"] = "ph";
        eamap["q"] = "q";
        eamap["r"] = "r";
        eamap["s"] = "s";
        eamap["t"] = "s";
        eamap["u"] = "e";
        eamap["v"] = "z";
        eamap["w"] = "w";
        eamap["x"] = "l";
        eamap["y"] = "á";
        eamap["z"] = "z";

        eamap["A"] = "A";
        eamap["B"] = "B";
        eamap["C"] = "C";
        eamap["D"] = "D";
        eamap["E"] = "E";
        eamap["F"] = "F";
        eamap["G"] = "G";
        eamap["H"] = "H";
        eamap["I"] = "I";
        eamap["J"] = "J";
        eamap["K"] = "K";
        eamap["L"] = "L";
        eamap["M"] = "M";
        eamap["N"] = "N";
        eamap["O"] = "O";
        eamap["P"] = "P";
        eamap["Q"] = "Q";
        eamap["R"] = "R";
        eamap["S"] = "S";
        eamap["T"] = "T";
        eamap["U"] = "U";
        eamap["V"] = "V";
        eamap["W"] = "W";
        eamap["X"] = "X";
        eamap["Y"] = "Y";
        eamap["Z"] = "Z";
        promap = new Array();
        promap["a"] = "aah";
        promap["b"] = "buh";
        promap["c"] = "se";
        promap["d"] = "duh";
        promap["e"] = "eh";
        promap["f"] = "f";
        promap["g"] = "guh";
        promap["h"] = "h’";
        promap["i"] = "íh";
        promap["j"] = "juh";
        promap["k"] = "kuh";
        promap["l"] = "l’";
        promap["m"] = "m";
        promap["n"] = "n";
        promap["o"] = "öh";
        promap["p"] = "puh";
        promap["q"] = "quah";
        promap["r"] = "ra";
        promap["s"] = "s";
        promap["t"] = "t’uh";
        promap["u"] = "ooh";
        promap["v"] = "vuh";
        promap["w"] = "wuh";
        promap["x"] = "k’ss";
        promap["y"] = "ya";
        promap["z"] = "z";
        promap["ph"] = "f’";
        promap["au"] = "aw";
        promap["gé"] = "zhé";
        promap["á"] = "ahh";
        promap["ku"] = "kü";

        promap["A"] = "Á";
        promap["B"] = "B’";
        promap["C"] = "CÉ";
        promap["D"] = "D’";
        promap["E"] = "EH";
        promap["F"] = "F’";
        promap["G"] = "G’";
        promap["H"] = "H’";
        promap["I"] = "Í’";
        promap["J"] = "J’";
        promap["K"] = "K’";
        promap["L"] = "L’";
        promap["M"] = "M";
        promap["N"] = "N";
        promap["O"] = "Ö’";
        promap["P"] = "P’";
        promap["Q"] = "Q’";
        promap["R"] = "RA";
        promap["S"] = "SEE";
        promap["T"] = "T’UH";
        promap["U"] = "OOH";
        promap["V"] = "VUH";
        promap["W"] = "WUH";
        promap["X"] = "K’SS";
        promap["Y"] = "YA";
        promap["Z"] = "Z";
        promap["PH"] = "F";
        promap["AU"] = "AW";
        promap["KU"] = "KÜ";

        function transme() {
            if (document.theform.picker[0].checked) { // english to al bhed 
                builder = "";
                toggler = 0;

                for (var i = 0; i < document.theform.stimulus.value.length; i++) {
                    if (document.theform.stimulus.value.charAt(i) == "[") {
                        toggler = 1;
                    };
                    if (document.theform.stimulus.value.charAt(i) == "]") {
                        toggler = 0;
                    };

                    if (toggler == 1) {
                        builder = builder + document.theform.stimulus.value.charAt(i);
                    } else {
                        if (validator.indexOf(document.theform.stimulus.value.charAt(i)) == -1) {
                            builder = builder + document.theform.stimulus.value.charAt(i);
                        } else {
                            builder = builder + eamap[document.theform.stimulus.value.charAt(i)];
                        };
                    };
                };

                builder2 = "";
                toggler = 0;
                prev = 0;

                for (var i = 0; i < document.theform.stimulus.value.length; i++) {
                    if (document.theform.stimulus.value.charAt(i) == "[") {
                        toggler = 1;
                    };
                    if (document.theform.stimulus.value.charAt(i) == "]") {
                        toggler = 0;
                    };

                    if (toggler == 1) {
                        builder2 = builder2 + document.theform.stimulus.value.charAt(i);
                    } else {
                        if (validator.indexOf(document.theform.stimulus.value.charAt(i)) == -1) {
                            builder2 = builder2 + document.theform.stimulus.value.charAt(i);
                            prev = 0;
                        } else {
                            temp = promap[eamap[document.theform.stimulus.value.charAt(i)]];
                            if (prev == 1) {
                                if ((vowels.indexOf(temp.charAt(0)) == -1) && (temp.length != 1)) {
                                    builder2 = builder2 + "-";
                                };
                            };
                            builder2 = builder2 + temp;
                            prev = 1;
                        };
                    };
                };
                document.theform.response.value = builder + "\n\nPronounced: \"" + builder2 + "\"";

            } else { //al bhed to english
                builder = "";
                toggler = 0;

                for (var i = 0; i < document.theform.stimulus.value.length; i++) {
                    if (document.theform.stimulus.value.charAt(i) == "[") {
                        toggler = 1;
                    };
                    if (document.theform.stimulus.value.charAt(i) == "]") {
                        toggler = 0;
                    };

                    if (toggler == 1) {
                        builder = builder + document.theform.stimulus.value.charAt(i);
                    } else {
                        if (validator.indexOf(document.theform.stimulus.value.charAt(i)) == -1) {
                            builder = builder + document.theform.stimulus.value.charAt(i);
                        } else {
                            builder = builder + aemap[document.theform.stimulus.value.charAt(i)];
                        };
                    };
                };
                document.theform.response.value = builder;
            };
            return false;
        };
    </script>
</head>

<body>
    <form method="get" name="theform" action="" onSubmit="return transme()">
        <p><b><h2>English to Custom Language Translator (With Phonetics)</h2></b>

            <p><b>Translate Text:</b>

                <br>    <u>(Excluded text should be within brackets).</u>

                <br>
                <textarea name="stimulus" cols="60" rows="6" wrap="VIRTUAL"></textarea>
            </p>
            <p><b>Translated Text:</b>

                <br>
                <textarea name="response" cols="60" rows="6" wrap="VIRTUAL"></textarea>
            </p>
            <p><b>Translation Options:</b>

                <br>
                <input name="picker" type="radio" value="0" checked>English to Custom Language (With Phonetics)
                <br>
                <input name="picker" type="radio" value="1">Custom Language to English (With Rough Phonetics)</p>
            <p>
                <input type="submit" value="Transl ate / Traunslausu!">
            </p>
    </form>
</body>

</html>

我很乐意帮助修复代码以确保完整功能。

1 个答案:

答案 0 :(得分:0)

删除onload包装器(jsfiddle页面的左上角),您的代码运行正常。

Fiddle

使用onload包装器,您的函数在全局范围内无法访问,因此您的提交处理程序onSubmit="return transme()"导致“transme未定义”错误,并且由于该函数从未被调用,因此它永远不会被调用达到阻止表单提交的return false