【问题标题】:Translate content of input field on click with google and php onclick使用 google 和 php onclick 在点击时翻译输入字段的内容
【发布时间】:2019-11-23 16:05:07
【问题描述】:

我正在尝试使用 php 翻译 input type="text"field 中的内容。

到目前为止,我得到了这个,但我有点迷茫......

HTML:

<form id="featured" name="featured" method="post" action="#" enctype="multipart/form-data">
<input type="text" style="width:300px;height:200px;" name="ttt" id="ttt" value="<?php echo $description_2_translate ;?>" onclick="translate_text()"/>
<input id="" name="" type="submit" value="Upload"/>
</form>

PHP:

   <?php   
    function translate_text($from_language, $to_language, $description_2_translate){
    $json = json_decode(file_get_contents('https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($description_2_translate) . '&langpair=' . $from_language . '|' . $to_language));
    $translated_text = $json->responseData->translatedText;

    return $translated_text;
     }?>

我在这里做错了什么?

【问题讨论】:

  • 嗯,会发生什么?你得到什么错误?
  • @ceejayoz 没有任何反应。这就是问题所在……
  • 好吧,您并没有在您显示的代码中的任何地方调用translate_text 函数。这就是我要开始的地方,除非你忘记分享你的一些代码。
  • @ceejayoz 我编辑了问题,您可以看到我在输入字段&lt;input type="text" style="width:300px;height:200px;" name="ttt" id="ttt" value="&lt;?php echo $description_2_translate ;?&gt;" onclick="translate_text()"/&gt; 上调用了该函数。不过,什么也没发生……
  • 不能使用onclick直接调用PHP函数。那是调用一个可能不存在的 JavaScript 函数。如果你想这样做,你需要研究 AJAX。

标签: php google-api google-translate


【解决方案1】:
{"responseData": null, "responseDetails": "Please use Translate v2.  See http://code.google.com/apis/language/translate/overview.html", "responseStatus": 403}

您正在使用已弃用的 API。

Google 已关闭免费版翻译器,并要求用户在 2011 年之前切换到付费版。

【讨论】:

  • 很有可能,但 OP 甚至没有解决这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-13
  • 1970-01-01
  • 1970-01-01
  • 2018-06-25
  • 2012-05-24
  • 2022-12-25
相关资源
最近更新 更多