Google 翻译 Javascript 生成的内容

发布于 2024-11-10 04:38:58 字数 11708 浏览 4 评论 0原文

使用谷歌翻译时,有什么方法可以翻译从用户表单输入生成的内容(使用我下面采用的方法)?

现在,页面将进行翻译,但是当用户在翻译的页面上填写并提交表单时,答案当然会以英语返回。这是有道理的,因为翻译发生在表单提交之前(并且发生在 Google 的服务器上)。

有没有相对简单的方法解决这个问题?我仍在摸索 JavaScript,因此非常感谢您提供的任何详细信息。一想到这个我的小脑袋就爆炸了。

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>

    <script type="text/javascript">

   <!-- Begin CE MP Savings Calc script
    function  doMath3() {
        var one = parseInt(document.theForm3.elements[0].value);
        var two = parseInt(document.theForm3.elements[1].value);
        var three = one * two;
        var z4yearlyCash = three * 4.25 * 12;
        var z3yearlyCash = three * 3.75 * 12;
        var z2yearlyCash = three * 3 * 12;
        var z1yearlyCash = three * 2.5 * 12;
        var baseyearlyCash = three * 1.5 * 12;
        var selection = document.getElementsByName("zone")[0].value;
        var answerCemp = document.getElementById("answerCemp");


    if (document.theForm3.elements[0].value == ""){
    alert("Please enter the number of days a month you commute on Commuter Express.")   
    }

    if (document.theForm3.elements[1].value == ""){
    alert("Please enter the number of times you board a Commuter Express daily.")   
    }   
        if(one > 31){
            alert("Come now, no month has " + one + " days! Please try again.");
        }

        else if(selection == "z4"){
            var prodZ4 = z4yearlyCash - 1680;
        if(prodZ4 > 0){
           answerCemp.innerHTML="<div class='answerbox'><h3>Your Yearly Savings<\/h3><p>If you pay cash, your yearly expense is $" + z4yearlyCash + ".<br>Commuter Express Zone 4 Monthly Passes cost $1680 per year.<br><strong>That&#39;s a yearly savings of $"  +  prodZ4  +  "!<\/strong><br><strong><a href='http://store.ladottransit.com/Merchant2/merchant.mvc?Screen=CTGY&amp;Store_Code=LTSO&amp;Category_Code=MP4'>Buy a Commuter Express Zone 4 Monthly Pass now.<\/a><\/strong><\/p><\/div>";
            }
        else if(prodZ4 <= 0){
            answerCemp.innerHTML="<div class='answerbox'><h3>Your Results<\/h3><p>Looks like you don't ride Commuter Express often enough to benefit from purchasing a Monthly Pass. Trip Tickets may be a better option for you. <strong><a href='#triptickets'>Try the Trip Tickets Calculator below.<\/a><\/strong><\/p><\/div>";
        }
        }

        else if(selection == "z3"){
            var prodZ3 = z3yearlyCash - 1488;
            if(prodZ3 > 0){
            answerCemp.innerHTML="<div class='answerbox'><h3>Your Yearly Savings<\/h3><p>If you pay cash, your yearly expense is $" + z3yearlyCash + ".<br>Commuter Express Zone 3 Monthly Passes cost $1488 per year.<br><strong>That&#39;s a yearly savings of $"  +  prodZ3  +  "!<\/strong><br><strong><a href='http://store.ladottransit.com/Merchant2/merchant.mvc?Screen=PROD&Store_Code=LTSO&Product_Code=LADOTMPZ3&Category_Code=MP4'>Buy A Commuter Express Zone 3 Monthly Pass Now<\/a><\/strong><\/p><\/div>";
            }
            else if(prodZ3 <= 0){
            answerCemp.innerHTML="<div class='answerbox'><h3>Your Results<\/h3><p>Looks like you don't ride Commuter Express often enough to benefit from purchasing a Monthly Pass. Trip Tickets may be a better option for you. <strong><a href='#triptickets'>Try the Trip Tickets Calculator below.<\/a><\/strong><\/p><\/div>";
        }
        }

        else if(selection == "z2"){
            var prodZ2 = z2yearlyCash - 1200;
            if(prodZ2 > 0){
            answerCemp.innerHTML="<div class='answerbox'><h3>Your Yearly Savings<\/h3><p>If you pay cash, your yearly expense is $" + z2yearlyCash + ".<br>Commuter Express Zone 2 Monthly Passes cost $1200 per year.<br><strong>That&#39;s a a yearly savings of $"  +  prodZ2  +  "<\/strong>!<br><strong><a href='http://store.ladottransit.com/Merchant2/merchant.mvc?Screen=PROD&Store_Code=LTSO&Product_Code=LADOTMPZ2&Category_Code=MP4'>Buy A Commuter Express Zone 2 Monthly Pass Now<\/a><\/strong><\/p><\/div>";
            }
            else if(prodZ2 <= 0){
            answerCemp.innerHTML="<div class='answerbox'><h3>Your Results<\/h3><p>Looks like you don't ride Commuter Express often enough to benefit from purchasing a Monthly Pass. Trip Tickets may be a better option for you. <strong><a href='#triptickets'>Try the Trip Tickets Calculator below.<\/a><\/strong><\/p><\/div>";
        }       
        }

        else if(selection == "z1"){
            var prodZ1 = z1yearlyCash - 960;
            if(prodZ1 > 0){
             answerCemp.innerHTML="<div class='answerbox'><h3>Your Yearly Savings<\/h3><p>If you pay cash, your yearly expense is $" + z1yearlyCash + ".<Br>Commuter Express Zone 1 Monthly Passes cost $960 per year.<br><strong>That&#39;s a yearly savings of $"  +  prodZ1  +  "<\/strong>!<br><strong><a href='http://store.ladottransit.com/Merchant2/merchant.mvc?Screen=PROD&Store_Code=LTSO&Product_Code=LADOTMPZ1&Category_Code=MP4'>Buy A Commuter Express Zone 1 Monthly Pass Now<\/a><\/strong><\/p><\/div>";
            }
            else if(prodZ1 <= 0){
            answerCemp.innerHTML="<div class='answerbox'><h3>Your Results<\/h3><p>Looks like you don't ride Commuter Express often enough to benefit from purchasing a Monthly Pass. Trip Tickets may be a better option for you. <strong><a href='#triptickets'>Try the Trip Tickets Calculator below.<\/a><\/strong><\/p><\/div>";
        }
        }

        else if(selection == "Base"){
            var prodBase = baseyearlyCash - 684;
            if(prodBase > 0){
            answerCemp.innerHTML="<div class='answerbox'><h3>Your Yearly Savings<\/h3><p>If you pay cash, your yearly expense is $" + baseyearlyCash + ".<br>Commuter Express Base Monthly Passes cost $684 per year.<br><strong>That&#39;s a yearly savings of $"  +  prodBase  +  "!<\/strong><br><strong><a href='http://store.ladottransit.com/Merchant2/merchant.mvc?Screen=PROD&Store_Code=LTSO&Product_Code=LADOTMPB&Category_Code=MP4'>Buy A Commuter Express Base Monthly Pass Now<\/a><\/strong><\/p><\/div>";
            }
            else if(prodBase <= 0){
            answerCemp.innerHTML="<div class='answerbox'><h3>Your Results<\/h3><p>Looks like you don't ride Commuter Express often enough to benefit from purchasing a Monthly Pass. Trip Tickets may be a better option for you. <strong><a href='#triptickets'>Try the Trip Tickets Calculator below.<\/a><\/strong><\/p><\/div>";
        }
        }
    }

    // End CE MP Savings Calc script -->

    </script>

