How to insert translated text into database php
Asked Answered
M

1

6
<!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>

I want to save a translated text inside a php mysql table. Currently, the system will echo in translated text but it will not save in the database as the text i typed. For example, if i type the word "test" and translate it to japanese as "テスト", the database will save the word "test" and not "テスト". how can i make it save the translated text?

Miraculous answered 30/3, 2016 at 8:17 Comment(10)
what collation has the field testmsg?Handspring
you probably have to check this out : dev.mysql.com/doc/refman/5.5/en/charset-charsets.htmlHandspring
hi, the collation involved inside is utf8_binMiraculous
Try $mysqli->query("SET NAMES utf8"); before mysqli_query($dbconnectivity, $sql);Bartholomeus
I dont think is the collalation problem because i can save japanese characters if i insert from mysql.Miraculous
It looks like you're setting the value of hiddenInput to being the innerHTML value of mymessage ... which is the untranslated text I presume - rather than the response from Google Translate. You're then inserting that untranslated text into the database when you post the form with $_POST['hiddenInput']Grewitz
the hidden input does not store the translated text. the translated text is being generated outside the form : <font class="">translated text</font>Handspring
So essentially he needs to take the response from Google Translate (the translated text) and use that to update the value of hiddenInput before the form is submitted; I'm pretty sure that's the solution but having never used the Google Translate API I don't know how to implement it so I'll leave an answer to someone more knowledgeable than I.Grewitz
@Grewitz this was the idea, but google translate generates the translate outside the form. i guess the solution is to get the outer HTML after the translation, replace the unwanted part and then post the parsed data.Handspring
thx for the reply. can i know what i can do to save the translated word then?Miraculous
T
0

You can use the Google Translation API to translate given post/get data with PHP directly. Than save it to any storage.

There is a good documentation here:

Example (from the docs):

<!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'];
}
?>

You can extend the example with a language select-box as well.

Tolyl answered 23/8, 2017 at 9:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.