javascript - 如何将翻译后的文本插入数据库php

标签 javascript php html mysql google-translate

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<form action="" method="post" name="theform">
  <table width="693" border="1" style="table-layout:fixed;">
    <tr>
      <td width="683" id="mymessage" contenteditable="true" name="mymessage">Write message here...</td>
    </tr>
  </table>
  <script>
document.getElementById('mymessage').addEventListener('input', function() {
    document.getElementById('hiddenInput').value = this.innerHTML;
     console.log(document.getElementById('hiddenInput').value);
});

document.getElementById("mymessage").addEventListener("click", removePlace);    
function removePlace()
{
    document.getElementById("mymessage").innerHTML="";
}
</script>
<div id="google_translate_element"><span class="notranslate">Select language to translate your text above:</span></div>
<script type="text/javascript">
function googleTranslateElementInit() 
{
  new google.translate.TranslateElement({pageLanguage: 'en', includedLanguages: 'en,fr,it,ja,ko,ms,ru,ta,th,zh-CN', layout: google.translate.TranslateElement.InlineLayout.SIMPLE, multilanguagePage: true}, 'google_translate_element');
}
</script><script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
<input type="hidden" id='hiddenInput' name='hiddenInput'>
  <span class="notranslate"><input type="submit" id="btnSend" name="btnSend" value="Send"></span>
</form>
<?php
$servername = "localhost";
$username = "mytranslateim";
$password = "qwerty";
$dbname = "test";
$dbconnectivity = mysqli_connect($servername, $username, $password, $dbname);
if (isset($_POST['btnSend']))
{
    $getmsg = $_POST['hiddenInput'];
    if($getmsg == "")
    {
        echo "nothing";
    }
    else
    {
    echo $getmsg;
    $sql = "INSERT INTO testing(testmsg) VALUES ('$getmsg')";//if i translated a text, for example i translate the word "test" in chinese, it will echo in chinese but will not save in database as chinese 
    $insertit = mysqli_query($dbconnectivity, $sql);
    }
}
?>
</body>
</html>

我想将翻译后的文本保存在 php mysql 表中。目前,系统将以翻译后的文本回显,但不会将我输入的文本保存在数据库中。例如,如果我输入单词“test”并将其翻译成日语为“テスト”,数据库将保存单词“test”而不是“テスト”。我怎样才能让它保存翻译后的文本?

最佳答案

您可以使用 Google Translation API 通过 PHP 直接翻译给定的发布/获取数据。然后将其保存到任何存储空间。

这里有一个很好的文档:

示例(来自文档):

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<form method="post" accept-charset="utf-8">
  <input type="text" name="translate" value="enter your text to translate..." />
  <input type="submit" name="submit" value="translate" />
</form>
</body>
</html>
<?php
if (isset($_POST['submit']) {
# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';

# Imports the Google Cloud client library
use Google\Cloud\Translate\TranslateClient;

# Your Google Cloud Platform project ID
$projectId = 'YOUR_PROJECT_ID';

# Instantiates a client
$translate = new TranslateClient([
    'projectId' => $projectId
]);

# The text to translate
$text = $_POST['translate'];
# The target language
$target = 'jp';

# Translates some text into Russian
$translation = $translate->translate($text, [
    'target' => $target
]);

# This is the result. You can save it to any storage.
echo 'Text: ' . $text . '
Translation: ' . $translation['text'];
}
?>

您也可以使用语言选择框扩展示例。

关于javascript - 如何将翻译后的文本插入数据库php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36303266/

相关文章:

php - PHP Composer Autoload 找不到类错误

javascript - 获取可拖动对象相对于 div 的位置

html - 在 div 内垂直居中图像不起作用

javascript - 如何使用 react-native-maps 根据缩放值调整在 map 上绘制的形状的大小

javascript - 在 Canvas 中绘制多个圆弧的 JQuery 动画问题

php - 在数据库中安全存储 HTML,而不影响字符编码

PHP For Loop 循环遍历数组并将当前数组项插入sql数据库,仅插入一条记录

html - 如何将查询参数和类属性传递给 MVC3 中的 Html.BeginForm?

javascript - 如何在播放按钮效果上复制愤怒的小鸟鼠标悬停

javascript - 如何在 JavaScript 中向变量添加数字以进行温度转换?