</head>

<body>

<form action="javascript:window.location = 'http://translate.google.com/translate?hl=en&amp;sl=en&amp;tl=' + document.translateform.translate.value + '&amp;u=' + window.location.href;" method="get" name="translateform">
                    <label for="translate">translate this page</label>
                    <select name="translate" id="translate">
                      <option value="es">Spanish</option>
                      <option value="ar">Arabic</option>
                      <option value="zh-CN">Chinese Simplified</option>
                      <option value="zh-TW">Chinese Traditional</option>
                      <option value="nl">Dutch</option>
                      <option value="fr">French</option>
                      <option value="de">German</option>
                      <option value="el">Greek</option>
                      <option value="it">Italian</option>
                      <option value="ja">Japanese</option>
                      <option value="ko">Korean</option>
                      <option value="pt">Portuguese</option>
                      <option value="ru">Russian</option>
                    </select>
                    <div class="gobuttonwrapper">
                      <input name="submit" type="submit" value="go" class="gobutton">
                    </div>
                  </form>


<div class="calcform">
  <form name="theForm3">
<h2><a name="cemp" id="cemp"></a>You Do the Math: Commuter Express Monthly Pass Vs. Cash</h2>
                        <div class="calcform-content">
                          <div class="formrow-calc">
                            <div class="calcform-col1">
                              <label for="trips-cemp">Daily boardings on Commuter Express Bus*:</label>
                              <input type="text" id="trips-cemp">

                            </div><div class="calcform-col3"></div>
                          </div>
                          <div class="formrow-calc">
                            <div class="calcform-col1">
                              <label for="days-cemp">Days you ride Commuter Express <strong>per month</strong>:</label>

                            <input type="text" id="days-cemp"></div>


                          </div>
                          <div class="formrow-calc">
                            <div class="calcform-col1">
                              <label for="choosezone">Choose Zone:</label>
                              <select name="zone" id="choosezone">
                                <option value="Base">Base</option>
                                <option value="z1">Zone 1</option>
                                <option value="z2">Zone 2</option>
                                <option value="z3">Zone 3</option>
                                <option value="z4">Zone 4</option>
                              </select>

                            </div>
                          </div>
                          <div class="clear"></div>
                          <div align="center">
                            <button type="submit" onclick="doMath3(); return false;" class="btn-submit"><img src="img/btn_savings.png" alt="Show My Yearly Savings"><br>
                            <img src="img/arrow_results.png" alt="your results will display below"></button>
                          </div>


                        </div>
                      </form>
                      <div id="answerCemp"></div>
                    </div>

