How to insert translated text into php database

<!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 the translated text inside the mysql php table. Currently, the system will echo in the translated text, but it will not be saved in the database as the text I printed. For example, if I type the word โ€œtestโ€ and translate it into Japanese as โ€œใƒ† ใ‚น ใƒˆโ€, the database will save the word โ€œtestโ€ rather than โ€œใƒ† ใ‚น ใƒˆโ€. How can I make it save the translated text?

+5
source share
1 answer

You can use the Google translation API to directly translate post / get data using PHP. Then save it to any storage.

There is some good documentation here:

Example (from documents):

 <!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 also extend the example using the language selection field.

0
source

Source: https://habr.com/ru/post/1246050/


All Articles