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
<?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
*/
?>
<?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
<?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 🙁