</body>
</html>

Is there any way to translate content that is generated from user form input when using Google Translate (using the method I employ below)?

Right now, the page will translate, but when a user fills in and submits the form on a translated page, the answer, of course, is returned in English. This makes sense, since the translation takes place prior to the form submission (and takes place on Google's servers).

Is there any relatively simple way around that? I'm still feeling my way around javascript, so any details you can provide are much appreciated. Just thinking about this made my tiny head explode.

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>

    <script type="text/javascript">

   <!-- Begin CE MP Savings Calc script
    function  doMath3() {
        var one = parseInt(document.theForm3.elements[0].value);
        var two = parseInt(document.theForm3.elements[1].value);
        var three = one * two;
        var z4yearlyCash = three * 4.25 * 12;
        var z3yearlyCash = three * 3.75 * 12;
        var z2yearlyCash = three * 3 * 12;
        var z1yearlyCash = three * 2.5 * 12;
        var baseyearlyCash = three * 1.5 * 12;
        var selection = document.getElementsByName("zone")[0].value;
        var answerCemp = document.getElementById("answerCemp");


    if (document.theForm3.elements[0].value == ""){
    alert("Please enter the number of days a month you commute on Commuter Express.")   
    }

    if (document.theForm3.elements[1].value == ""){
    alert("Please enter the number of times you board a Commuter Express daily.")   
    }   
        if(one > 31){
            alert("Come now, no month has " + one + " days! Please try again.");
        }

        else if(selection == "z4"){
            var prodZ4 = z4yearlyCash - 1680;
        if(prodZ4 > 0){
           answerCemp.innerHTML="<div class='answerbox'><h3>Your Yearly Savings<\/h3><p>If you pay cash, your yearly expense is $" + z4yearlyCash + ".<br>Commuter Express Zone 4 Monthly Passes cost $1680 per year.<br><strong>That's a yearly savings of $"  +  prodZ4  +  "!<\/strong><br><strong><a href='http://store.ladottransit.com/Merchant2/merchant.mvc?Screen=CTGY&Store_Code=LTSO&Category_Code=MP4'>Buy a Commuter Express Zone 4 Monthly Pass now.<\/a><\/strong><\/p><\/div>";
            }
        else if(prodZ4 <= 0){
            answerCemp.innerHTML="<div class='answerbox'><h3>Your Results<\/h3><p>Looks like you don't ride Commuter Express often enough to benefit from purchasing a Monthly Pass. Trip Tickets may be a better option for you. <strong><a href='#triptickets'>Try the Trip Tickets Calculator below.<\/a><\/strong><\/p><\/div>";
        }
        }

        else if(selection == "z3"){
            var prodZ3 = z3yearlyCash - 1488;
            if(prodZ3 > 0){
            answerCemp.innerHTML="<div class='answerbox'><h3>Your Yearly Savings<\/h3><p>If you pay cash, your yearly expense is $" + z3yearlyCash + ".<br>Commuter Express Zone 3 Monthly Passes cost $1488 per year.<br><strong>That's a yearly savings of $"  +  prodZ3  +  "!<\/strong><br><strong><a href='http://store.ladottransit.com/Merchant2/merchant.mvc?Screen=PROD&Store_Code=LTSO&Product_Code=LADOTMPZ3&Category_Code=MP4'>Buy A Commuter Express Zone 3 Monthly Pass Now<\/a><\/strong><\/p><\/div>";
            }
            else if(prodZ3 <= 0){
            answerCemp.innerHTML="<div class='answerbox'><h3>Your Results<\/h3><p>Looks like you don't ride Commuter Express often enough to benefit from purchasing a Monthly Pass. Trip Tickets may be a better option for you. <strong><a href='#triptickets'>Try the Trip Tickets Calculator below.<\/a><\/strong><\/p><\/div>";
        }
        }

        else if(selection == "z2"){
            var prodZ2 = z2yearlyCash - 1200;
            if(prodZ2 > 0){
            answerCemp.innerHTML="<div class='answerbox'><h3>Your Yearly Savings<\/h3><p>If you pay cash, your yearly expense is $" + z2yearlyCash + ".<br>Commuter Express Zone 2 Monthly Passes cost $1200 per year.<br><strong>That's a a yearly savings of $"  +  prodZ2  +  "<\/strong>!<br><strong><a href='http://store.ladottransit.com/Merchant2/merchant.mvc?Screen=PROD&Store_Code=LTSO&Product_Code=LADOTMPZ2&Category_Code=MP4'>Buy A Commuter Express Zone 2 Monthly Pass Now<\/a><\/strong><\/p><\/div>";
            }
            else if(prodZ2 <= 0){
            answerCemp.innerHTML="<div class='answerbox'><h3>Your Results<\/h3><p>Looks like you don't ride Commuter Express often enough to benefit from purchasing a Monthly Pass. Trip Tickets may be a better option for you. <strong><a href='#triptickets'>Try the Trip Tickets Calculator below.<\/a><\/strong><\/p><\/div>";
        }       
        }

        else if(selection == "z1"){
            var prodZ1 = z1yearlyCash - 960;
            if(prodZ1 > 0){
             answerCemp.innerHTML="<div class='answerbox'><h3>Your Yearly Savings<\/h3><p>If you pay cash, your yearly expense is $" + z1yearlyCash + ".<Br>Commuter Express Zone 1 Monthly Passes cost $960 per year.<br><strong>That's a yearly savings of $"  +  prodZ1  +  "<\/strong>!<br><strong><a href='http://store.ladottransit.com/Merchant2/merchant.mvc?Screen=PROD&Store_Code=LTSO&Product_Code=LADOTMPZ1&Category_Code=MP4'>Buy A Commuter Express Zone 1 Monthly Pass Now<\/a><\/strong><\/p><\/div>";
            }
            else if(prodZ1 <= 0){
            answerCemp.innerHTML="<div class='answerbox'><h3>Your Results<\/h3><p>Looks like you don't ride Commuter Express often enough to benefit from purchasing a Monthly Pass. Trip Tickets may be a better option for you. <strong><a href='#triptickets'>Try the Trip Tickets Calculator below.<\/a><\/strong><\/p><\/div>";
        }
        }

        else if(selection == "Base"){
            var prodBase = baseyearlyCash - 684;
            if(prodBase > 0){
            answerCemp.innerHTML="<div class='answerbox'><h3>Your Yearly Savings<\/h3><p>If you pay cash, your yearly expense is $" + baseyearlyCash + ".<br>Commuter Express Base Monthly Passes cost $684 per year.<br><strong>That's a yearly savings of $"  +  prodBase  +  "!<\/strong><br><strong><a href='http://store.ladottransit.com/Merchant2/merchant.mvc?Screen=PROD&Store_Code=LTSO&Product_Code=LADOTMPB&Category_Code=MP4'>Buy A Commuter Express Base Monthly Pass Now<\/a><\/strong><\/p><\/div>";
            }
            else if(prodBase <= 0){
            answerCemp.innerHTML="<div class='answerbox'><h3>Your Results<\/h3><p>Looks like you don't ride Commuter Express often enough to benefit from purchasing a Monthly Pass. Trip Tickets may be a better option for you. <strong><a href='#triptickets'>Try the Trip Tickets Calculator below.<\/a><\/strong><\/p><\/div>";
        }
        }
    }

    // End CE MP Savings Calc script -->

    </script>

</head>

<body>

<form action="javascript:window.location = 'http://translate.google.com/translate?hl=en&sl=en&tl=' + document.translateform.translate.value + '&u=' + window.location.href;" method="get" name="translateform">
                    <label for="translate">translate this page</label>
                    <select name="translate" id="translate">
                      <option value="es">Spanish</option>
                      <option value="ar">Arabic</option>
                      <option value="zh-CN">Chinese Simplified</option>
                      <option value="zh-TW">Chinese Traditional</option>
                      <option value="nl">Dutch</option>
                      <option value="fr">French</option>
                      <option value="de">German</option>
                      <option value="el">Greek</option>
                      <option value="it">Italian</option>
                      <option value="ja">Japanese</option>
                      <option value="ko">Korean</option>
                      <option value="pt">Portuguese</option>
                      <option value="ru">Russian</option>
                    </select>
                    <div class="gobuttonwrapper">
                      <input name="submit" type="submit" value="go" class="gobutton">
                    </div>
                  </form>


<div class="calcform">
  <form name="theForm3">
<h2><a name="cemp" id="cemp"></a>You Do the Math: Commuter Express Monthly Pass Vs. Cash</h2>
                        <div class="calcform-content">
                          <div class="formrow-calc">
                            <div class="calcform-col1">
                              <label for="trips-cemp">Daily boardings on Commuter Express Bus*:</label>
                              <input type="text" id="trips-cemp">

                            </div><div class="calcform-col3"></div>
                          </div>
                          <div class="formrow-calc">
                            <div class="calcform-col1">
                              <label for="days-cemp">Days you ride Commuter Express <strong>per month</strong>:</label>

                            <input type="text" id="days-cemp"></div>


                          </div>
                          <div class="formrow-calc">
                            <div class="calcform-col1">
                              <label for="choosezone">Choose Zone:</label>
                              <select name="zone" id="choosezone">
                                <option value="Base">Base</option>
                                <option value="z1">Zone 1</option>
                                <option value="z2">Zone 2</option>
                                <option value="z3">Zone 3</option>
                                <option value="z4">Zone 4</option>
                              </select>

                            </div>
                          </div>
                          <div class="clear"></div>
                          <div align="center">
                            <button type="submit" onclick="doMath3(); return false;" class="btn-submit"><img src="img/btn_savings.png" alt="Show My Yearly Savings"><br>
                            <img src="img/arrow_results.png" alt="your results will display below"></button>
                          </div>


                        </div>
                      </form>
                      <div id="answerCemp"></div>
                    </div>

</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二货你真萌 2024-11-17 04:38:59

如果您自己创建页面,特别是您想要翻译页面的各个元素,则可以使用此插件。要填充翻译数据,您可以使用谷歌翻译来获取文本短语的翻译版本。

https://github.com/coolbloke1324/jquery-lang-js

该插件有语言跨页面持久性,允许基于短语和正则表达式的匹配,可以处理任意数量的语言选项,并允许您从单击事件触发更改,如您所描述的。

您可以在 https://www.orbzu.com 上看到它的实时使用(查看页面左上角标记图标,然后选择一种新语言以查看页面上的文本自动更改)。

If you are creating the page yourself and especially where you are looking to translate individual elements of a page, you can use this plugin. To populate the translation data you could use Google Translate to get the translated versions of your text phrases.

https://github.com/coolbloke1324/jquery-lang-js

That plugin has language persistence across pages, allows both phrase and regex-based matching, can handle any number of language options and allows you to trigger the change from a click event as you described.

You can see it being used live on https://www.orbzu.com (look top-left of the page for the flag icon and then select a new language to see the text on the page change automatically).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文