In this tutorial I complete my Translation Web Service Example. If you want the whole database to use in any way it is here. All of the code can be found down below. As well if you want to test the web service find out how to translate “I like dogs and cats” here.
This is a transliteration web service in which it directly translates from one word to another. It isn’t perfect, but it is the best I could do in 2 hours. I hope you find it useful.
If you like videos like this, it helps others to find them with a click here [googleplusone]
I’m giving away my last Samsung Galaxy Note 3 and Samsung Galaxy Gear Smart watch. Check out this contest if you’d like to win both.
Code From the Videos
translatetest.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php header('Content-Type:application/json; charset=UTF-8'); $english_words = "My dog likes to eat chicken"; $encoded_words = urlencode($english_words); // Get the specific student data $translated_words = file_get_contents('http://newjustin.com/translateit.php?action=translations&english_words=I+like+cats'); echo $translated_words; /* DUMP DATABASE mysqldump -u mysqladmin -p language > lang_bu.sql */ ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
<?php header('Content-Type:application/json; charset=UTF-8'); function translate($translate_to, $matching_ids, $dbc){ // Represents the id name in the table so for Russian it // would be russian_id $translate_id = $translate_to . '_id'; /* SELECT russian.word FROM russian JOIN( SELECT 131 AS id, 1 AS word_order UNION ALL SELECT 26, 2 UNION ALL SELECT 26, 3 UNION ALL SELECT 69, 4) WordsToSearch ON russian.russian_id = WordsToSearch.id ORDER BY WordsToSearch.word_order; */ $translate_query = 'SELECT word FROM ' . $translate_to . ' JOIN(' . 'SELECT ' . $matching_ids[0] . ' AS id, 1 AS word_order'; $array_size_2 = count($matching_ids); for ($i = 1; $i < $array_size_2; $i++) { $translate_query = $translate_query . " UNION ALL SELECT '" . $matching_ids[$i] . "', " . ($i + 1) . " "; } $translate_query = $translate_query . ') WordsToSearch ON ' . $translate_to . '.' . $translate_id . ' = WordsToSearch.id ORDER BY WordsToSearch.word_order;'; // Issue the query to the database $translate_response = mysqli_query($dbc, $translate_query); if($translate_response){ while($row = mysqli_fetch_array($translate_response)){ $translated_text = $translated_text . ' ' . $row['word']; } } return $translated_text; } // Close function translate function get_translation($translate_to, $english_words){ // Trim white space from the name and store the name $english_words = trim($english_words); $english_array = array(); // Break the words into an array $english_array = explode(" ", $english_words); // Get a connection to the database include('../mysqli_connect_language.php'); // Set character set in PHP to get proper characters $dbc->set_charset("utf8"); /* UNION ALL combines the result set of multiple SELECT statements which makes sure we receive all rows even if there are duplicates SELECT english.word FROM english JOIN ( SELECT 'A' AS word, 1 AS word_order UNION ALL SELECT 'dog', 2 UNION ALL SELECT 'a', 3 UNION ALL SELECT 'cat', 4) WordsToSearch ON english.word = WordsToSearch.word ORDER BY WordsToSearch.word_order; */ $query = "SELECT english_id, english.word FROM english JOIN( SELECT '" . $english_array[0] . "' AS word, 1 AS word_order"; // Get the size of the array $array_size = count($english_array); for ($i = 1; $i < $array_size; $i++) { $query = $query . " UNION ALL SELECT '" . $english_array[$i] . "', " . ($i + 1) . " "; } $query = $query . ") WordsToSearch ON english.word = WordsToSearch.word ORDER BY WordsToSearch.word_order;"; // Issue the query to the database $response = @mysqli_query($dbc, $query); // Array that contains the matching ids in order $matching_ids = array(); if($response){ while($row = mysqli_fetch_array($response)){ $matching_ids[] = $row['english_id']; // Holds the array after the select $array_after_query[] = $row['word']; } } // Close if($response) return translate($translate_to, $matching_ids, $dbc); } // NEW STUFF function get_all_translations($english_words){ $language_array = array("arabic", "chinese", "danish", "dutch", "french", "german", "italian", "portuguese", "russian", "spanish"); $translations_array = array(); foreach($language_array as $language){ $translations_array[] = get_translation($language, $english_words); } $all_translations = '{"translations": ['; $index = 0; foreach($language_array as $language){ $all_translations = $all_translations . '{"' . $language . '":"' . $translations_array[$index] . '"},'; $index++; } $all_translations = rtrim($all_translations, ","); $all_translations = $all_translations . ']}'; return $all_translations; } // END OF NEW STUFF if(isset($_GET["action"])){ // NEW STUFF switch($_GET["action"]){ case "translate": $translate_to = $_GET["language"]; $english_words = urldecode($_GET["english_words"]); $translated_text = get_translation($translate_to, $english_words); break; case "translations": $english_words = urldecode($_GET["english_words"]); $translated_text = get_all_translations($english_words); } // END OF NEW STUFF /* $translate_to = $_GET["language"]; $english_words = urldecode($_GET["english_words"]); $translated_text = get_translation($translate_to, $english_words); */ } echo $translated_text; // exit($translated_text); ?> |
mysqli_connect_langauge.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php // Defined as constants so that they can't be changed DEFINE ('DB_USER', 'studentweb'); DEFINE ('DB_PASSWORD', 'turtledove'); DEFINE ('DB_HOST', 'localhost'); DEFINE ('DB_NAME', 'language'); // $dbc will contain a resource link to the database // @ keeps the error from showing in the browser $dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die('Could not connect to MySQL: ' . mysqli_connect_error()); ?> |
got it thanks but waiting for subtitles as mentioned earlier……….
I added subtitles to every video YouTube would allow. Sorry some times it doesn’t work 🙁