I have a database that has around 700 terms. I would just like to write a single php file called internationalize that would check the mysql database and return the proper translation. All the PHP variables have been setup and there is view that contains the translation of all the languages. There are about 11 languages. I have looked in a lot of places, but there isn't one that fits my situation. Any suggestions would be greatly appreciated.
if (!isset($_SESSION))
{
session_start();
}
if (isset($setup))
{
$lang=$_SESSION['setupLang'];
}
elseif (isset($_SESSION['lang']))
{
$lang=$_SESSION['lang'];
}
else
{
require("fetchMainConfig.php");
}
$lang_code = $lang;
//file_dir contains the language codes for example: il,fr,pt,ge and so on
$sql = "SELECT file_dir FROM `hydroserver_translation`.`language_file_dir`";
if($lang_code = $sql){
// What should go in here??? What is the best way to
// dynamically translate the database?
}
I am also attaching a copy of the database view. I plan to write the code is such a way that if there is no language translation the default translation is in english.
This is what I have done and it works. Thank you for your help!
<?php
//Connects to the database
$mysqlserver="servername";
$mysqlusername="langreader";
$mysqlpassword="readHSLlang@9";
$error=0;
$link=mysqli_connect($mysqlserver, $mysqlusername, $mysqlpassword) or $error=1;
$dbname = 'translation';
mysqli_select_db($link, $dbname) or $error=1;
if(!$error)
{//check which is the session language
$language = $lang_code;
//language file path
$file_path = "languages/" .$language. ".php";
//Check if file exists
$file_exists = file_exists($file_path);
if($file_exists){
// The file exists. Now just check when it was last time created.
$file_created_time = filemtime($file_path);
$timezone = date_default_timezone_set('UTC');
$current_time = time();
//Time lapse to check the difference between the current time and the last created time
$time_lapse = (abs($current_time-$file_created_time)/60/60);
//SQL statement to access the view from the database
$sql = "SELECT * FROM translation.translations_by_language";
$terms = mysqli_query($link, $sql);
//Will create a new file if it has been more than four hours
if($time_lapse >= '4.0'){
//Deleting the existing file to avoid any parsing errors
unlink($file_exists);
//Writing the new language_file
$lang_file= fopen("languages/" .$language. ".php","c+");
//Loops through the query and shows the translated terms
//and english terms if there are no translations for the term
$new_file = "<?php" . "\n ";
fwrite($lang_file, $new_file);
while($row = mysqli_fetch_array($terms)) {
if ($row[$language] != "")
fwrite($lang_file,$row['php_variable']. " = " . '"' . addslashes($row[$language]) . '"' . ";" . "\n ");
else
fwrite($lang_file,$row['php_variable']. " = " . '"' . addslashes($row['english_phrase']) . '"' . ";" . "\n ");
}
$last_line = "?>";
fwrite($lang_file, $last_line);
fclose($lang_file);
}
}
else{
// Creating a new file if the file doesn't exist
$sql = "SELECT * FROM hydroserver_translation.translations_by_language";
$terms = mysqli_query($link, $sql);
$lang_file= fopen("languages/" .$language. ".php","c+");
$new_file = "<?php" . "\n ";
fwrite($lang_file, $new_file);
while($row = mysqli_fetch_array($terms)) {
print_r($row);
if ($row[$language] != "")
//Provide the translation
fwrite($lang_file,$row['php_variable']. " = " . '"' . addslashes($row[$language]) . '"' . ";" . "\n ");
//If translation doesn't exist, just use the english phrase
else
fwrite($lang_file,$row['php_variable']. " = " . '"' . addslashes($row['english_phrase']) . '"' . ";" . "\n ");
}
$last_line = "?>";
fwrite($lang_file, $last_line);
fclose($lang_file);
//New language file succesfully created!!!!
}
}