2014-12-05 76 views
0

我有一個包含約700個術語的數據庫。我只想寫一個名爲internationalize的php文件,它將檢查mysql數據庫並返回正確的翻譯。所有的PHP變量已經設置完畢,並且有包含所有語言翻譯的視圖。大約有11種語言。我在很多地方看過,但沒有一個適合我的情況。任何建議將不勝感激。創建一個用於動態翻譯內容的php文件

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? 
} 

我也附加了數據庫視圖的副本。我打算編寫代碼是這樣一種方式,如果沒有語言翻譯,默認翻譯是英文。

User image

+2

任何你不想使用gettext和POT文件的原因? – techouse 2014-12-05 22:48:25

+0

你想翻譯什麼文字?我不明白什麼是輸入。 – 2014-12-05 22:48:36

+1

看起來像腳本爲php變量賦值取決於所選擇的語言。看起來該腳本生成了幾個變量,它們從數據庫中獲取它的值,並在腳本的其餘部分中使用它們。 – Sirac 2014-12-05 22:56:36

回答

0

這就是我所做的,它的工作原理。感謝您的幫助!

<?php 
//Connects to the database 
$mysqlserver="servername"; 
$mysqlusername="langreader"; 
$mysqlpassword="[email protected]"; 
$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!!!! 
} 
}